[英]react native ble manager BleManager.write function doesn't respond as expected
const infoConnected = () => {//Show the connected devices
BleManager.getConnectedPeripherals([]).then((results) => {
if (results.length == 0) {
console.log('No connected peripherals')
}
//for (var i = 0; i < 1;/*list2.length;*/ i++) {//once for now
console.log("Connected Device Variables:");
//FULL INFO
let deviceInfo=list2[0];
console.log(deviceInfo);
//Split INFO
console.log("peripheralId INFO:");
console.log(list2[0].id);
console.log("serviceUUID INFO:");
console.log(list2[0].advertising.serviceUUIDs);
console.log("characteristicUUID INFO:");
console.log(list2[0].id);
//write and take a promis
//const data=stringToBytesToBytes('dsadsa');
//const dataByte = convertString.UTF8.stringToBytes(data);
//const data = stringToBytes(String(12345));
//const dataByte = convertString.UTF8.stringToBytes(data);
//const data = stringToBytes("XXXXXXXXXXXXXXXXXXXXXX");
let command = ['XX', '00', 'XX', '00', '00', '00','00','00','00','00','00','00','00','00','00','00','00','00', 'XX','XX'];
let data = command.map(x => {return parseInt(x, 16);});
response=BleManager.write(
"XX:24:XX:XX:37:XX",
'XX-0000-1000-8000-XX',
'XX-0000-1000-8000-XX',
data,
)
.then(() => {
console.log(`*******************************Sent ${data}`);
})
.catch((error) => {
console.log(error);
});
console.log("*******************************Response From Slave(peripheral)="+JSON.stringify(response));
});
BleManager.read(
"4C:XX:XX:XX:XX:XX",
'XX-0000-XXXX-XXXX-XX',
'XX-0000-XXXX-XXXX-XX'
)
.then((readData) => {
// Success code
console.log("*******************************Read: " + readData);
buffer = Buffer.Buffer.from(readData); //https://github.com/feross/buffer#convert-arraybuffer-to-buffer
const sensorData=buffer.readUInt8(1, true);
console.log("*******************************Buffer read:"+ buffer);
console.log("*******************************SensorData read:"+sensorData);
//const sensorData = buffer.readUInt8(1, true);
})
.catch((error) => {
// Failure code
console.log(error);
});
Output Hey, I'm new to react native, I want to use write function to communicate with my bluetooth device. Output嘿,我是新手反应原生,我想用写 function 与我的蓝牙设备通信。
The return value is;返回值为; Response From Slave(peripheral)={"_1":0,"_2":0,"_3":null,"_4":null}从机的响应(外设)={"_1":0,"_2":0,"_3":null,"_4":null}
The function above can't communicate with my bluetooth device the response shown below is wrong.上面的 function 无法与我的蓝牙设备通信,下面显示的响应是错误的。 Am I doing smt wrong.我做错了吗。 Thanks for help:) The Output感谢您的帮助:) Output
Your response is an unresolved promise.您的回复是未解决的 promise。 Your .then
block will get the response, but since you're not returning it in that block (or using it), it can't be stored or displayed.您的.then
块将获得响应,但由于您没有在该块中返回它(或使用它),因此无法存储或显示它。 Try this instead试试这个
.then(response => {
console.log(`*******************************Sent ${data}`);
console.log("*******************************Response From Slave(peripheral)="+JSON.stringify(response));
});
If you want to store the response, consider using async/await syntax;如果要存储响应,请考虑使用 async/await 语法; it's a simpler mental model.这是一个更简单的心理 model。
const response = await BleManager.write(
// ...
);
console.log(response);
You will have to mark the function this is inside as async, of course.当然,您必须将内部的 function 标记为异步。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.