我可以与硬件建立BLE连接。通过使用服务UUID和特征UUID,我能够通过启动通知功能接收来自硬件的数据。但是当我尝试向硬件发送数据时,它显示错误写入错误状态,如下面的代码所示。
BleManager.retrieveServices(peripheral.id).then((peripheralInfo) => {
console.log(peripheralInfo);
var service = '6e400001-b5a3-f393-e0a9-e50e24dcca9e';
var WriteCharacteristic = '6e400002-b5a3-f393-e0a9-e50e24dcca9e';
var ReadCharacteristic = '6e400003-b5a3-f393-e0a9-e50e24dcca9e';
setTimeout(() => {
// receiving data from hardware
BleManager.startNotification(peripheral.id, service, ReadCharacteristic).then(() => {
console.log('Started notification on ' + peripheral.id);
setTimeout(() => {
// sending data to the hardware
BleManager.write(peripheral.id, service, WriteCharacteristic, [1,95]).then(() => {
console.log('Writed NORMAL crust');
});
}, 500);
}).catch((error) => {
console.log('Notification error', error);
});首先面临一些特征未被发现的问题。在进行了一些更改之后,我得到了如下错误:
Write error Status - 3我找不到任何解决这个错误的方法。提前谢谢你
发布于 2021-05-10 14:44:44
使用BleManager.write时,应首先准备数据
数据准备:
如果你的数据不是字节数组格式,你应该先转换它。对于字符串,您可以使用convert-string或其他npm包来实现此目的。首先安装程序包:
npm install convert-string然后在你的应用程序中使用它:
// Import/require in the beginning of the file
import { stringToBytes } from "convert-string";
// Convert data to byte array before write/writeWithoutResponse
const data = stringToBytes(yourStringData);在我的例子中,我将我的数据转换为字节格式,这样你也可以做同样的事情。
const dataByte = convertString.UTF8.stringToBytes(data);然后在我的代码中使用
BleManager.retrieveServices(currentDevice.PID).then((peripheralInfo) => {
console.log(peripheralInfo);
BleManager.write(
currentDevice.PID,
'0000ffe0-0000-1000-8000-00805f9b34fb',
'0000ffe1-0000-1000-8000-00805f9b34fb',
dataByte,
)
.then(() => {
console.log(`Sent ${dataByte}`);
})
.catch((error) => {
console.log(error);
});
});
};https://stackoverflow.com/questions/62130904
复制相似问题