如何解决无法在 Flutter 中使用 flutter_blue 读取多个服务的数据
我在我的 Flutter 应用程序中使用 Flutter_blue 包,但问题是我无法读取第二个服务的数据。当我获得第一服务的特征值时。
我从 BASIC 服务 UUID 获取数据,但不是从 PV 服务 UUID。
但是如果我睡了大约 20 到 30 秒,那么它就起作用了。
分享代码:
discoverServices() async {
List<BluetoothService> services =await widget.device.discoverServices();
EasyLoading.show(status: 'Fetching data from device...');
Future.delayed(Duration(seconds: 7),() async {
Logger().d("Sensor Screen: Waiting for services to be read");
for(BluetoothService service in services){
String formattedServiceUUID =
"0x${service.uuid.toString().toupperCase().substring(4,8)}";
if(formattedServiceUUID==ServicesAndcharacteristics.PV_SERVICES_UUID){
Logger().d("Sensor Screen: PV_SERVICES_UUID ==FOUND==");
for(BluetoothCharacteristic characteristic in service.characteristics){
String formattedCharacteristic = "0x${characteristic.uuid.toString().toupperCase().substring(4,8)}";
if (formattedCharacteristic == ServicesAndcharacteristics.CHAR_2A11) {
if(characteristic.isnotifying==false){
await characteristic.setNotifyValue(true);
}
await characteristic.read();
var value=characteristic.value;
Logger().d("Sensor Screen: Value of VV1 value is-"+value.toString());
CHAR_2A11_STREAM = characteristic.value;
setState(() {
if(isReady==false){
isReady = true;
EasyLoading.dismiss();
}
});
}
}
}
if(formattedServiceUUID==ServicesAndcharacteristics.BASIC_SERVICES_UUID){
Logger().d("Sensor Screen: CHAT_SERVICE_UUID ==FOUND==");
for(BluetoothCharacteristic characteristic in service.characteristics){
String formattedCharacteristic = "0x${characteristic.uuid.toString().toupperCase().substring(4,8)}";
Logger().d("Sensor Screen: CHAR FOUND-> "+formattedCharacteristic);
/* if (formattedCharacteristic == BATTERY_CHAR_UUID) {
Logger().d("Sensor Screen: BATTERY_CHAR_UUID ==FOUND==");
if(characteristic.properties.notify==true){
if(characteristic.isnotifying==false){
await characteristic.setNotifyValue(true);
}
}
await characteristic.read();
CHAR_2A02_STREAM = characteristic.value;
setState(() {
if(isReady==false){
isReady = true;
EasyLoading.dismiss();
}
});
}
if (formattedCharacteristic == CHAT_CHAR_UUID) {
Logger().d("Sensor Screen: CHAT_CHAR_UUID ==FOUND==");
if(characteristic.properties.notify==true){
if(characteristic.isnotifying==false){
await characteristic.setNotifyValue(true);
}
}
await characteristic.read();
CHAR_2A0A_STREAM = characteristic.value;
setState(() {
if(isReady==false){
isReady = true;
EasyLoading.dismiss();
}
});
}*/
if (formattedCharacteristic == ServicesAndcharacteristics.CHAR_2A03) {
if(characteristic.properties.notify==true){
Logger().d("Sensor Screen: Notify property FOUND For -> "+formattedCharacteristic);
if(characteristic.isnotifying==false){
await characteristic.setNotifyValue(true);
}
}
await characteristic.read();
CHAR_2A03_STREAM = characteristic.value;
setState(() {
if(isReady==false){
isReady = true;
EasyLoading.dismiss();
}
});
}
if (formattedCharacteristic == ServicesAndcharacteristics.CHAR_2A0A) {
if(characteristic.properties.notify==true){
Logger().d("Sensor Screen: Notify property FOUND For -> "+formattedCharacteristic);
if(characteristic.isnotifying==false){
await characteristic.setNotifyValue(true);
}
}
await characteristic.read();
CHAR_2A0A_STREAM = characteristic.value;
Logger().d("Sensor Screen: Now CHANGE STATE OF SCREEN");
setState(() {
if(isReady==false){
isReady = true;
EasyLoading.dismiss();
}
});
}
if (formattedCharacteristic == ServicesAndcharacteristics.CHAR_2A04) {
if(characteristic.properties.notify==true){
Logger().d("Sensor Screen: Notify property FOUND For -> "+formattedCharacteristic);
if(characteristic.isnotifying==false){
await characteristic.setNotifyValue(true);
}
}
await characteristic.read();
CHAR_2A04_STREAM = characteristic.value;
setState(() {
if(isReady==false){
isReady = true;
EasyLoading.dismiss();
}
});
}
if (formattedCharacteristic == ServicesAndcharacteristics.CHAR_2A02) {
if(characteristic.properties.notify==true){
Logger().d("Sensor Screen: Notify property FOUND For -> "+formattedCharacteristic);
if(characteristic.isnotifying==false){
await characteristic.setNotifyValue(true);
}
}
await characteristic.read();
CHAR_2A02_STREAM = characteristic.value;
setState(() {
if(isReady==false){
isReady = true;
EasyLoading.dismiss();
}
});
}
/*if (formattedCharacteristic == ServicesAndcharacteristics.CHAR_2A11) {
if(characteristic.isnotifying==false){
await characteristic.setNotifyValue(true);
}
await characteristic.read();
CHAR_2A11_STREAM = characteristic.value;
setState(() {
if(isReady==false){
isReady = true;
EasyLoading.dismiss();
}
});
}*/
}
}
}
});
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。