chipweinberger / flutter_blue_plus

Flutter plugin for connecting and communicationg with Bluetooth Low Energy devices, on Android, iOS, macOS
Other
791 stars 478 forks source link

[Help]: [iOS] OnConnectionStateChanged state does not change when using Settings app #880

Closed mika324 closed 6 months ago

mika324 commented 6 months ago

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

  1. Put the app in the background
  2. Tap the connected device from the list of Bluetooth devices on your device to disconnect.
  3. While in the background, tap the device you disconnected from the list of Bluetooth devices to reconnect it.

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.πŸ™

Logs

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
chipweinberger commented 6 months ago

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