Search code examples
uwpbluetooth-lowenergywin-universal-appwindows-10-universalgatt

How can I reconnect to BLE device? - UWP


Both the device id and the address are random that I CANNOT re-use them for bleDevice = await BluetoothLEDevice.FromIdAsync or bleDevice = await BluetoothLEDevice.FromBluetoothAddressAsync.

I came up with 2 possible solutions: (1)ConnectionStatusChange & (2)Pairing,

but both cases I have problems.


(1) MAIN QUESTION

Before I disconnect, I first save the bleDevice as bleDeviceReconnect.

Then I unsubscribe, dispose characteristics & service & bluetooth le device, and set them to null.

In the case of reconnecting, instead of bleDevice = await BluetoothLEDevice.FromIdAsync(bleDeviceReconnect.DeviceId), I'm trying to use:

var newSession = GattSession.FromIdAsyn(bleDeviceReconnect.BluetoothDeviceId);
newSession.MaintainConnection = true;
bleDeviceReconnect.ConnectionStatusChanged += ConnectionStatusChangedHandler;

But this case, event handler is NEVER CALLED.

The handler looks like this:

private async void ConnectionStatusChangedHandler(BluetoothLEDevice bluetoothLeDevice, object o)
{
   Debug.WriteLine(bluetoothLeDevice.ConnectionStatus.ToString()) //To check if event comes or not
   if(bluetoothLeDevice.ConnectionStatus.ToString() == "Conncected")
   {
      sList = bluetoothLeDevice.GetGattServicesForUuidAsync(custom service GUID)
      .
      .
      .
   }
}

(2) Would be great if you can answer this question as well, but you don't have to if you don't know or you don't want

Does pairing make the device id or address unchanged?

I first need to use either one of the methods I said above in order to get the Bluetooth LE object, which I can use it to find the custom service (sList = bleDevice.GetGattServicesForUuidAsync) and characteristics (cList = sList.Services[0].GetCharacteristicsForUuidAsync), and subscribe/write to the characteristi and so on.

Hence, if the id or address changes, pairing has no meaning.


How can I correctly reconnect to BLE device???

I would really appreciate any help.


EDIT

Richard Zhang - MSFT asked for a minimal runnable demo that I'm adding this:

Microsoft BluetoothLE Code

(1) Scenario2_Client.xaml

Add 2 buttons: [Disconnect] and [Reconnect]

*Disconnection does not happen right away. Please wait at least 5 seconds before you press [Reconncect]

(2) Scenario2_Client.xaml.cs

Declare private BluetoothLEDevice bluetoothLeDeviceReconnect

private void Disconnect_btn_Click(object sender, RoutedEventArgs e)
{
   bluetoothLeDeviceReconnect = bluetoothLeDevice;

   RemoveValueChangedHandler();
   selectedCharacteristic?.Service?.Dispose();
   selectedCharacteristic = null;
   service.Dispose();
   service = null;
   bluetoothLeDevice?.Dispose();
   bluetoothLeDevice = null;
   GC.Collect();
}

private void Reconnect_btn_Click(object sender, RoutedEventArgs e)
{
   var newSession = GattSession.FromIdAsyn(bluetoothLeDeviceReconnect.BluetoothDeviceId);
   newSession.MaintainConnection = true;
   bluetoothLeDeviceReconnect.ConnectionStatusChanged += ConnectionStatusChangedHandler;
}

private async void ConnectionStatusChangedHandler(BluetoothLEDevice bluetoothLeDevice, object o)
{
   SEE ABOVE
}

Currently I'm running out of time that I couldn't test this yet.

Lemme know if this doesn't work as what I said! I'll make a GitHub account, upload my project, and copy the link & paste it here.


Solution

  • NVM It works!

    The mobile app was the problem during the pairing.

    Also, I had to pair AND add the handler to make it work.

    I can get the BluetoothLEDevice object via the handler, but searching services is now an issue.

    Since this is not the same problem I asked in this post, I'll post a new question.