writeRobotPartConfig method
Implementation
Future<void> writeRobotPartConfig({
required String partId,
required String secret,
APIKey? apiKey,
String appAddress = 'https://app.viam.com:443',
String psk = 'viamsetup',
}) async {
final bleService = await getBleService();
final cryptoCharacteristic = bleService.characteristics.firstWhere(
(char) => char.uuid.str == ViamBluetoothUUIDs.cryptoUUID,
orElse: () => throw Exception('cryptoCharacteristic not found'),
);
final publicKeyBytes = await cryptoCharacteristic.read();
final publicKey = _publicKey(Uint8List.fromList(publicKeyBytes));
final encoder = _encoder(publicKey);
final encodedPartId = encoder.process(utf8.encode('$psk:$partId'));
final partIdCharacteristic = bleService.characteristics.firstWhere(
(char) => char.uuid.str == ViamBluetoothUUIDs.robotPartUUID,
orElse: () => throw Exception('partIdCharacteristic not found'),
);
await partIdCharacteristic.write(encodedPartId);
final encodedSecret = encoder.process(utf8.encode('$psk:$secret'));
final partSecretCharacteristic = bleService.characteristics.firstWhere(
(char) => char.uuid.str == ViamBluetoothUUIDs.robotPartSecretUUID,
orElse: () => throw Exception('partSecretCharacteristic not found'),
);
await partSecretCharacteristic.write(encodedSecret);
final encodedAppAddress = encoder.process(utf8.encode('$psk:$appAddress'));
final appAddressCharacteristic = bleService.characteristics.firstWhere(
(char) => char.uuid.str == ViamBluetoothUUIDs.appAddressUUID,
orElse: () => throw Exception('appAddressCharacteristic not found'),
);
await appAddressCharacteristic.write(encodedAppAddress);
if (apiKey != null) {
final encodedApiKey = encoder.process(utf8.encode('$psk:${apiKey.toJson()}'));
final apiKeyCharacteristic = bleService.characteristics.firstWhere(
(char) => char.uuid.str == ViamBluetoothUUIDs.apiKeyCredsUUID,
orElse: () => throw Exception('apiKeyCharacteristic not found'),
);
await apiKeyCharacteristic.write(encodedApiKey);
}
}