How to check status of bluetooth in Windows Runtime?

1

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?

c#
windows-phone-8
windows-runtime
windows-phone
windows-phone-8.1
asked on Stack Overflow May 8, 2014 by user3293835 • edited May 23, 2017 by Community

1 Answer

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
}
answered on Stack Overflow May 8, 2014 by PaulH • edited May 8, 2014 by PaulH

User contributions licensed under CC BY-SA 3.0