How to check status of bluetooth in Windows Runtime?

天涯浪子 提交于 2020-01-25 03:55:30

问题


There are some odd ways, like checking for pair devices and catching exceptions to see it if is on or not.

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

But I see there is a new BluetoothConnectionStatus api but don't know how to use it.

How to check bluetooth status in Windows Phone Runtime apps?


回答1:


It's probably something like this:

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
}


来源:https://stackoverflow.com/questions/23545233/how-to-check-status-of-bluetooth-in-windows-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!