public async Task RegisterForTemperatureChanges(BluetoothLEDevice bluetoothLeDevice) { var service = await bluetoothLeDevice.GetGattServiceForUuidAsync(serviceGuid); var accessStatus = await service.RequestAccessAsync(); var openStatus = await service.OpenAsync(GattSharingMode.Exclusive); foreach (var characteristics in await service.GetCharacteristics2Async()) { if (!Probes.Contains(characteristics.Uuid)) { continue; } Debug.WriteLine("Registering probe " + characteristics.Uuid); await characteristics.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify); var byteArray = await characteristics.ReadBytesAsync(); TemperatureChanged?.Invoke(this, new TemperatureChangedEventArg( Probes.IndexOf(characteristics.Uuid), CalculateTemperature(byteArray))); characteristics.ValueChanged += (GattCharacteristic sender, GattValueChangedEventArgs args) => { var temperature = ReadTemperature(args.CharacteristicValue); TemperatureChanged?.Invoke(this, new TemperatureChangedEventArg( Probes.IndexOf(characteristics.Uuid), CalculateTemperature(temperature))); }; } }
private async Task ListServices() { var service = await bluetoothLeDevice.GetGattServiceForUuidAsync(SERVICE_UUID); foreach (var characteristics in await service.GetCharacteristics2Async()) { if (readCharacteristics.ContainsKey(characteristics.Uuid)) { Debug.WriteLine("Registering characteristics Uuid=" + characteristics.Uuid); characteristics.ValueChanged += (GattCharacteristic sender, GattValueChangedEventArgs args) => { var eventHandler = readCharacteristics.GetValueOrDefault(characteristics.Uuid); var data = ReadData(args); Debug.WriteLine(String.Format("{2}: Received data from characteristic {0} data: {1}", characteristics.Uuid, data.ToByteString(), DateTime.Now)); lock (syncObj) { eventHandler?.Invoke(this, data); } }; await characteristics.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify); } } var accessStatus = await service.RequestAccessAsync(); var openStatus = await service.OpenAsync(GattSharingMode.Exclusive); }
public async Task ConnectAsync(string id) { Debug.WriteLine("Connecting to " + id); bluetoothLeDevice = await BluetoothLEDevice.FromIdAsync(id); if (bluetoothLeDevice == null) { throw new Exception("AquaClean device not found"); } Debug.WriteLine("Connected"); bluetoothLeDevice.ConnectionStatusChanged += async(BluetoothLEDevice device, object obj) => { Debug.WriteLine("Connection status changed to: " + device.ConnectionStatus); ConnectionStatusChanged?.Invoke(this, device.ConnectionStatus == BluetoothConnectionStatus.Connected); }; // Register read characteristics readCharacteristics.Add(BULK_CHAR_BULK_READ_0_UUID_STRING, DataReceived); readCharacteristics.Add(BULK_CHAR_BULK_READ_1_UUID_STRING, DataReceived); readCharacteristics.Add(BULK_CHAR_BULK_READ_2_UUID_STRING, DataReceived); readCharacteristics.Add(BULK_CHAR_BULK_READ_3_UUID_STRING, DataReceived); // Load write characteristic var service = await bluetoothLeDevice.GetGattServiceForUuidAsync(SERVICE_UUID); var characteristicsResult = await service.GetCharacteristicsAsync(); var characteristics = characteristicsResult.Characteristics; writeCharacteristic = characteristics.First((c) => c.Uuid == BULK_CHAR_BULK_WRITE_0_UUID_STRING); await ListServices(); }
public async Task <string> GetFirmwareVersionAsync(BluetoothLEDevice bluetoothLeDevice) { var service = await bluetoothLeDevice.GetGattServiceForUuidAsync(DEVICE_SERVICE_GUID); var characteristics = await service.GetCharacteristicForUuid2Async(FIRMWARE_CHARACTERISTIC_GUID); return(await characteristics.ReadStringAsync()); }
public async Task <string> GetAppearanceNameAsync(BluetoothLEDevice bluetoothLeDevice) { var service = await bluetoothLeDevice.GetGattServiceForUuidAsync(GENERIC_SERVICE_GUID); var characteristics = await service.GetCharacteristicForHandleAsync(23); return(await characteristics.ReadStringAsync()); }
public async Task Authenticate(BluetoothLEDevice bluetoothLeDevice) { Debug.WriteLine("Start authentication with iGrill"); // Gett Service var service = await bluetoothLeDevice.GetGattServiceForUuidAsync(DEVICE_SERVICE_GUID); var characteristics = await service.GetCharacteristics2Async(); var challengeCharacteristic = characteristics.First((c) => c.Uuid == APP_CHALLENGE_GUID); var deviceChallengeCharacteristig = characteristics.First((c) => c.Uuid == DEVICE_CHALLENGE_GUID); var responseCharacterisitc = characteristics.First((c) => c.Uuid == DEVICE_RESPONSE_GUID); var encryptionKey = GetEncryptionKey(iGrillVersion); Debug.WriteLine("Send challenge to iGrill"); var challenge = new byte[16]; Array.Copy(Enumerable.Range(0, 8).Select(n => (byte)rnd.Next(0, 255)).ToArray(), challenge, 8); await challengeCharacteristic.WriteBytesAsync(challenge); // read device challenge Debug.WriteLine("Read encrypted challenge from iGrill"); byte[] encrypted_device_challenge = await deviceChallengeCharacteristig.ReadBytesAsync(); var device_challenge = Encryption.Decrypt(encrypted_device_challenge, encryptionKey); // verify device challenge Debug.WriteLine("Comparing challenges..."); Enumerable.Range(0, 8).ToList().ForEach(i => { if (challenge[i] != device_challenge[i]) { throw new Exception("Invalid device challange"); } }); // send device response Debug.WriteLine("Send encrypted response to iGrill"); var device_response = new byte[16]; Array.Copy(device_challenge, 8, device_response, 8, 8); var encrypted_device_response = Encryption.Encrypt(device_response, encryptionKey); await responseCharacterisitc.WriteBytesAsync(encrypted_device_response); // Authenticated Debug.WriteLine("Authentication with iGrill successful"); }
public async Task RegisterForBatteryChanges(BluetoothLEDevice bluetoothLeDevice) { var services = await bluetoothLeDevice.GetGattServiceForUuidAsync(BATTERY_SERVICE_GUID); var characteristics = await services.GetCharacteristicForUuid2Async(BATTERY_CHARACTERISTIC); var bytes = await characteristics.ReadBytesAsync(); BatteryLevelChanged?.Invoke(this, bytes[0]); characteristics.ValueChanged += (GattCharacteristic sender, GattValueChangedEventArgs args) => { var reader = DataReader.FromBuffer(args.CharacteristicValue); reader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8; reader.ByteOrder = ByteOrder.LittleEndian; var byteArray = new byte[reader.UnconsumedBufferLength]; reader.ReadBytes(byteArray); Debug.WriteLine("Battery Level: " + byteArray[0]); BatteryLevelChanged?.Invoke(this, byteArray[0]); }; }