question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BLE does not scan

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:15

github_iconTop GitHub Comments

4reactions
ghostcommented, Oct 16, 2018

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?

0reactions
janseriscommented, Mar 14, 2023

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?

This still applies. Very frustrating. Android 12

With GPS turned on, devices are discovered. With GPS turned off, no devices are discovered.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android 10 not working with BLE Bluetooth scanning
I am working on BLE Bluetooth scanning is working on all devices except the Android 10. Android 10 is not working, anyone please...
Read more >
BLE not scanning or showing an devices
The problem is that older apps that I had already downloaded onto my device using the same code from App Inventor still work...
Read more >
BLE.scan() example not working
Hi, I am trying to get the BLE.scan() example from the docs working. It compiles and I can flash it to my argon...
Read more >
BLE Issues In Android 7 And Above
To resolve this issue, you can set a filter with any unique identifier like CompanyId or ManufactureId of your BLE device. And it...
Read more >
BLE Scan does not work on Android 12 when targetSDK is ...
Hello ! I've a problem with scanning method on Android 12 (SDK 31). Since the update, if i start a scan, he will...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found