最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to initiate a new device pairing using the Chrome web bluetooth API? - Stack Overflow

programmeradmin0浏览0评论

Using navigator.bluetooth.requestDevice(), I can access bluetooth devices I've already set up a pair with using native OSX bluetooth pairing, but no previously-unpaired devices appear, even when their attributes match my generic query. Available devices show up in a Chrome modal requesting user consent to pair, but the only device that shows up is the one I've already paired with.

Am I misunderstanding the intended use case here, or is there another way to establish a connection with a nearby (previously unpaired) device from Chrome?

Docs: / (See Example 2)

function bluetoothConnect() {
  navigator.bluetooth.requestDevice({filters: [{services: ['generic_access']}]})
	  .then(device => {console.log(`Connected to: ${device.name}`)})
	  .catch(console.error);
}

Using navigator.bluetooth.requestDevice(), I can access bluetooth devices I've already set up a pair with using native OSX bluetooth pairing, but no previously-unpaired devices appear, even when their attributes match my generic query. Available devices show up in a Chrome modal requesting user consent to pair, but the only device that shows up is the one I've already paired with.

Am I misunderstanding the intended use case here, or is there another way to establish a connection with a nearby (previously unpaired) device from Chrome?

Docs: https://webbluetoothcg.github.io/web-bluetooth/ (See Example 2)

function bluetoothConnect() {
  navigator.bluetooth.requestDevice({filters: [{services: ['generic_access']}]})
	  .then(device => {console.log(`Connected to: ${device.name}`)})
	  .catch(console.error);
}

Share Improve this question edited May 9, 2016 at 6:06 Alex Patch asked May 6, 2016 at 22:30 Alex PatchAlex Patch 1251 silver badge9 bronze badges 1
  • hi.. hope you found the answer and seems, its been long time you worked on this issue. In case, you recall, how do you actually unpair the BLE device via api? – UGandhi Commented Mar 11, 2022 at 0:00
Add a ment  | 

2 Answers 2

Reset to default 6

First, Mac OS X is not yet fully implemented as we speak. Only discovery and GATT server connect/disconnect are working for now. See the Chrome Implementation status at https://github./WebBluetoothCG/web-bluetooth/blob/gh-pages/implementation-status.md. Check out Chrome OS, Linux and Android M (Android Lollipop workaround).

Regarding your specific issue, I believe that generic_access is not broadcasted by a nearby BLE device but is found because you've already paired (cached) this device. If your device is named "foo" for instance, you can go to https://googlechrome.github.io/samples/web-bluetooth/device-info.html and fill "foo" as the Device Name and hit "Get Bluetooth Device Info" button.

I would remend you give a try to all Web Bluetooth samples at https://googlechrome.github.io/samples/web-bluetooth/index.html as well.

To plement the other answer, please be aware of the Chrome's chrome://bluetooth-internals tab/tool. This tool can not only list available devices and their services (which I don't think you can do with the Web Bluetooth API unless you requested the services in optionalServices or filters/services) and the characteristics of those services.

In the device list, there is a Forget button which bees active once you connect to the devices GATT server using the Inspect button. This should allow you to full unpair and pair again in cases where you need to test the flow end to end.

Edit: Actually after doing some experimentation around this, the device still remains paired even after using the Forget button. Probably a bug in Chrome, however, the cache of the paired devices seems to be per-profile. This means the real answer is to:

Use the Incognito mode and in there the pairing flow will initiate from the get-go for each new Incognito session.

发布评论

评论列表(0)

  1. 暂无评论