使用React Native App连接蓝牙设备



我使用的是react原生蓝牙串行。每当我试图通过我的react原生应用程序连接蓝牙设备时,我都会收到这个错误:

错误:读取失败,套接字可能关闭或超时,读取ret:-1无法连接到设备

以下是我要做的:

connectToDevice () {
if (this.state.discovering) {
return false
} else {
this.setState({ discovering: true })
console.log("searching");
BluetoothSerial.list()
.then((Devices) => {
this.setState({ Devices, discovering: false })
for(var i = 0; 1; i++)
{
if(Devices[i]["name"] == "JBL Flip 3 SE")
{
console.log(Devices[i]["id"]);
BluetoothSerial.connect(Devices[i]["id"]).then((res) => {
console.log(res);
}).catch(err => {
console.log(err);
})
break;
}
}
// console.log(unpairedDevices);
})
.catch((err) => console.log(err.message))
}
}

即使在克隆示例存储库时也会发生同样的情况。

知道为什么会发生这种事吗?

我鼓励您在react native中使用react native ble plx用于蓝牙,如果您需要实际示例,您可以查看我们的辅助项目,该项目使用此库向Arduino发送颜色并处理LED灯https://github.com/SparingSoftware/HomeLed

最新更新