How to check if Bluetooth is enabled on the device

I want to check if Bluetooth is enabled on the device (so that the application can use it without user interaction). Is there any way to do this? Can I also check Bluetooth and Bluetooth Low Energy separately?

+5
source share
3 answers

I accomplished this with the Radio class.

To check if Bluetooth is turned on:

 public static async Task<bool> GetBluetoothIsEnabledAsync() { var radios = await Radio.GetRadiosAsync(); var bluetoothRadio = radios.FirstOrDefault(radio => radio.Kind == RadioKind.Bluetooth); return bluetoothRadio != null && bluetoothRadio.State == RadioState.On; } 

To check if Bluetooth is supported (in general):

 public static async Task<bool> GetBluetoothIsSupportedAsync() { var radios = await Radio.GetRadiosAsync(); return radios.FirstOrDefault(radio => radio.Kind == RadioKind.Bluetooth) != null; } 

If Bluetooth is not installed, there will be no Bluetooth radio in the list of radio stations, and the LINQ query will return zero there.

As for checking Bluetooth Classic and LE separately, I am currently exploring ways to do this and updating this answer when I know for sure that the method exists and works.

+8
source

Is there any way to do this? Can I also check Bluetooth and Bluetooth Low Energy separately?

What do you mean by β€œdevice”, is it the device on which the application is running, or the device on which the Bluetooth service is installed, to which the application must have access?

As far as I know, there is no API in UWP to check if Bluetooth is enabled on the device.

On a Windows Mobile device, you can use the following method as a workaround.

 private async void FindPaired() { // Search for all paired devices PeerFinder.AlternateIdentities["Bluetooth:Paired"] = ""; try { var peers = await PeerFinder.FindAllPeersAsync(); // Handle the result of the FindAllPeersAsync call } catch (Exception ex) { if ((uint)ex.HResult == 0x8007048F) { MessageBox.Show("Bluetooth is turned off"); } } } 

On a Windows PC, I suggest you check the availability of services at the Bluetooth service level as a workaround.

For non-BLE services, such as RFCOMM, you can get the number of devices with a specific service identifier. If Bluetooth is disabled at the hardware level, the counter will be 0.

 rfcommServiceInfoCollection = await DeviceInformation.FindAllAsync( RfcommDeviceService.GetDeviceSelector(RfcommServiceId.ObexObjectPush)); 

For BLE services, you can use the BluetoothLEAdvertisementWatcher class to receive BLE ads. If Bluetooth is disabled at the hardware level, ads will not be accepted.

 watcher = new BluetoothLEAdvertisementWatcher(); watcher.Received += OnAdvertisementReceived; private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs) { var address = eventArgs.BluetoothAddress; BluetoothLEDevice device = await BluetoothLEDevice.FromBluetoothAddressAsync(address); var cnt =device.GattServices.Count; watcher.Stop(); } 
+2
source

Mixing @Zenel's answer and the new BluetoothAdapter class (from Win 10 Creators Update):

 /// <summary> /// Check, if any Bluetooth is present and on. /// </summary> /// <returns>null, if no Bluetooth LE is installed, false, if BLE is off, true if BLE is on.</returns> public static async Task<bool?> IsBleEnabledAsync() { BluetoothAdapter btAdapter = await BluetoothAdapter.GetDefaultAsync(); if (btAdapter == null) return null; if (!btAdapter.IsCentralRoleSupported) return null; var radios = await Radio.GetRadiosAsync(); var radio = radios.FirstOrDefault(r => r.Kind == RadioKind.Bluetooth); if (radio == null) return null; // probably device just removed // await radio.SetStateAsync(RadioState.On); return radio.State == RadioState.On; } 
+1
source

Source: https://habr.com/ru/post/1233234/


All Articles