如何检查设备上是否启用了蓝牙 [英] How to check if bluetooth is enabled on a device

查看:255
本文介绍了如何检查设备上是否启用了蓝牙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查设备上是否启用了蓝牙(以便应用程序可以在无需用户交互的情况下使用它).有什么办法吗?我还可以分别检查蓝牙和低功耗蓝牙吗?

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

推荐答案

我使用Radio类完成了此任务.

I accomplished this using the Radio class.

要检查是否启用了蓝牙:

To check if Bluetooth is enabled:

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 (in general) is supported:

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

如果未安装蓝牙,则无线电列表中将没有蓝牙无线电,并且那里的LINQ查询将返回null.

If Bluetooth isn't installed, then there will be no Bluetooth radio in the radios list, and the LINQ query there will return null.

至于分别检查Bluetooth Classic和LE,我目前正在研究这样做的方法,并且在我确定存在并且可行的情况下将更新此答案.

As for checking Bluetooth Classic and LE separately, I am currently investigating ways to do that and will update this answer when I know for certain that a way exists and works.

这篇关于如何检查设备上是否启用了蓝牙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆