【问题标题】:How to check status of bluetooth in Windows Runtime?如何在 Windows 运行时检查蓝牙的状态?
【发布时间】:2014-05-08 14:49:10
【问题描述】:

有一些奇怪的方法,例如检查配对设备 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 运行时应用程序中检查蓝牙状态?

【问题讨论】:

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


    【解决方案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
    }
    

    【讨论】:

    • BluetoothDevice 没有构造函数,是静态的。
    • 哦,我明白了。它不是静态的。它只是没有演员。它要求您使用FromHostNameAsyncFromBluetoothAddressAsync 选择特定设备。您可以使用PeerFinder API 获取该信息
    • 谢谢。似乎捕捉异常是唯一知道的方法。如果蓝牙关闭,它会抛出异常,另外,我的蓝牙不在同行中列出,只有其他设备。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-02
    • 2018-04-28
    • 1970-01-01
    • 2011-03-19
    • 2017-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多