getConnectedDevices method

  1. @override
Future<List<ConnectedBleDevice>> getConnectedDevices(
  1. List<String> serviceUUIDs
)
override

Gets a list of identifiers for all connected devices.

This method communicates with the native platform code to obtain a list of all connected devices. It returns a list of device identifiers as strings.

On iOS, the identifiers returned by this method are the UUIDs of the connected peripherals. This means that the identifiers are specific to the iOS device on which this method is called. The same Bluetooth device will be associated with different identifiers on different iOS devices. Therefore, it may be necessary for the Flutter side to maintain a mapping between the device identifiers and the device addresses, or other identifiers, if cross-device consistency is required.

On Android, the process is simpler because this method will return a list of BDA (Bluetooth Device Address) strings, which are unique identifiers for each connected device. These identifiers are consistent across devices.

Returns a Future containing a list of ConnectedBleDevice objects representing Bluetooth devices.

Implementation

@override
Future<List<ConnectedBleDevice>> getConnectedDevices(List<String> serviceUUIDs) async {
  try {
    // Prepare the arguments with the list of UUIDs
    final Map<String, dynamic> arguments = {
      'serviceUUIDs': serviceUUIDs,
    };

    // Call the platform-specific code via Method Channel
    final List<dynamic> connectedDevices =
        await channel.invokeMethod('getConnectedDevices', arguments) as List<dynamic>;

    // Convert the dynamic list to a strongly-typed list of maps
    final List<ConnectedBleDevice> devices = connectedDevices.map((dynamic device) {
      final Map<dynamic, dynamic> deviceMap = device as Map<dynamic, dynamic>;
      return ConnectedBleDevice.fromMap(deviceMap);
    }).toList();

    return devices;
  } on PlatformException catch (e) {
    throw Exception('Failed to get connected devices: ${e.message}');
  }
}