0

I am using Plugin.BluetoothLE for making app in combination with arduino project. So for mobile app I am using Xamarin forms and Plugin.BluetoothLE library. Problem is that my mobile device doesn't recognize any bluetooth device. This is my code snippet from view model where I scan devices.

private void BtnFindHandler()
{
  if (CrossBleAdapter.Current.Status != AdapterStatus.PoweredOn)
  {
    Debug.WriteLine("Bluetooth is not turned on.");
      return;
  }

  if (CrossBleAdapter.Current.IsScanning)
    CrossBleAdapter.Current.StopScan();

  var scanner = CrossBleAdapter.Current.Scan().Subscribe(scanResult =>
  {
    if (scanResult.Device.Name != null)
      Debug.WriteLine(scanResult.Device.Name);
  });
}

And this is my Android manifest file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.appname" android:installLocation="preferExternal">
    <uses-sdk android:minSdkVersion="23" android:targetSdkVersion="28" />
  <uses-permission android:name="android.permission.BLUETOOTH" />
  <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
  <uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />
  <uses-permission android:name="android.permission.LOCATION_HARDWARE" />
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  <uses-feature android:name="android.hardware.bluetooth_le" android:required="true" />
    <application android:label="TestArduino.Android" android:icon="@mipmap/ic_launcher"></application>
</manifest>

Also in MainActivity.cs, I am asking user explicitly for some some permissions and there is code for that.

 protected override void OnCreate(Bundle bundle)
 {
   TabLayoutResource = Resource.Layout.Tabbar;
   ToolbarResource = Resource.Layout.Toolbar;

   base.OnCreate(bundle);

   global::Xamarin.Forms.Forms.Init(this, bundle);
   LoadApplication(new App(new AndroidInitializer()));

   RequestPermissions(new[]
   {
      Manifest.Permission.AccessCoarseLocation,
      Manifest.Permission.BluetoothPrivileged
      }, 0);
 }

Also I was trying to set manualy permission for my app, but with no success at all. Thank you very much :)

Nenad
  • 119
  • 1
  • 12

3 Answers3

0

You need to ask the user for the Location permission and grant it. Bluetooth permissions in your manifest are enough, no need to ask the user for them.

You can use this PermissionsPlugin to implement this.

And if you want to know why there is a debate as if it is a bug or a feature

Eli
  • 414
  • 5
  • 10
  • I tried to use this plugin and in moment of scanning I have granted permission for Location, but it's still doesn't work. – Nenad Jan 18 '20 at 17:50
  • 1
    @Nenad You can try to grant the permission manually in your phone settings for that application to check. – Eli Jan 18 '20 at 18:29
  • Also I tried that, without success. I am going to try with phone older thant Android 6.0 and post my result here. Thank tou for your time. – Nenad Jan 18 '20 at 18:33
  • @Nenad You may try logging the exceptions in your `Subscribe` – Eli Jan 18 '20 at 18:39
  • There is not any kind of exception, just do not recognize any device :( – Nenad Jan 20 '20 at 16:19
  • @Nenad Please update your question with your new code. Also make sure you are not launching the scan from a background thread. – Eli Jan 20 '20 at 19:30
0

Just turn on the GPS on your phone. Apparently on some devices just requesting and allowing the location permissions is not enough.

MadDeveloper
  • 796
  • 11
  • 20
-1

Use this plugin. Works perfectly for bluetooth. https://www.nuget.org/packages/Jarvis.Connections.Bluetooth/

  • The plugin he is using is a master chief piece of art so please don't suggest to swap for no strong reason. – Eli Jan 19 '20 at 13:41