【发布时间】:2020-01-27 12:57:26
【问题描述】:
我正在使用ble-plx 库通过蓝牙连接到设备。
我写了这段代码,但我不明白为什么有时它会失败,而有时却完美无缺。
错误在哪里,这意味着我的代码有时会失败但其他方面可以正常工作?
(我正在连接两台设备,所以我不知道在扫描中首先找到哪个。)。
scansione1() {
this.manager.startDeviceScan(null, null, (error, device) => {
if (error) {
// This is used if failed immediately the scan
this.manager.stopDeviceScan();
Alert.alert("Error.")
Actions.homepageutente()
return; }
console.log("Start Scan 1");
if ((device.name == this.model_dx(this.props.Model)) || (device.name == this.model_sx(this.props.Model)))
{
this.manager.stopDeviceScan();
this.setState({dispositivo1: device.name})
// I set this variable device1 because I'll use it in other page.
this.setState({device1: device})
device
.connect()
.then( () => {
console.log("Launch scansione 2")
this.scansione2();
})
.catch(() => {
// --> When My connection fails I receive this error <--
Alert.alert("Connection bluetooth device 1 not working");
Actions.homepage();
});
}
else if ((device.name == null )) {
this.manager.stopDeviceScan();
this.scansione1();
} else {
this.manager.stopDeviceScan();
Alert.alert("Device " + device.name + " Not find.");
Actions.homepage();
}
});
}
非常感谢:)
【问题讨论】:
-
代码失败时会发生什么?有错误信息吗?会发生什么?
-
@ipsascientiapotestas 我写了一条评论,当它失败时,我收到进入 catch
-
如果再次运行代码,报错后能否重新连接?你可能只是连接不好
-
是的,如果我重试它可以工作(可能是第三次)。所以不是代码的问题?我不明白为什么它不应该工作
-
我可能是错的,但我看不出代码本身有什么大问题,尤其是当你重试时它确实有效,所以它可能是外部因素
标签: javascript reactjs react-native mobile-application