react-native - 无法使用 react-native-ble-plx 从 BLE 读取
问题描述
我在我的 Ubuntu 机器上使用 React Native 和 BLE 包 react-native-ble-plx。
根据文档,我做了以下事情:
- 已扫描设备
- 连接到设备
- device.discoverAllServicesAndCharacteristics
- writeCharacteristicWithResponseForDevice
我的问题在于阅读我从发送命令中得到的响应。
RX 特性是可写的,但 TX 只是可通知的而不是可读的。因此我需要使用 monitorCharacteristicForDevice 来监控特性,但是我无法让它发挥作用。
在下面的代码中,我从未收到过“监控成功”、“从设备接收数据时出错”或“监控失败”。
我如何阅读回报?
monitorDevice = () => {
this.setState({monitoring: true})
console.log(this.state.servicesDiscovered)
if(this.state.servicesDiscovered){
{this.manager.monitorCharacteristicForDevice(this.state.connectedTo,
SERVICE_UUID,
TX_CHARACTERISTIC,
(error, characteristic) => {
if (error) {
console.error("Error at receiving data from device", error);
return
}
else {
this.setState({monitoring: true})
console.log('monitor success')
console.log('monitor success' + characteristic.value);
this.state.messagesRecieved.push(characteristic.value)
}
})
}
}
else{
console.log("Monitor failure")
}
}
checkBattery = () => {
if(this.state.monitoring){
this.manager.writeCharacteristicWithResponseForDevice(this.state.connectedTo,
SERVICE_UUID,
RX_CHARACTERISTIC,
"YmF0dGVyeQ==")
.then(characteristic => {
console.log("Successfully sent: " + characteristic.value)
return
})
.catch(err => {
console.log(err)
})
}
else{
alert("Not monitoring")
}
}
解决方案
推荐阅读
- python - 尝试通过命令行/gitbash 将 Python27 添加到路径时出现问题
- hive - msck 修复表是否需要 hadoop/map-reduce?
- javascript - React react-bootstrap - 如何从外部组件关闭模式窗口
- algorithm - 如何解决合并排序中的递归错误?
- javascript - JavaScript 将数组与数组对象进行比较以查看它们的值是否匹配
- reactjs - Reducer 改变了状态但没有重新渲染
- node.js - npm 调试模块:如何以精美的字体显示对象?
- javascript - BrowserSync:拒绝执行内联脚本,锚标签不起作用
- apache-spark-sql - 分析表计算统计信息 - 未显示统计信息
- swift - UITableView 或 UILabel 在使用委托和协议观察者方法时发现 NIL,因为它在 viewDidLoad 之前调用