【发布时间】:2020-10-13 05:17:40
【问题描述】:
我在使用flutter blue plugin(0.5.0 版本)时遇到问题。
我的蓝牙设备只连接了一次,在我关闭 iPhone 上的蓝牙并重新打开之前,我无法再次连接。
这是一个实验视频。 https://drive.google.com/file/d/1pQW4JFQI5R_zcxF3wfozT6Ru2AY2KxBm/view?usp=sharing
我不能使用最新的插件版本,因为项目太旧了。 此问题仅在 ios 中发生。
这是我的连接代码:
_connect(BluetoothDevice d) async {
device = d;
// Connect to device
deviceConnection = _flutterBlue
.connect(device, timeout: const Duration(seconds: 4))
.listen(
null,
onDone: _disconnect,
);
// Update the connection state immediately
device.state.then((s) {
setState(() {
deviceState = s;
});
});
// Subscribe to connection changes
deviceStateSubscription = device.onStateChanged().listen((s) {
if (mounted)
setState(() {
deviceState = s;
});
if (s == BluetoothDeviceState.connected) {
device.discoverServices().then((s) {
var bservice = s
.where((r) =>
r.uuid
.toString()
.toLowerCase()
.contains("0000ffe0-0000-1000-8000-00805f9b34fb"))
.first;
if (bservice != null) {
var char = bservice.characteristics
.where((c) =>
c.uuid
.toString()
.toLowerCase()
.contains("0000ffe4-0000-1000-8000-00805f9b34fb"))
.first;
if (char != null) {
_setNotification(char);
}
}
bservice = s
.where((r) =>
r.uuid
.toString()
.toLowerCase()
.contains("00001808-0000-1000-8000-00805f9b34fb"))
.first;
if (bservice != null) {
var char = bservice.characteristics
.where((c) =>
c.uuid
.toString()
.toLowerCase()
.contains("00002a18-0000-1000-8000-00805f9b34fb"))
.first;
if (char != null) {
_setNotification(char);
}
}
setState(() {
//Accu-Answer isaw Service UUID="0000ffe0-0000-1000-8000-00805f9b34fb"
//Accu-Answer isaw Characteristics UUID="0000ffe4-0000-1000-8000-00805f9b34fb"
//YASEE Service UUID="00001808-0000-1000-8000-00805f9b34fb"
//YASEE Characteristics UUID="00002a18-0000-1000-8000-00805f9b34fb"
// var bservice=s.where((r) =>r.uuid.toString().toLowerCase().contains("0000ffe0-0000-1000-8000-00805f9b34fb")).first;//Accu-Answer
// var bservice=s.where((r) =>r.uuid.toString().toLowerCase().contains("00001808-0000-1000-8000-00805f9b34fb")).first;//YASEE
for (int i = 0; i < s.length; i++) {
var bservice = s[i];
var tmpUUID = bservice.uuid.toString().toLowerCase();
if (tmpUUID == "0000ffe0-0000-1000-8000-00805f9b34fb") {
var char = bservice.characteristics
.where((c) =>
c.uuid
.toString()
.toLowerCase()
.contains("0000ffe4-0000-1000-8000-00805f9b34fb"))
.first;
if (char != null) {
_setNotification(char);
}
break;
} else if (tmpUUID == "00001808-0000-1000-8000-00805f9b34fb") {
var char = bservice.characteristics
.where((c) =>
c.uuid
.toString()
.toLowerCase()
.contains("00002a18-0000-1000-8000-00805f9b34fb"))
.first;
if (char != null) {
_setNotification(char);
}
break;
}
}
//services = s;
});
});
}
});}
我正在测试iPhone 6 plus ios 版本12.4.7
【问题讨论】:
-
究竟是什么不起作用?我不知道如何帮助你。
标签: ios objective-c swift flutter bluetooth