private async void EnumeratingChara(GattDeviceService service) { string charaName; characteristics = null; try { var accessStatus = await service.RequestAccessAsync(); if (accessStatus == DeviceAccessStatus.Allowed) { var result = await service.GetCharacteristicsAsync(BluetoothCacheMode.Uncached); if (result.Status == GattCommunicationStatus.Success) { characteristics = result.Characteristics; } else { NotifyUser("Error accessing service.", NotifyType.ErrorMessage); characteristics = new List <GattCharacteristic>(); } } else { NotifyUser("Error accessing service.", NotifyType.ErrorMessage); characteristics = new List <GattCharacteristic>(); } } catch (Exception ex) { NotifyUser("Restricted service. Can't read charcteristics:" + ex.Message, NotifyType.ErrorMessage); characteristics = new List <GattCharacteristic>(); } var chara = characteristics[0]; charaName = DisplayHelpers.GetCharacteristicName(chara); CharaReadUUID.Text = charaName; CharaReadUUIDBorder.Visibility = Visibility.Visible; EnumerationReadDescriptor(chara); chara = characteristics[1]; charaName = DisplayHelpers.GetCharacteristicName(chara); CharaWriteUUID.Text = charaName; CharaWriteUUIDBorder.Visibility = Visibility.Visible; EnumerationWriteDescriptor(chara); CharaReadUUIDBorder.Visibility = Visibility.Visible; }
private async void ConnectButton_Click(object sender, RoutedEventArgs e) { ConnectButton.IsEnabled = false; ClearBluetoothLEDeviceAsync(); try { // BT_Code: BluetoothLEDevice.FromIdAsync must be called from a UI thread because it may prompt for consent. bluetoothLeDevice = await BluetoothLEDevice.FromIdAsync(bleDeviceDisplay.Id); if (bluetoothLeDevice == null) { NotifyUser("Failed to connect to device.", NotifyType.ErrorMessage); ConnectButton.IsEnabled = true; } } catch (Exception ex) when(ex.HResult == E_DEVICE_NOT_AVAILABLE) { NotifyUser("Bluetooth radio is not on.", NotifyType.ErrorMessage); ConnectButton.IsEnabled = true; } if (bluetoothLeDevice != null) { // Note: BluetoothLEDevice.GattServices property will return an empty list for unpaired devices. For all uses we recommend using the GetGattServicesAsync method. // BT_Code: GetGattServicesAsync returns a list of all the supported services of the device (even if it's not paired to the system). // If the services supported by the device are expected to change during BT usage, subscribe to the GattServicesChanged event. GattDeviceServicesResult result = await bluetoothLeDevice.GetGattServicesAsync(BluetoothCacheMode.Uncached); if (result.Status == GattCommunicationStatus.Success) { var services = result.Services; string serviceName; foreach (var service in services) { serviceName = DisplayHelpers.GetServiceName(service); if (serviceName.IndexOf("Custom Service") >= 0) { NotifyUser("Found custom service", NotifyType.StatusMessage); ServiceUUID.Text = DisplayHelpers.GetServiceName(service); ServiceUUIDBorder.Visibility = Visibility.Visible; EnumeratingChara(service); ConnectButton.IsEnabled = false; break; } else { NotifyUser("Not found customr service", NotifyType.ErrorMessage); ConnectButton.IsEnabled = true; } } } else { NotifyUser("Device unreachable", NotifyType.ErrorMessage); ConnectButton.IsEnabled = true; } } }