user3293835
user3293835

Reputation: 899

How to check status of bluetooth in Windows Runtime?

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?

Upvotes: 1

Views: 4153

Answers (1)

PaulH
PaulH

Reputation: 7843

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
}

Upvotes: 1

Related Questions