emitCurrentPermissionStatus static method

Stream<BluetoothPermissionStatus> emitCurrentPermissionStatus(
  1. MethodChannel channel
)

Emits the current Bluetooth permission status to the Dart side.

This method communicates with the native platform code to obtain the current Bluetooth permission status and emits it to any listeners on the Dart side.

Listeners on the Dart side will receive one of the following enum values from BluetoothPermissionStatus:

  • BluetoothPermissionStatus.GRANTED: Indicates that Bluetooth permission is granted.
  • BluetoothPermissionStatus.DENIED: Indicates that Bluetooth permission is denied.

Returns a Stream of BluetoothPermissionStatus values representing the current Bluetooth permission status on the device.

Implementation

static Stream<BluetoothPermissionStatus> emitCurrentPermissionStatus(
  MethodChannel channel,
) {
  final StreamController<BluetoothPermissionStatus> streamController =
      StreamController<BluetoothPermissionStatus>.broadcast();

  channel
    ..setMethodCallHandler((MethodCall call) async {
      if (call.method == 'permissionStatusUpdated') {
        final String permissionStatusString = call.arguments as String;

        // Convert the string status to its corresponding enum value
        final BluetoothPermissionStatus status =
            BluetoothPermissionStatus.values.firstWhere(
          (status) => status.identifier == permissionStatusString,
        );

        streamController.add(status);
      }
    })

    // Begin emitting Bluetooth permission status updates from the platform side.
    ..invokeMethod('emitCurrentPermissionStatus');

  return streamController.stream;
}