Using Plugin.BLE to connect/reconnect to BLE device

Phillip Mobley 51 Reputation points
2024-10-11T02:18:25.0166667+00:00

Hello all,

as in the title, I am using the Plugin.BLE nuget package to connect to a device via BLE. On app startup, I have no problem connecting and reading/writing to the BLE device. My issues start happening once I reconnect to the device in the event the connection is lost (device turns off/on or too far away).

What ends up happening is that my Android app is able to connect to the BLE device. I am also able to read the characteristics. However, when I try to use one of the characteristics, the app reports back an error saying the function timed-out.

I have tried many things to get this part to work. I am inheriting from IDisposable and calling the Dispose method and wiping out my different properties for the BLE device, characteristics, etc. But it seems to not be liking that.

Not sure what is going on and if someone could give me some pointers, I would be grateful. Also are there any limitations to the Plugin.BLE library I should be aware of based off of what I am doing?

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,592 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 76,546 Reputation points Microsoft Vendor
    2024-10-11T07:35:49.7033333+00:00

    Hello,

    My issues start happening once I reconnect to the device in the event the connection is lost (device turns off/on or too far away).

    Please implement the DeviceConnectionLost event, if the connection is lost, this event will be invoked, you can use adapter.ConnectToKnownDeviceAsync(deviceId); method to reconnect it.

    adapter.DeviceConnectionLost += Adapter_DeviceConnectionLost;
     
    
    private void Adapter_DeviceDisconnected(object? sender, Plugin.BLE.Abstractions.EventArgs.DeviceEventArgs e)
      {
          
          AttemptReconnect(e.Device.Id);
      }
    
    
    async void AttemptReconnect(Guid deviceId)
    {
    
     
         try
         {
             //show loading dialog or smth
             await adapter.ConnectToKnownDeviceAsync(deviceId);
             //
         }
         catch (TaskCanceledException e)
         {
     
             //do nothing, task was cancelled
         }
         catch (Exception ex)
         {
             //retry, android will throw a GATT 133 error if device is still not in range, ios willl try indefinately
             AttemptReconnect(deviceId);
         }
         finally
         {
             //hide loading
         }
    }
    

    By the way, do not forget to add following runtime permissions(permissions are runtime permissions. Therefore, you must explicitly request user approval in your app before you can look for Bluetooth devices).

    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
    

    I test it in Android devices and use BLE to connect to the iPhone devices, If I turn off the iPhone devices's bluetooth and reopen it, DeviceConnectionLost will be executed and reconnect it.

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.