如何连接到 Ble 设备而无需重新扫描和手动设备选择
我正在创建一个 Ionic React (TypeScript) 应用程序,它使用 社区蓝牙插件。
当我尝试使用 requestDevice 连接到设备时,这会显示可用的设备和然后我可以与该设备配对/连接,一切都很好。
await BleClient.initialize();
if (isAndroid) {
await BleClient.disconnect(devices.deviceId);
}
const device = await BleClient.requestDevice({
services: services ? services : [],
optionalServices: optionalServices ? optionalServices : [],
namePrefix: prefixFilter ? prefixFilter : "",
});
await BleClient.connect(device.deviceId, (deviceId) => onDisconnect(deviceId));
await BleClient.getServices(device.deviceId).then(
(services) => {
if (services[0]) {
//....
} else {
//....
}
}
)
但是,如果我保存设备 ID,然后尝试使用 getDevices< 直接与该设备连接/a> (而不是扫描和手动连接)它总是失败并显示以下控制台输出:
未捕获(承诺)错误:未找到设备。首先调用“requestDevice”、“requestLEScan”或“getDevices”。
我使用的代码是这样的:
await BleClient.initialize();
if (isAndroid) {
await BleClient.disconnect(devices.deviceId);
}
await BleClient.getDevices(devices.deviceId);
await BleClient.connect(devices.deviceId, (deviceId) => onDisconnect(deviceId));
澄清:我希望能够搜索可用设备并在第一次打开应用程序时连接到设备。然后,保存设备 ID 并使用此 ID 从此时开始使用 getDevices 直接连接到设备。同样,如果应用程序关闭并重新打开,我需要能够获取存储的设备数据并直接与该设备连接,而无需整个扫描和手动选择过程。
我不明白我错过了什么。
I'm creating an Ionic react (TypeScript) app which uses the Community Bluetooth-le plugin.
When I try to connect to a device using requestDevice this shows the available devices and I can then pair/connect with that device and all is good.
await BleClient.initialize();
if (isAndroid) {
await BleClient.disconnect(devices.deviceId);
}
const device = await BleClient.requestDevice({
services: services ? services : [],
optionalServices: optionalServices ? optionalServices : [],
namePrefix: prefixFilter ? prefixFilter : "",
});
await BleClient.connect(device.deviceId, (deviceId) => onDisconnect(deviceId));
await BleClient.getServices(device.deviceId).then(
(services) => {
if (services[0]) {
//....
} else {
//....
}
}
)
However, if I save the device ID and then try to directly connect with that device using getDevices (rather than scanning and manually connecting) it always fails with the following console output:
Uncaught (in promise) Error: Device not found. Call "requestDevice", "requestLEScan" or "getDevices" first.
The code I use is this:
await BleClient.initialize();
if (isAndroid) {
await BleClient.disconnect(devices.deviceId);
}
await BleClient.getDevices(devices.deviceId);
await BleClient.connect(devices.deviceId, (deviceId) => onDisconnect(deviceId));
For clarification: I want to be able to search for available devices and connect to the device the first time the app is opened. Then, save the device ID and use this ID to connect to the device directly using getDevices from that point onwards. Likewise if the app is closed and re-opened I need to be able to take the stored device data and connect with that device directly without the whole scan and manual selection process.
I don't understand what I'm missing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设设备 1(应用程序)正在扫描,设备 2 正在广告。首次连接后尝试绑定设备。这样您下次就可以自动连接,无需扫描。
确保与 Device-1 断开连接后,Device-2 处于可连接模式。
EDIT-1
例如,我正在使用名为 nRF connect 的通用应用程序和智能手表。在此应用程序中,我们可以扫描并连接任何 BLE 设备。我按照以下步骤操作:
第三步之后,设备被绑定,以后只要设备在附近,您就可以与其连接,而无需广告和扫描。 PFA 图像。
这是要遵循的流程的概述,关于代码,您可以找到许多与 android 或 iOS 相关的现成示例。希望这有帮助!
I assume Device-1 (app) is scanning and Device-2 is advertising. Try to bond the devices after first time connection. This allow you to connect it automatically without scanning next time.
Make sure the Device-2 is in connectable mode after getting disconnected from Device-1.
EDIT-1
For example I am using a generic app called nRF connect and a smart watch. In this app we can scan and connect with any BLE device. I am following below steps:
After third step, device get bonded and later whenever the device is in vicinity you can connect with it, without advertising and scanning. PFA image.
This is the overview of process to be followed, in regards to code you can get many ready examples related to android or iOS. Hope this is helpful!!