getDevices static method

Future<List<WebBluetoothDevice>> getDevices()

Get a list back of already paired devices. A device becomes paired once a user clicks on it in the pair menu and the web app also connects to the device. If only a user pairs a device, but not connection attempt is made then it won't be marked as paired.

No browser currently supports this without needing a browser flag. https://caniuse.com/mdn-api_bluetooth_getdevices

Will return an empty list if hasGetDevices returns false. See hasGetDevices.

Implementation

static Future<List<WebBluetoothDevice>> getDevices() async {
  if (!hasGetDevices()) {
    return [];
  }
  final promise = _nativeBluetooth.getDevices();
  final result = await _JSUtil.promiseToFuture(promise);
  if (result is List) {
    final items = <WebBluetoothDevice>[];
    for (final item in result) {
      try {
        items.add(WebBluetoothDevice.fromJSObject(item));
      } catch (e, stack) {
        if (e is UnsupportedError) {
          webBluetoothLogger.severe(
              "Could not convert known device to BluetoothDevice.", e, stack);
        } else {
          rethrow;
        }
      }
    }
    return items;
  }
  return [];
}