BluetoothLeScanner.startScan with Android 6.0 does not discover devices
if permissions granted, have a try: turn ON the GPS.
Android BLE Scan never finds devices
The Problem
The problem is that you are only overriding the onBatchScanResults
method and not onScanResult
method. onBatchScanResults
will only get triggered if:
- You have set the
ScanSettings
mode toScanSettings.SCAN_MODE_LOW_POWER
(this is the default) using theScanSettings.Builder
. - You have set the report delay time to some value >0 using
setReportDelay(long reportDelayMillis)
in yourScanSettings.Builder
.
reportDelayMillis - Delay of report in milliseconds. Set to 0 to be notified of results immediately. Values > 0 causes the scan results to be queued up and delivered after the requested delay or when the internal buffers fill up.
For example:
public void startScan(BluetoothLeScanner scanner) {
ScanFilter filter = new ScanFilter.Builder().setDeviceName(null).build();
ArrayList<ScanFilter> filters = new ArrayList<ScanFilter>();
filters.add(filter);
ScanSettings settings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_POWER)
.setReportDelay(1l)
.build();
Log.i(TAG,"The setting are "+settings.getReportDelayMillis());
scanner.startScan(filters,settings,BLEScan);
}
The Solution
However, you probably just want to get the results once at a time and not a batch of results. To accomplish that, you don't have to modify the ScanSettings, you just need to override the onScanResult
method in your ScanCallback
and that will do the trick.
private ScanCallback mScanCallback =
new ScanCallback() {
public void onScanResult(int callbackType, ScanResult result) {
System.out.println(result.getDevice().getName())
// Do whatever you want
};
...
};
The Alternative - RxAndroidBle
Anyway, I highly recommend using the library RxAndroidBle. It is very well maintained and it solves many of the BLE issues out of the box (scanning is maybe the less complicated part in BLE).
Using that library, the scanning can be done like this:
Disposable scanSubscription = rxBleClient.scanBleDevices(
new ScanSettings.Builder()
// .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) // change if needed
// .setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES) // change if needed
.build()
// add filters if needed
)
.subscribe(
scanResult -> {
// Process scan result here.
},
throwable -> {
// Handle an error here.
}
);
// When done, just dispose.
scanSubscription.dispose();
Location needs to be enabled for Bluetooth Low Energy Scanning on Android 6.0
No, this is not a bug.
This issue was brought up to Google where they responded saying that this was the intended behavior and they won't fix it. They directed developers to this site where it points out that location permission is now needed for hardware identifier access. It is now the developer's responsibility to make their users aware of the requirement.
In the issue, however, it doesn't address why Location services (GPS, etc.) are required and it doesn't seem like they are going to revisit the issue to explain this since it has been marked as the intended behavior.
To answer the second part of the question: Yes, it is possible to scan without enabling Location services. You can do a Bluetooth classic scan using BluetoothAdapter.getDefaultAdapter().startDiscovery()
and that will work with Location services off. This will discover all Bluetooth devices, BLE and otherwise. However, BLE devices won't have a scan record that they would have had if they were seen as a result of startScan()
.
Android 12: BLE Scan does not find any devices
I found the solution. Contrary to the statements in the official documentation you still need the android.permission.ACCESS_FINE_LOCATION
and android.permission.ACCESS_COARSE_LOCATION
permissions to be set in the Manifest and request them from the mobile user. Now everything works fine again.
Devices are not detected by Bluetooth LE scan
If device has BLE it does not mean that it will be visible (broadcast) any data by default. In order to achieve communication between two smart phones you need to have (install or write your own) application that uses BLE to create proper BLE service that will be exposed to other devices.
Some BLE capable devices such is iPhone for example broadcasts BAS (Battery Service) or similar, but I don't think that Android does.
So, don't expect to find any device on scan that you have described, and if you still need
to communicate two smartphones with Android 5.x through BLE
try to read Bluetooth Low Energy documentation and be ready to understand UUID, GATT, Characteristic...
BluetoothLescan() not finding any devices
Your code should work fine if you are scanning for BLE devices like a heart rate monitor or a proximity sensor. The problem could be that with this code your app is the GATT client and is searching for a GATT server.
So if you want to connect to another phone you could write a GATT server app and run it on the other phone (as indicated in the last paragraph here)
Bluetooth LE can't find any device on android 6.0
Yes you have to ask and be granted those permissions or else nothing will show up under runtime. But what i can see is that you are missing some dependencies on AppCompat
Add this to your build.gradle
file
implementation com.android.support:appcompat-v7:28.0.0
And also don't use implicit version number this will/can cause unpredicted errors because it can upgrade whenever without you knowing and introducing a nasty bug.
Set it always with a explicit number 'com.android.support:support-v4:28.0'
Related Topics
How to Install Android Sdk Build Tools on the Command Line
How to Make an Android App to Always Run in Background
How to Make Alarm Manager Work When Android 6.0 in Doze Mode
How to Know the Map Is Ready to Get Used When Using the Supportmapfragment
How Do Android Screen Coordinates Work
Differences Between Textwatcher 's Ontextchanged, Beforetextchanged and Aftertextchanged
How to Write Vertically in a Textview in Android
Is There a Real Solution to Debug Cordova Apps
Android Image View Pinch Zooming
How to Find and Turn on Usb Debugging Mode on Nexus 4
Intent Action for Network Events in Android Sdk
Error: "Adb Connection Error:An Existing Connection Was Forcibly Closed by the Remote Host"
How to Call a Local Web Service from an Android Mobile Application