如何检查设备上是否启用了蓝牙

时间:2015-10-08 10:32:41

标签: bluetooth windows-10 win-universal-app uwp

我想检查设备上是否启用了蓝牙(以便应用可以在没有用户交互的情况下使用它)。有没有办法做到这一点?我还可以单独检查蓝牙和蓝牙低功耗吗?

3 个答案:

答案 0 :(得分:8)

我使用Radio类完成了这项工作。

检查蓝牙是否已启用:

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;
}

检查是否支持蓝牙(一般情况下):

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

如果未安装蓝牙,则无线电列表中将不会有蓝牙无线电,并且LINQ查询将返回空。

至于单独检查Bluetooth Classic和LE,我正在研究如何做到这一点,并且当我确定某种方式存在并且有效时,我会更新这个答案。

答案 1 :(得分:2)

  

有没有办法做到这一点?我还可以单独检查蓝牙和蓝牙低功耗吗?

“设备”是什么意思,是应用程序运行的设备,还是设备托管应用程序需要访问的蓝牙服务?

据我所知,UWP中没有API来检查设备上是否启用了蓝牙。

在Windows Mobile设备上,您可以使用以下方法作为解决方法。

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");
         }
     }
 }

在Windows PC设备上,我建议您检查蓝牙服务级别的服务辅助功能作为解决方法。

对于像RFCOMM这样的非BLE服务,您可以获得具有特定服务ID的设备的数量。如果在硬件级别禁用蓝牙,则计数将为0.

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

对于BLE服务,您可以使用BluetoothLEAdvertisementWatcher类接收BLE广告。如果在硬件级别禁用蓝牙,则不会接收任何广告。

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 :(得分:2)

混合@Zenel答案和新BluetoothAdapter课程(来自Win 10 Creators更新):

/// <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;
    // for UWP
    var radio = await btAdapter.GetRadioAsync();
    // for Desktop, see warning bellow
    var radios = await Radio.GetRadiosAsync().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;
}

桌面警告: Radio.GetRadiosAsync()无法在运行see the doc时针对不同的arch编译的桌面应用上运行。作为工作流程,您可以使用WMI解决方法:

SelectQuery sq = new SelectQuery("SELECT DeviceId FROM Win32_PnPEntity WHERE service='BthLEEnum'");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(sq);
return searcher.Get().Count > 0;