安卓設(shè)備斷開后重新總是報(bào)錯(cuò)组力,iOS無此問題怔接,困擾一個(gè)月時(shí)間,光顧了各大網(wǎng)站無解,其實(shí)作者早已給出答案滨砍。
https://github.com/pauldemarco/flutter_blue/issues/525
connect() method:
void connect(BluetoothDevice device) async {
await _device.connect(autoConnect: false);
_stateSubscription = _device.state.listen((state) async {
if (state == BluetoothDeviceState.disconnected) {
await _stateSubscription.cancel();
_stateSubscription = null;
}
if (state == BluetoothDeviceState.connected) {
_mtuSubscription = _device.mtu.listen((mtu) async {
await notifyCharacteristic.setNotifyValue(true);
});
_servicesSubscription = _device.services.listen((services) async {
var service = services.first;
notifyCharacteristic = service.characteristics.firstWhere((c) => c.uuid.toString().toUpperCase() == TX_NOTIFY_CHARACTERISTIC_UUID);
writeCharacteristic = service.characteristics.firstWhere((c) => c.uuid.toString().toUpperCase() == RX_WRITE_CHARACTERISTIC_UUID);
_notifySubscription = notifyCharacteristic.value.listen((value) {
//Proceed with data...
});
_device.requestMtu(PREFERRED_MTU_SIZE);
});
_device.discoverServices();
}
});
}
disconnect() method
Future<void> disconnect() {
return _device.disconnect().then((value) => _clearResources());
}
Future<void> _clearResources() async {
await _mtuSubscription.cancel();
_mtuSubscription = null;
await _servicesSubscription.cancel();
_servicesSubscription = null;
await _notifySubscription.cancel();
_notifySubscription = null;
notifyChannel = null;
writeChannel = null;
}