我有一个应用程序,用于使用 BLE-PLX 库通过蓝牙连接到外部设备。
我在扫描和连接过程中遇到问题,因为并不总是有效。
这是我用来通过蓝牙连接一台设备的代码:
// First Scan
scans1() {
this.manager.startDeviceScan(null, null, (error, device) => {
if (error) {
this.manager.stopDeviceScan();
console.log("0.Error, retry connection.")
this.scans1()
//return;
}
if ((device.name == this.model_dx(this.props.Model)) || (device.name == this.model_sx(this.props.Model)))
{
this.manager.stopDeviceScan();
console.log("1.Device Founded - ", device.name)
this.setState({device1: device})
this.manager.connectToDevice(device.id)
.then(() => {
console.log("2.Launch Scans 2") // this is for connect to the second device.
this.scan2();
})
.catch(() => {
Alert.alert("Error " + "Connection Failed.");
Actions.homepage();
})
}
else if ((device.name == null )) {
this.manager.stopDeviceScan();
console.log("3.Device is - null - retry scan")
this.scans1();
} else {
this.manager.stopDeviceScan();
console.log("4.Error: Device not found.")
Actions.homepage();
}
});
现在我想知道的是,如何查看设备是否已连接? (我不知道是否可能是另一种错误,我已尝试解决任何类型的问题)。
按照他们所说的指南:
检查设备的连接状态。
isDeviceConnected(deviceIdentifier: DeviceId): Promise<boolean>
参数
deviceIdentifier (DeviceId)
设备标识符。
返回
Promise<boolean>:
如果设备已连接,则 Promise 发出 true,否则发出 false。
但是我不明白如何使用它。
最佳答案
我找到了解决方案:
我可以使用:
this.manager.isDeviceConnected(device.id).then((res) => console.log(res))
关于javascript - 如何查看设备是否已连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58218842/