简体   繁体   English

在使用 Chrome 中的实验性 BLE 扫描发现的蓝牙设备上访问 GATT 服务器

[英]Get access to GATT server on a bluetooth device discovered using experimental BLE scanning in Chrome

I am working on a POC where I use the experimental BLE scan interface to discover multiple Bluetooth devices, and connect to their GATT services from a web application.我正在开发一个 POC,我使用实验性 BLE 扫描接口来发现多个蓝牙设备,并从 web 应用程序连接到它们的 GATT 服务。

Requesting a single device using navigator.bluetooth.requestDevice yields a device where I can connect to the GATT server.使用navigator.bluetooth.requestDevice请求单个设备会产生一个可以连接到 GATT 服务器的设备。

When when I try to discover devices using requestLEScan , the devices I receive does not allow me to connect to the GATT server.当我尝试使用requestLEScan发现设备时,我收到的设备不允许我连接到 GATT 服务器。

  const [ devices, dispatch ] = useReducer(reducer, {});
  const scanClick = () => {
    navigator.bluetooth.requestLEScan({
      acceptAllAdvertisements: true,
    }).then((scan) => {
      setTimeout(() => scan.stop(), 10000);
    });
    navigator.bluetooth.addEventListener('advertisementreceived', (event) => {
      console.log("DEVICE DETECTED", event);
      parseBluetoothDevice(event.device).then(x => x && dispatch(x));
    });
  }

const parseBluetoothDevice = async (device) => {
  // Should return a representation with the device information I want to display
  await device.gatt.connect();
  ...
}

The call to device.gatt.connect() throws an error:device.gatt.connect()的调用会引发错误:

GATT operation not authorized GATT 操作未经授权

How do I get authorized to access the GATT server on the detected device?如何获得授权访问被检测设备上的 GATT 服务器?

Is my problem that I need to call permissions.request() , which is not yet implemented for Bluetooth?我的问题是我需要调用还没有为蓝牙实现的permissions.request()吗? ( Bluetooth implementation status ) 蓝牙实施状况

I have tried instead of setting acceptAllAdvertisement , to pass filters with a service UUID that I know my devices support - and that I want to query, but then I don't see any scan results.我尝试而不是设置acceptAllAdvertisement ,而是使用我知道我的设备支持的服务 UUID 传递filters - 并且我想查询,但是我没有看到任何扫描结果。

I am running Chrome 83.0.4103.97 on MacOS with "experimental web features" enabled.我在 MacOS 上运行 Chrome 83.0.4103.97 并启用了“实验性 web 功能”。

The requestLEScan() API only grants permission to start a scan and see advertisement packets from nearby devices, but it does not grant permission to connect to these devices. requestLEScan() API 仅授予开始扫描和查看来自附近设备的广告包的权限,但不授予连接到这些设备的权限。 To do this, you would need to use requestDevice() and add the services that you're interested in using to the filter or to optionalServices .为此,您需要使用requestDevice()并将您有兴趣使用的服务添加到过滤器或optionalServices中。

In Chrome, the prompt displayed when requestDevice() requires the user to select the Bluetooth device that they want to allow the site to connect to, whereas the prompt displayed for requestLEScan() only asks if the user wants to allow the site to scan for nearby Bluetooth devices.在 Chrome 中, requestDevice()时显示的提示要求用户 select 允许站点连接的蓝牙设备,而requestLEScan()显示的提示仅询问用户是否要允许站点扫描附近的蓝牙设备。 This is why you aren't able to connect to these devices.这就是您无法连接到这些设备的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM