private async Task <GattCommunicationStatus> WriteValueAsync(IBuffer value) { // Send the command byte[] debugValue = value.ToArray(); try { if (GattCharacteristic.CharacteristicProperties.HasFlag(GattCharacteristicProperties.ReliableWrites)) { GattReliableWriteTransaction transaction = new GattReliableWriteTransaction(); transaction.WriteValue(GattCharacteristic, value); return(await transaction.CommitAsync()); } else { return(await GattCharacteristic.WriteValueAsync(value, GattWriteOption.WriteWithResponse)); } } catch (Exception e) { switch ((uint)e.HResult) { case 0xE04200A0: //System.Diagnostics.Debug.WriteLine("Unknown command. The commandID was not recognized by the NP."); throw new UnknownCommandException(); case 0xE04200A1: //System.Diagnostics.Debug.WriteLine("Invalid command. The command was improperly formatted."); throw new InvalidCommandException(); case 0xE04200A2: //System.Diagnostics.Debug.WriteLine("Invalid parameter. One of the parameters (for example, the NotificationUID) does not refer to an existing object on the NP."); throw new InvalidParameterException(); case 0xE04200A3: //System.Diagnostics.Debug.WriteLine("Action failed. The action was not performed."); throw new ActionFailedException(); default: System.Diagnostics.Debug.WriteLine("Failed to get notification attributes. " + e.Message); break; } throw e; } }
private async void ButtonAction4_Click(object sender, RoutedEventArgs e) { var writeableChars = new[] {"Parrot_FC1", "Parrot_D22", "Parrot_D23", "Parrot_D24", "Parrot_D52", "Parrot_D53", "Parrot_D54"}; DevicesInformation = string.Empty; foreach (var writeableChar in writeableChars) { foreach (var rsCharacteristic in _dicCharacteristics.Where(rsCharacteristic => rsCharacteristic.Value.CharName == writeableChar)) { await rsCharacteristic.Value.Characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify); DevicesInformation += $" Test write {rsCharacteristic.Value.CharName}{Environment.NewLine}"; try { var gattTransaction = new GattReliableWriteTransaction(); var charToTest = rsCharacteristic.Value.Characteristic; byte[] arr1 = { 02, 40, 20, 09, 00, 05, 00, 04, 00, 12, 0xC0, 00, 01, 00 }; var writer = new DataWriter(); writer.WriteBytes(arr1); gattTransaction.WriteValue(charToTest, writer.DetachBuffer()); DevicesInformation += $" - 1 OK{Environment.NewLine}"; byte[] arr2 = { 02, 40, 20, 09, 00, 05, 00, 04, 00, 12, 0xBD, 00, 01, 00 }; writer = new DataWriter(); writer.WriteBytes(arr2); gattTransaction.WriteValue(charToTest, writer.DetachBuffer()); DevicesInformation += $" - 2 OK{Environment.NewLine}"; byte[] arr3 = { 02, 40, 20, 09, 00, 05, 00, 04, 00, 12, 0xE4, 00, 01, 00 }; writer = new DataWriter(); writer.WriteBytes(arr3); gattTransaction.WriteValue(charToTest, writer.DetachBuffer()); DevicesInformation += $" - 3 OK{Environment.NewLine}"; byte[] arr4 = { 02, 40, 20, 09, 00, 05, 00, 04, 00, 12, 0xE7, 00, 01, 00 }; writer = new DataWriter(); writer.WriteBytes(arr4); gattTransaction.WriteValue(charToTest, writer.DetachBuffer()); DevicesInformation += $" - 4 OK{Environment.NewLine}"; byte[] arr5 = { 02, 40, 20, 09, 00, 05, 00, 04, 00, 12, 52, 16, 01, 00 }; writer = new DataWriter(); writer.WriteBytes(arr5); gattTransaction.WriteValue(charToTest, writer.DetachBuffer()); DevicesInformation += $" - 5 OK{Environment.NewLine}"; byte[] arr6 = { 02, 40, 20, 09, 00, 05, 00, 04, 00, 12, 52, 26, 01, 00 }; writer = new DataWriter(); writer.WriteBytes(arr6); gattTransaction.WriteValue(charToTest, writer.DetachBuffer()); DevicesInformation += $" - 6 OK{Environment.NewLine}"; var status = await gattTransaction.CommitAsync(); switch (status) { case GattCommunicationStatus.Success: DevicesInformation += " Writing to your device OK !"; break; case GattCommunicationStatus.Unreachable: DevicesInformation += " Writing to your device Failed !"; break; } } catch (Exception exception) { DevicesInformation += $" - ERROR {exception.Message}{Environment.NewLine}"; } } } DevicesInformation += "Done"; }
private async void ButtonAction4_Click(object sender, RoutedEventArgs e) { var writeableChars = new[] { "Parrot_FC1", "Parrot_D22", "Parrot_D23", "Parrot_D24", "Parrot_D52", "Parrot_D53", "Parrot_D54" }; DevicesInformation = string.Empty; foreach (var writeableChar in writeableChars) { foreach (var rsCharacteristic in _dicCharacteristics.Where(rsCharacteristic => rsCharacteristic.Value.CharName == writeableChar)) { await rsCharacteristic.Value.Characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify); DevicesInformation += $" Test write {rsCharacteristic.Value.CharName}{Environment.NewLine}"; try { var gattTransaction = new GattReliableWriteTransaction(); var charToTest = rsCharacteristic.Value.Characteristic; byte[] arr1 = { 02, 40, 20, 09, 00, 05, 00, 04, 00, 12, 0xC0, 00, 01, 00 }; var writer = new DataWriter(); writer.WriteBytes(arr1); gattTransaction.WriteValue(charToTest, writer.DetachBuffer()); DevicesInformation += $" - 1 OK{Environment.NewLine}"; byte[] arr2 = { 02, 40, 20, 09, 00, 05, 00, 04, 00, 12, 0xBD, 00, 01, 00 }; writer = new DataWriter(); writer.WriteBytes(arr2); gattTransaction.WriteValue(charToTest, writer.DetachBuffer()); DevicesInformation += $" - 2 OK{Environment.NewLine}"; byte[] arr3 = { 02, 40, 20, 09, 00, 05, 00, 04, 00, 12, 0xE4, 00, 01, 00 }; writer = new DataWriter(); writer.WriteBytes(arr3); gattTransaction.WriteValue(charToTest, writer.DetachBuffer()); DevicesInformation += $" - 3 OK{Environment.NewLine}"; byte[] arr4 = { 02, 40, 20, 09, 00, 05, 00, 04, 00, 12, 0xE7, 00, 01, 00 }; writer = new DataWriter(); writer.WriteBytes(arr4); gattTransaction.WriteValue(charToTest, writer.DetachBuffer()); DevicesInformation += $" - 4 OK{Environment.NewLine}"; byte[] arr5 = { 02, 40, 20, 09, 00, 05, 00, 04, 00, 12, 52, 16, 01, 00 }; writer = new DataWriter(); writer.WriteBytes(arr5); gattTransaction.WriteValue(charToTest, writer.DetachBuffer()); DevicesInformation += $" - 5 OK{Environment.NewLine}"; byte[] arr6 = { 02, 40, 20, 09, 00, 05, 00, 04, 00, 12, 52, 26, 01, 00 }; writer = new DataWriter(); writer.WriteBytes(arr6); gattTransaction.WriteValue(charToTest, writer.DetachBuffer()); DevicesInformation += $" - 6 OK{Environment.NewLine}"; var status = await gattTransaction.CommitAsync(); switch (status) { case GattCommunicationStatus.Success: DevicesInformation += " Writing to your device OK !"; break; case GattCommunicationStatus.Unreachable: DevicesInformation += " Writing to your device Failed !"; break; } } catch (Exception exception) { DevicesInformation += $" - ERROR {exception.Message}{Environment.NewLine}"; } } } DevicesInformation += "Done"; }
public void WriteTransaction(GattCharacteristic characteristic, IBuffer value) { transaction.WriteValue(characteristic, value); }