dotnet-bluetooth-le: BLE does not scan

Steps to reproduce

  1. Plugin has been added as suggested.

  2. Visual studio and Xamarin.Forms are updated to the latest version.

  3. Code attached below

using System;

using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Plugin.BLE;
using Plugin.BLE.Abstractions.Exceptions;
using System.Collections.Generic;
using Android.Bluetooth;
using Whale_Watch.Droid;

namespace Whale_Watch.Droid
{
    [Activity(Label = "BLE_Test",  Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected async 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());
            await scanAsync();
        }
        public async System.Threading.Tasks.Task scanAsync()
        {          
            List<Plugin.BLE.Abstractions.Contracts.IDevice> deviceList = new List<Plugin.BLE.Abstractions.Contracts.IDevice>();
            var ble = CrossBluetoothLE.Current;
            var adapter = CrossBluetoothLE.Current.Adapter;
            var state = ble.State;           
            adapter.ScanTimeout = 200000;
            adapter.DeviceDiscovered += (s, a) => deviceList.Add(a.Device);
            await adapter.StartScanningForDevicesAsync();            
        }      
    }
}

Expected behavior

It should scan for all nearby Bluetooth le devices.

Actual behavior

It will run the code and no errors are formed, however even though there are ble devices visible it does not return any or add them to the list. I am unsure if my code is at fault.

Crashlog

Does not crash or cause an exception

Configuration

Version of the Plugin: 1.3.0

Platform: Compiled using Android 8.1

Device: Moto E4 Plus, but will also occur on all other devices tested.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 15

Most upvoted comments

Okay, so this is pretty frustrating. Apparently you have to enable Location services for this to work on newer OS versions. Turn on location and everythign works fine no matter the version or manifest!

Found this out from a stackoverflow article, but haven’t seen ANYTHING about it in the android or Xamarin docs… Hope this solves some more people’s issues as well! Maybe a note about this should be added to the readme here as well?