Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bluetooth Low Energy startScan on Android 6.0 does not find devices

I'm developing an application with Bluetooth Low Energy using Nexus 5. It worked on Lollipop and now it is not working on Marshmallow. I set the ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION permissions in the manifest and on runTime in the Activity.

This is the list of ScanFilters:

mScanFilterTest = new ScanFilter.Builder().build(); mScanFilter = new ArrayList<ScanFilter>(); mScanFilter.add(mScanFilterTest); 

These are the settings:

mScanSettings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_POWER).setReportDelay(0)                 .setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES).build(); 

These are my callbacks:

 mBLEScan = new ScanCallback() {      @Override      public void onScanResult(int callbackType, ScanResult result) {          super.onScanResult(callbackType, result);          Log.i(TAG, "******************************************");          Log.i(TAG, "The scan result " + result);          Log.i(TAG, "------------------------------------------");          }      }; 

This is my call:

mBluetoothLeScanner.startScan(mScanFilter, mScanSettings, mBLEScan); 

It starts the scan but does not find any device. Please help me!!!!

like image 662
Jacopo Tosi Avatar asked Oct 09 '15 16:10

Jacopo Tosi


People also ask

Why does Bluetooth low energy requires location?

Your app needs this permission because a Bluetooth scan can be used to gather information about the location of the user. This information may come from the user's own devices, as well as Bluetooth beacons in use at locations such as shops and transit facilities.”

How do I scan my Android phone with BLE?

To find BLE devices, you use the startScan() method. This method takes a ScanCallback as a parameter. You must implement this callback, because that is how scan results are returned.

What devices Bluetooth low energy?

Fitness trackers and smart appliances Because Bluetooth technologies (both classic and BLE) are so commonly available in smartphones, tablets, and laptops, it stands to reason that personal devices that we frequently pair with them – such as fitness trackers and various smart appliances – would use these, too.


2 Answers

I struggled with the same issue. To fix it you have to enable "Location" (GPS) in the settings of the phone as well as request location permission in the app at runtime. Both need to be done for scanning to work properly.

To request the location permission put the following in a dialog or the likes:

yourActivity.requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, yourPermissionRequestCode); 

and implement:

@Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults){     if(requestCode == yourPermissionRequestCode)     {         ... //Do something based on grantResults     } } 

in yourActivity and handle whatever the user selects. You also need to do the following to turn on your device's location services:

Intent enableLocationIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); yourActivity.startActivityForResult(enableLocationIntent, yourServiceRequestCode); 

You can check if the user turned on the location services by implementing:

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {     if(requestCode == yourServiceRequestCode)     {         ...//Do whatever you need to     } } 

in yourActivity. You can also manually turn on location (GPS) services by doing:

Enter phone settings -> Select "Location" -> Then turn it on

It should look like this in the Phone settings:

Location (GPS) services enabled in Phone Settings

Or like this in the quick-settings drop down:

Location (GPS) services enabled in drop down settings

Once the user has enabled the permission and started location services then you should start scanning. I've noticed that if you are already scanning while you enable the permission/turn on the location service it will still not put anything in your onScanResults

I'm not sure if this is a bug or a "feature" for iBeacons/Bluetooth advertising (NOTE: advertising as in selling products not the technical Bluetooth advertising) to allow companies to see your location and direct you to where they want.

Hope this fixes your problem!

EDIT I meant to add: you only need this for SCANNING. Once you are connected to the BLE device you can shut off the location service on your phone and you will still be connected to your devices. However, you cannot then discover or connect to any new devices and all advertising devices will drop from the onScanResults

like image 88
V-PTR Avatar answered Sep 23 '22 11:09

V-PTR


Like @Jacopo Tosi say you must ask for location permissions. You can read here how to do it. And you also must turn on Location. i don't know why the did it. but it's the only way i managed to make it work.

like image 32
Maor Hadad Avatar answered Sep 23 '22 11:09

Maor Hadad