enableServices static method

Future<BrilliantDevice> enableServices(
  1. BluetoothDevice device
)

Implementation

static Future<BrilliantDevice> enableServices(BluetoothDevice device) async {
  if (Platform.isAndroid) {
    await device.requestMtu(512);
  }

  BrilliantDevice finalDevice = BrilliantDevice(
    device: device,
    state: BrilliantConnectionState.disconnected,
  );

  List<BluetoothService> services = await device.discoverServices();

  for (var service in services) {
    // If Frame
    if (service.serviceUuid == Guid('7a230001-5475-a6a4-654c-8431f6ad49c4')) {
      _log.fine("Found Frame service");
      for (var characteristic in service.characteristics) {
        if (characteristic.characteristicUuid ==
            Guid('7a230002-5475-a6a4-654c-8431f6ad49c4')) {
          _log.fine("Found Frame TX characteristic");
          finalDevice.txChannel = characteristic;
        }
        if (characteristic.characteristicUuid ==
            Guid('7a230003-5475-a6a4-654c-8431f6ad49c4')) {
          _log.fine("Found Frame RX characteristic");
          finalDevice.rxChannel = characteristic;

          await characteristic.setNotifyValue(true);
          _log.fine("Enabled RX notifications");

          finalDevice.maxStringLength = device.mtuNow - 3;
          finalDevice.maxDataLength = device.mtuNow - 4;
          _log.fine(() => "Max string length: ${finalDevice.maxStringLength}");
          _log.fine(() => "Max data length: ${finalDevice.maxDataLength}");
        }
      }
    }
  }

  // TODO ugly hack: need to work out what to await here to ensure the Frame is ready
  // Don't let BrilliantBluetooth.connect complete until the Frame is ready
  await Future.delayed(const Duration(milliseconds: 100));

  if (finalDevice.txChannel != null && finalDevice.rxChannel != null) {
    finalDevice.state = BrilliantConnectionState.connected;
    return finalDevice;
  }

  throw ("Incomplete set of services found");
}