public static bool HasNotify(this Characteristic ch) => ch.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Indicate) || ch.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Notify);
private void Characteristic_ValueChanged(Uap.GattCharacteristic sender, Uap.GattValueChangedEventArgs args) { OnCharacteristicValueChanged(new GattCharacteristicValueChangedEventArgs(args.CharacteristicValue.ToArray())); }
private void Characteristic_ValueChanged(Uap.GattCharacteristic sender, Uap.GattValueChangedEventArgs args) { OnCharacteristicValueChanged(); }
internal GattCharacteristic(BluetoothRemoteGATTService service, Uap.GattCharacteristic characteristic) : this(service) { _characteristic = characteristic; }
internal GattCharacteristic(GattService service, Uap.GattCharacteristic characteristic) : this(service) { _characteristic = characteristic; }
private void SelectedCharacteristic_ValueChanged(Windows.Devices.Bluetooth.GenericAttributeProfile.GattCharacteristic sender, Windows.Devices.Bluetooth.GenericAttributeProfile.GattValueChangedEventArgs args) { if (eventData.Length != args.CharacteristicValue.Length) { eventData = new byte[args.CharacteristicValue.Length]; } Windows.Storage.Streams.DataReader.FromBuffer(args.CharacteristicValue).ReadBytes(eventData); val1 = val2 = val3 = 0; int bit = 0; //val1 += (eventData[48] & (1 << 7)) != 0 ? (1 << 0) : 0; //val1 += (eventData[49] & (1 << 0)) != 0 ? (1 << 1) : 0; //val1 += (eventData[49] & (1 << 1)) != 0 ? (1 << 2) : 0; //val1 += (eventData[49] & (1 << 3)) != 0 ? (1 << bit++) : 0; val1 += (eventData[49] & (1 << 3)) != 0 ? (1 << bit++) : 0; bit = 0; //val2 += (eventData[51] & (1 << 0)) != 0 ? (1 << 0) : 0; //val2 += (eventData[51] & (1 << 1)) != 0 ? (1 << 1) : 0; //val2 += (eventData[51] & (1 << 2)) != 0 ? (1 << 2) : 0; //val2 += (eventData[51] & (1 << 3)) != 0 ? (1 << 3) : 0; val2 += (eventData[51] & (1 << 4)) != 0 ? (1 << bit++) : 0; bit = 0; //val3 += (eventData[52] & (1 << 5)) != 0 ? (1 << 0) : 0; //val3 += (eventData[52] & (1 << 6)) != 0 ? (1 << 1) : 0; //val3 += (eventData[52] & (1 << 7)) != 0 ? (1 << 2) : 0; //val3 += (eventData[53] & (1 << 0)) != 0 ? (1 << 3) : 0; val3 += (eventData[52] & (1 << 6)) != 0 ? (1 << bit++) : 0; val3 += (eventData[52] & (1 << 7)) != 0 ? (1 << bit++) : 0; val3 += (eventData[53] & (1 << 0)) != 0 ? (1 << bit++) : 0; val3 += (eventData[53] & (1 << 1)) != 0 ? (1 << bit++) : 0; val3 += (eventData[53] & (1 << 2)) != 0 ? (1 << bit++) : 0; axisX = axisY = 0; axisY += (eventData[55] & (1 << 3)) != 0 ? (1 << 0) : 0; axisY += (eventData[55] & (1 << 4)) != 0 ? (1 << 1) : 0; axisY += (eventData[55] & (1 << 5)) != 0 ? (1 << 2) : 0; axisY += (eventData[55] & (1 << 6)) != 0 ? (1 << 3) : 0; axisY += (eventData[55] & (1 << 7)) != 0 ? (1 << 4) : 0; axisY += (eventData[54] & (1 << 0)) != 0 ? (1 << 5) : 0; axisY += (eventData[54] & (1 << 1)) != 0 ? (1 << 6) : 0; axisY += (eventData[54] & (1 << 2)) != 0 ? (1 << 7) : 0; axisX += (eventData[55] & (1 << 0)) != 0 ? (1 << 7) : 0; axisX += (eventData[56] & (1 << 7)) != 0 ? (1 << 6) : 0; axisX += (eventData[56] & (1 << 6)) != 0 ? (1 << 5) : 0; axisX += (eventData[56] & (1 << 5)) != 0 ? (1 << 4) : 0; axisX += (eventData[56] & (1 << 4)) != 0 ? (1 << 3) : 0; axisX += (eventData[56] & (1 << 3)) != 0 ? (1 << 2) : 0; axisX += (eventData[56] & (1 << 2)) != 0 ? (1 << 1) : 0; axisX += (eventData[56] & (1 << 1)) != 0 ? (1 << 0) : 0; BitArray bits = new BitArray(eventData); for (int i = 0; i < bits.Count; i++) { eventBits[i] = bits[i] ? 1 : 0; if (bits[i]) { if (eventAnalysis[i] < 0) { eventAnalysis[i] = 0; } else { eventAnalysis[i] = Math.Min(eventAnalysis[i] + 1, eventAnalysisThr); } } else { if (eventAnalysis[i] > 0) { eventAnalysis[i] = 0; } else { eventAnalysis[i] = Math.Max(eventAnalysis[i] - 1, -eventAnalysisThr); } } } triggerButton = 0 != (eventData[58] & (1 << 0)); homeButton = 0 != (eventData[58] & (1 << 1)); backButton = 0 != (eventData[58] & (1 << 2)); touchpadButton = 0 != (eventData[58] & (1 << 3)); volumeDownButton = 0 != (eventData[58] & (1 << 4)); volumeUpButton = 0 != (eventData[58] & (1 << 5)); touchpadPressed = axisX != 0 && axisY != 0; }
private async void Get_Characteriisics() { Guid notifyGuid = new Guid("{c8c51726-81bc-483b-a052-f7a14ea3d281}"); Guid writeGuid = new Guid("{c8c51726-81bc-483b-a052-f7a14ea3d282}"); bool success = true; var charResult = myService.GetAllCharacteristics(); //if (CharResult.Status == GattCommunicationStatus.Success) { foreach (Windows.Devices.Bluetooth.GenericAttributeProfile.GattCharacteristic c in charResult) { if ( c.CharacteristicProperties.HasFlag(Windows.Devices.Bluetooth.GenericAttributeProfile.GattCharacteristicProperties.Notify) && c.Uuid == notifyGuid) { notifyCharacteristic = c; } else if ( c.CharacteristicProperties.HasFlag(Windows.Devices.Bluetooth.GenericAttributeProfile.GattCharacteristicProperties.Write) && c.Uuid == writeGuid) { writeCharacteristic = c; } if (notifyCharacteristic != null && writeCharacteristic != null) { break; } } success = notifyCharacteristic != null && writeCharacteristic != null; if (success) { try { // Write the ClientCharacteristicConfigurationDescriptor in order for server to send notifications. var result = await notifyCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync( Windows.Devices.Bluetooth.GenericAttributeProfile.GattClientCharacteristicConfigurationDescriptorValue.Notify); if (result == Windows.Devices.Bluetooth.GenericAttributeProfile.GattCommunicationStatus.Success) { // var dialogNotifications = new MessageDialog("Successfully registered for notifications"); // await dialogNotifications.ShowAsync(); notifyCharacteristic.ValueChanged += SelectedCharacteristic_ValueChanged; success = true; } if (success) { success = await InitialKickEvents(); } } catch (Exception ex) { // This usually happens when not all characteristics are found // or selected characteristic has no Notify. //var dialogNotifications = new MessageDialog(ex.Message); //await dialogNotifications.ShowAsync(); //await Task.Delay(100); //Get_Characteriisics(myService); //try again //!!! Add a max try counter to prevent infinite loop!!!!!!! } } } }