问题描述
现在我有一个蓝牙模块,它会在收到每条消息时发回响应 所以如果我写到特征,我需要能够在读取订阅完成的响应之前等待。
我试过了:
subScribetocharacteristic(characteristic);
writeCharacterisiticWithoutResponse(characteristic,[1]);
readValuefromSubscribedCharacteristic();
但是我过早地读取了值(在模块响应之前)
已编辑:
这是我目前所拥有的,我该如何正确地做到这一点?
//this is a snippet from a dialog prompting user for password that pops up after onTap of listTile of a ble device list
//userData = user input password of this prompt
//this is onpressed of that ok button for the password prompt dialog
//creates a buffer variable
List receiveData = [];
onpressed: () async {
//define characteristic
final characteristic = QualifiedCharacteristic(characteristicId:characteristicId,serviceId: serviceId,deviceid: id);
//subscribe to characteristic
await ble.subscribetocharacteristic(characteristic).listen((event) {
//if seq of data starts with byte 5550 its a match
if (event[0] == 5 && event[1] == 5 && event[2] == 5 && event[3] == 0) {
print('New incoming data,inserting data to buffer');
receiveData = new Uint8List.fromList(event);
}
print(event);
print(receiveData);
});
//send the unlock command with password
await writeCharacteristicWithoutResponse(charateristic,userData);
//check receiveData - result is something wrong with data if receiveData is []
if (receiveData == Null) {
//data is empty,so means there isnt something yet in the buffer
print('Something wrong with data,it might be corrupt/loss packet');
} else {
// the module should return 1 at array[8] if module is unlocked
if (receiveData[8] == 1) {
//if its 1,module is unlocked and go back to prevIoUs page
print('unlocked!!');
Navigator.of(context).pop(userData);
} else {
//if its not,module is locked and reprompt the showDialog
print('unlocked');
}
}
//clear receiveData afer read from it
globals.receiveData = [0];
}
解决方法
README 文件的使用部分描述了正确的过程,如下所示:
final characteristic = QualifiedCharacteristic(serviceId: serviceUuid,characteristicId: characteristicUuid,deviceId: foundDeviceId);
flutterReactiveBle.subscribeToCharacteristic(characteristic).listen((data) {
// code to handle incoming data
},onError: (dynamic error) {
// code to handle errors
});
编辑:
您应该将有关接收到的数据的所有内容都移动到 listen 方法中:
//this is a snippet from a dialog prompting user for password that pops up after onTap of listTile of a ble device list
//userData = user input password of this prompt
//this is onPressed of that ok button for the password prompt dialog
//creates a buffer variable
List receiveData = [];
onPressed: () async {
//define characteristic
final characteristic = QualifiedCharacteristic(characteristicId:characteristicId,serviceId: serviceId,deviceId: id);
//subscribe to characteristic
await ble.subscribeToCharacteristic(characteristic).listen((event) {
//if seq of data starts with byte 5550 its a match
if (event[0] == 5 && event[1] == 5 && event[2] == 5 && event[3] == 0) {
print('New incoming data,inserting data to buffer');
receiveData = new Uint8List.fromList(event);
}
print(event);
print(receiveData);
//check receiveData - result is something wrong with data if receiveData is []
if (receiveData == Null) {
//data is empty,so means there isnt something yet in the buffer
print('Something wrong with data,it might be corrupt/loss packet');
} else {
// the module should return 1 at array[8] if module is unlocked
if (receiveData[8] == 1) {
//if its 1,module is unlocked and go back to previous page
print('unlocked!!');
Navigator.of(context).pop(userData);
} else {
//if its not,module is locked and reprompt the showDialog
print('unlocked');
}
}
//clear receiveData afer read from it
globals.receiveData = [0];
});
//send the unlock command with password
await writeCharacteristicWithoutResponse(charateristic,userData);
}