authenticateDevice method

  1. @override
Future<VtjCommandResult<bool>> authenticateDevice(
  1. BluetoothDevice device,
  2. String deviceKey
)
override

Implementation

@override
Future<VtjCommandResult<bool>> authenticateDevice(
  BluetoothDevice device,
  String deviceKey,
) async {
  final challengeResult = await _executeCommand<Uint8List>(
    device: device,
    createCommand: () => AuthChallengeCommand(),
  );

  return challengeResult.when(
    success: (challenge) async {
      List<int> deviceKeyBinData = List<int>.generate(16, (index) {
        int startIndex = index * 2;
        return int.parse(
          deviceKey.substring(startIndex, startIndex + 2),
          radix: 16,
        );
      });

      final input = Uint8List.fromList([...challenge, ...deviceKeyBinData]);
      final hash = sha256.convert(input);
      final authKey = Uint8List.fromList(hash.bytes.sublist(0, 16));

      final authResult = await _executeCommand<bool>(
        device: device,
        createCommand: () => PerformAuthCommand(authKey),
      );

      _isAuthenticated = authResult.value;

      return authResult;
    },
    failure: (error, code) => VtjCommandResult.failure(error, code),
  );
}