Closed mika324 closed 6 months ago
No
1.32.2
3.19.5
iOS
15.5
OPPO Enco Free2, AirBuds U
When connecting from the device list on the device, the onConnectionStateChanged state does not change.
The steps are as follows: 1.Start the app 2.BLE connection
I tried this, but the following code cannot detect when it is connected. Can be detected when disconnected.
What we want to achieve is to detect when a device is connected to Bluetooth even when the app is in the background and reconnect.
final PERIPHERAL_DEVICE_INFO = { "id": Platform.isIOS ? "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" : "XX:XX:XX:XX:XX:XX", } class _ScanScreenState extends State<ScanScreen> with WidgetsBindingObserver { List<BluetoothDevice> _systemDevices = []; List<ScanResult> _scanResults = []; bool _isScanning = false; late StreamSubscription<List<ScanResult>> _scanResultsSubscription; Timer? _scanTimer; @override void initState() { super.initState(); _scanResultsSubscription = FlutterBluePlus.scanResults.listen((List<ScanResult> results) async { bool found = results.isNotEmpty && results.any((result) => result.device.remoteId.toString() == PERIPHERAL_DEVICE_INFO['id'].toString()); if (found) { // connect device } // Not detected when connecting FlutterBluePlus.events.onConnectionStateChanged.listen((state) { print('onConnectionStateChanged events: ${state.connectionState}'); }); scanDevice(); } void scanDevice() async { FlutterBluePlus.stopScan(); isIOS ? FlutterBluePlus.startScan( withRemoteIds: [PERIPHERAL_DEVICE_INFO['id'] as String], timeout: const Duration(milliseconds: 1000), ) : FlutterBluePlus.startScan( withRemoteIds: [PERIPHERAL_DEVICE_INFO['id'] as String], timeout: const Duration(milliseconds: 1000), ); } }
I would like someone who knows how to solve this problem to tell me. Thank you for your time.
I'm Japanese, so I apologize if there are any mistakes in the English translation.π
2024-05-13 15:30:35.864859+0900 Runner[84848:3760937] [FBP-iOS] didDisconnectPeripheral: 2024-05-13 15:30:35.866376+0900 Runner[84848:3761183] flutter: [FBP] \^[[1;30m[[ OnConnectionStateChanged ]]\^[[0m result: \^[[1;33m{remote_id: D89BE109-8326-B20F-0CDF-BFDC698019AA, connection_state: 0, disconnect_reason_code: 23789258, disconnect_reason_string: connection cancele<β¦> 2024-05-13 15:30:36.964190+0900 Runner[84848:3761183] flutter: onConnectionStateChanged!: BluetoothDevice{remoteId: D89BE109-8326-B20F-0CDF-BFDC698019AA, platformName: OPPO Enco Free2, services: Instance of 'BmDiscoverServicesResult'} BluetoothConnectionState.disconnected 2024-05-13 15:30:36.964689+0900 Runner[84848:3761183] flutter: γγγ€γΉγεζγγγΎγγ: BluetoothDevice{remoteId: D89BE109-8326-B20F-0CDF-BFDC698019AA, platformName: OPPO Enco Free2, services: Instance of 'BmDiscoverServicesResult'} 2024-05-13 15:30:36.965460+0900 Runner[84848:3761183] flutter: onConnectionStateChanged events: BluetoothConnectionState.disconnected
OnConnectionStateChanged is only for connections to your app.
If you want to monitor connections to other apps or the system, you need to periodically check systemDevices
systemDevices
Requirements
Have you checked this problem on the example app?
No
FlutterBluePlus Version
1.32.2
Flutter Version
3.19.5
What OS?
iOS
OS Version
15.5
Bluetooth Module
OPPO Enco Free2, AirBuds U
What is your problem?
When connecting from the device list on the device, the onConnectionStateChanged state does not change.
The steps are as follows: 1.Start the app 2.BLE connection
I tried this, but the following code cannot detect when it is connected. Can be detected when disconnected.
What we want to achieve is to detect when a device is connected to Bluetooth even when the app is in the background and reconnect.
I would like someone who knows how to solve this problem to tell me. Thank you for your time.
I'm Japanese, so I apologize if there are any mistakes in the English translation.π
Logs