如何在Windows运行时检查蓝牙的状态?

时间:2014-05-08 14:49:11

标签: c# windows-phone-8 windows-runtime windows-phone windows-phone-8.1

有一些奇怪的方法,比如检查配对设备and catching exceptions,看它是否打开。

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运行时应用程序中的蓝牙状态?

1 个答案:

答案 0 :(得分: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
}