Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plugin.BLE for Xamarin.Forms not detecting devices

I recently started development on an Android and iOS application using Xamarin.Forms. I need to detect and connect to a custom embedded device using BLE, however, I was unsuccessful until now. I tried to use the Plugin.BLE NuGet package, but the app just doesn't seem to detect any device. However, it does respond to changes in the status of the phone's Bluetooth connection (ON/OFF). I tried to pair the device beforehand, but it made no difference. I also tried to check for devices with another app from the Play store, and I can see the respective device there. The code is being run from the MainPage.xaml.cs of the Shared Project after InitializeComponent(). The device that I'm testing the application on is a Google Pixel XL running Android 8.1. Nothing changed when I tried to use a Samsung Galaxy S7 with Android 7.

This is the code that I used, straight from the official documentation.

Code sample:

private async void BLE()
{
   var ble = CrossBluetoothLE.Current;
   var adapter = CrossBluetoothLE.Current.Adapter;
   var state = ble.State;

   adapter.ScanMode = Plugin.BLE.Abstractions.Contracts.ScanMode.LowLatency;

   adapter.ScanTimeout = 5000;

   ble.StateChanged += (s, e) =>
   {
      i++;
      this.display.Text = "The bluetooth state changed, iteration  " + i;
      Debug.WriteLine($"The bluetooth state changed to {e.NewState}");
   };

   adapter.DeviceDiscovered += (s, a) => 
   {
      //  Debug.WriteLine(a.Device);
      eviceList.Add(a.Device.ToString());
     Debug.WriteLine("Device list: " + a.Device);
   };

   adapter.DeviceAdvertised += (s, a) =>
   {
      Debug.WriteLine("Device advertised: " + a.Device);
   };

   await adapter.StartScanningForDevicesAsync();
}

Permissions:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
like image 715
John Szatmari Avatar asked May 24 '18 12:05

John Szatmari


1 Answers

I had this issue a couple of days ago, for me it was a permissions issue as Bluetooth requires the Location permission (as you already have in your code) but the Location permission was never requested from the user.

To solve it I added this to the MainActivity.cs file so all the required permissions are checked/requested before the app starts.

private readonly string[] Permissions =
{
    Manifest.Permission.Bluetooth,
    Manifest.Permission.BluetoothAdmin,
    Manifest.Permission.AccessCoarseLocation,
    Manifest.Permission.AccessFineLocation
};

protected override void OnCreate(Bundle savedInstanceState)
{
    ...

    CheckPermissions();

    LoadApplication(new App());
}

private void CheckPermissions()
{
    bool minimumPermissionsGranted = true;

    foreach (string permission in Permissions)
    {
        if (CheckSelfPermission(permission) != Permission.Granted)
        {
            minimumPermissionsGranted = false;
        }
    }

    // If any of the minimum permissions aren't granted, we request them from the user
    if (!minimumPermissionsGranted)
    {
        RequestPermissions(Permissions, 0);
    }
}
like image 105
bolt19 Avatar answered Sep 22 '22 10:09

bolt19