/// <summary> /// Helper function to convert a UUID to a name /// </summary> /// <param name="uuid"></param> /// <returns>Name of the UUID</returns> public static string ConvertUuidToName(Guid uuid) { var shortId = BluetoothUuidHelper.TryGetShortId(uuid); if (shortId.HasValue && Enum.TryParse(shortId.Value.ToString(), out GattDescriptorUuid name) == true) { return(name.ToString()); } return(uuid.ToString()); }
public void InitiateDefault() { filename = "polar" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".csv"; var heartrateSelector = GattDeviceService .GetDeviceSelectorFromUuid(GattServiceUuids.HeartRate); var devices = AsyncResult(DeviceInformation .FindAllAsync(heartrateSelector)); var device = devices.FirstOrDefault(); if (device == null) { throw new ArgumentOutOfRangeException( "Unable to locate heart rate device."); } GattDeviceService service; lock (_disposeSync) { if (_isDisposed) { throw new ObjectDisposedException(GetType().Name); } Cleanup(); service = AsyncResult(GattDeviceService.FromIdAsync(device.Id)); _service = service; } var uuid = BluetoothUuidHelper.FromShortId(_heartRateMeasurementCharacteristicId); var heartrate = AsyncResult(service.GetCharacteristicsForUuidAsync(uuid)).Characteristics.FirstOrDefault(); if (heartrate == null) { throw new ArgumentOutOfRangeException( $"Unable to locate heart rate measurement on device {device.Name} ({device.Id})."); } var status = AsyncResult( heartrate.WriteClientCharacteristicConfigurationDescriptorAsync( GattClientCharacteristicConfigurationDescriptorValue.Notify)); heartrate.ValueChanged += HeartRate_ValueChanged; Debug.WriteLine($"Started {status}"); }
/// <summary> /// Initializes a new instance of the<see cref="ObservableGattCharacteristics" /> class. /// </summary> /// <param name="characteristic">Characteristic this class wraps</param> /// <param name="parent">The parent service that wraps this characteristic</param> public ObservableGattCharacteristics(GattCharacteristic characteristic, ObservableGattDeviceService parent) { Characteristic = characteristic; Parent = parent; Name = GattCharacteristicUuidHelper.ConvertUuidToName(characteristic.Uuid); var shortId = BluetoothUuidHelper.TryGetShortId(characteristic.Uuid); if (shortId.HasValue) { ShortUUID = "0x" + shortId.Value.ToString("X"); } else { ShortUUID = ""; } UUID = characteristic.Uuid.ToString(); }
private uint AsShortIdImpl() { var shortId = BluetoothUuidHelper.TryGetShortId(_uuid); return(shortId.HasValue ? shortId.Value: 0); }
private static RfcommServiceId FromShortIdImpl(uint shortId) { Guid g = BluetoothUuidHelper.FromShortId(shortId); return(new RfcommServiceId(g)); }
async void Search_Clicked(object sender, EventArgs e) { foreach (DeviceInformation di in await DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid(BluetoothUuidHelper.FromShortId(0xfff0)))) { System.Diagnostics.Debug.WriteLine(di); } }
private static string GetDeviceSelectorFromShortIdImpl(ushort serviceShortId) { return("bluetoothService:" + BluetoothUuidHelper.FromShortId(serviceShortId).ToString()); }
private async Task CreateHidService() { // HID service. var hidServiceProviderCreationResult = await GattServiceProvider.CreateAsync(GattServiceUuids.HumanInterfaceDevice); if (hidServiceProviderCreationResult.Error != BluetoothError.Success) { Debug.WriteLine("Failed to create the HID service provider: " + hidServiceProviderCreationResult.Error); throw new Exception("Failed to create the HID service provider: " + hidServiceProviderCreationResult.Error); } m_hidServiceProvider = hidServiceProviderCreationResult.ServiceProvider; m_hidService = m_hidServiceProvider.Service; // HID keyboard Report characteristic. var hidKeyboardReportCharacteristicCreationResult = await m_hidService.CreateCharacteristicAsync(GattCharacteristicUuids.Report, c_hidInputReportParameters); if (hidKeyboardReportCharacteristicCreationResult.Error != BluetoothError.Success) { Debug.WriteLine("Failed to create the keyboard report characteristic: " + hidKeyboardReportCharacteristicCreationResult.Error); throw new Exception("Failed to create the keyboard report characteristic: " + hidKeyboardReportCharacteristicCreationResult.Error); } m_hidKeyboardReport = hidKeyboardReportCharacteristicCreationResult.Characteristic; m_hidKeyboardReport.SubscribedClientsChanged += HidKeyboardReport_SubscribedClientsChanged; // HID keyboard Report Reference descriptor. var hidKeyboardReportReferenceCreationResult = await m_hidKeyboardReport.CreateDescriptorAsync(BluetoothUuidHelper.FromShortId(c_hidReportReferenceDescriptorShortUuid), c_hidKeyboardReportReferenceParameters); if (hidKeyboardReportReferenceCreationResult.Error != BluetoothError.Success) { Debug.WriteLine("Failed to create the keyboard report reference descriptor: " + hidKeyboardReportReferenceCreationResult.Error); throw new Exception("Failed to create the keyboard report reference descriptor: " + hidKeyboardReportReferenceCreationResult.Error); } m_hidKeyboardReportReference = hidKeyboardReportReferenceCreationResult.Descriptor; // HID Report Map characteristic. var hidReportMapCharacteristicCreationResult = await m_hidService.CreateCharacteristicAsync(GattCharacteristicUuids.ReportMap, c_hidReportMapParameters); if (hidReportMapCharacteristicCreationResult.Error != BluetoothError.Success) { Debug.WriteLine("Failed to create the HID report map characteristic: " + hidReportMapCharacteristicCreationResult.Error); throw new Exception("Failed to create the HID report map characteristic: " + hidReportMapCharacteristicCreationResult.Error); } m_hidReportMap = hidReportMapCharacteristicCreationResult.Characteristic; // HID Information characteristic. var hidInformationCharacteristicCreationResult = await m_hidService.CreateCharacteristicAsync(GattCharacteristicUuids.HidInformation, c_hidInformationParameters); if (hidInformationCharacteristicCreationResult.Error != BluetoothError.Success) { Debug.WriteLine("Failed to create the HID information characteristic: " + hidInformationCharacteristicCreationResult.Error); throw new Exception("Failed to create the HID information characteristic: " + hidInformationCharacteristicCreationResult.Error); } m_hidInformation = hidInformationCharacteristicCreationResult.Characteristic; // HID Control Point characteristic. var hidControlPointCharacteristicCreationResult = await m_hidService.CreateCharacteristicAsync(GattCharacteristicUuids.HidControlPoint, c_hidControlPointParameters); if (hidControlPointCharacteristicCreationResult.Error != BluetoothError.Success) { Debug.WriteLine("Failed to create the HID control point characteristic: " + hidControlPointCharacteristicCreationResult.Error); throw new Exception("Failed to create the HID control point characteristic: " + hidControlPointCharacteristicCreationResult.Error); } m_hidControlPoint = hidControlPointCharacteristicCreationResult.Characteristic; m_hidControlPoint.WriteRequested += HidControlPoint_WriteRequested; m_hidServiceProvider.AdvertisementStatusChanged += HidServiceProvider_AdvertisementStatusChanged; }