enableServices static method

Future<BrilliantDevice> enableServices(
  1. BluetoothDevice device
)

Enables services on the 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 == _serviceUUID) {
      _log.fine("Found Frame service");
      for (var characteristic in service.characteristics) {
        if (characteristic.characteristicUuid == _txCharacteristicUUID) {
          _log.fine("Found Frame TX characteristic");
          finalDevice._txChannel = characteristic;
        }
        if (characteristic.characteristicUuid == _rxCharacteristicUUID) {
          _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}");
        }
      }
    }

    // If DFU
    if (service.serviceUuid == Guid('fe59')) {
      _log.fine("Found DFU service");
      for (var characteristic in service.characteristics) {
        if (characteristic.characteristicUuid ==
            Guid('8ec90001-f315-4f60-9fb8-838830daea50')) {
          _log.fine("Found DFU control characteristic");
          finalDevice._dfuControl = characteristic;
          await characteristic.setNotifyValue(true);
          _log.fine("Enabled DFU control notifications");
        }
        if (characteristic.characteristicUuid ==
            Guid('8ec90002-f315-4f60-9fb8-838830daea50')) {
          _log.fine("Found DFU packet characteristic");
          finalDevice._dfuPacket = characteristic;
        }
      }
    }
  }

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

  if (finalDevice._dfuControl != null && finalDevice._dfuPacket != null) {
    finalDevice.state = BrilliantConnectionState.dfuConnected;
    return finalDevice;
  }

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