1

有一些奇怪的方法,如檢查配對設備and catching exceptions以查看它是否打開。如何在Windows運行時檢查藍牙的狀態?

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

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

如何檢查Windows Phone Runtime應用程序中的藍牙狀態?

回答

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 
} 
+0

BluetoothDevice類還沒有構造,是靜態的。 – user3293835

+0

好的。我更新了我的回答 – PaulH

+0

哦,我明白了。這不是靜態的。它只是沒有ctor。它要求您使用'FromHostNameAsync'或'FromBluetoothAddressAsync'選擇特定設備。您可以使用「PeerFinder」API獲取該信息 – PaulH