Search code examples
c#bluetoothuwpwindows-store-appsmicrosoft-metro

Notification of Bluetooth connection in UWP app


I'm using Windows.Devices API in a WinForms App on Windows 10. I'm connecting to a Bluetooth Barcode scanner. The connection works fine and I'm getting an Event when the connection has been established and also when the connection has been dropped (disconnected). But when I change the battery in the scanner and the scanner re-connects to Windows I'm getting no Event. My question now is: if there's any possibility to get an Event when the device re-connects over Bleutooth?

My need is to reconnect the socket to the device after the device has been reconnected over Bluetooth.

EDIT: Dear admins, this is not a WinForms issue. This issue even doesn't deal with UI at all. It's just an API issue with the Windows.Devices UWP API. Please stop suggesting changing the tags.

Thanks Sven

Some Sample Code:

_device = await BluetoothDevice.FromBluetoothAddressAsync(decimalAddress);
_device.ConnectionStatusChanged += OnDeviceConnectionStatusChanged;

Event is called only on first connect and on disconnect of device:

private void OnDeviceConnectionStatusChanged(BluetoothDevice sender, object args)
{
}

Solution

  • What I've found out so far is that it seems very common that the event never raises again. One has to actively create a new connection to the device, that is, for example by opening a socket for receiving data. So the solution for my problem was to connect to the bluetooth device by calling

    StreamSocket.ConnectAsync()
    

    again to establish a new connection. Afterwards the ConnectionStatusChanged is raised with state connected.

    Good luck

    Sven