如何检查在Windows运行时蓝牙状态?(How to check status of blueto

2019-10-20 02:40发布

有一些奇怪的方式,比如检查对设备和捕获异常 ,看看它是否是与否。

if ((uint)ex.HResult == 0x8007048F)
{
   var result = MessageBox.Show("Bluetooth is turned off.\nTo see the current Bluetooth settings tap 'ok'", "Bluetooth Off", MessageBoxButton.OKCancel);
}

但是,我看到有一个新的BluetoothConnectionStatus API,但不知道如何使用它。

如何检查在Windows Phone的应用程序运行蓝牙状态?

Answer 1:

这可能是这样的:

using Windows.Devices.Bluetooth;
// look for any paired device
PeerFinder.AllowBluetooth = true;
// start looking for BT devices
PeerFinder.Start();
PeerFinder.AlternateIdentities["Bluetooth:Paired"] = "";
// get the list of paired devices
var peers = await PeerFinder.FindAllPeersAsync();
var peer = peers.First(p => p.DisplayName.Contains("my_bt_device_name"));

var bt = await BluetoothDevice.FromHostNameAsync(peer.HostName);
if (bt.ConnectionStatus == BluetoothConnectionStatus.Connected)
{
    // My device is connected
}


文章来源: How to check status of bluetooth in Windows Runtime?