I am trying to detect nearby Bluetooth Low-Energy devices on a service. When the service is started, startLeScan() is called, and then stopLeScan() is called after 10 seconds. Even though startLeScan() returns true, and I didn't get any error, onLeScan on the LeScanCallback was not called. the Service:
.
.
.
// Device scan callback.
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) {
Log.i(TAG, "New LE Device: " + device.getName() + " @ " + rssi);
if (DEVICE_NAME.equals(device.getName()) && deviceAddress.equals(device.getAddress())) {
mDevice = device;
BleScan(false);
connectBluetoothDevice();
}
}
};
/**
* Starts and stops Bluetooth LE scanning for BLE devices.
*
* @param enable true to start scanning, false to stop scanning.
*/
public void scanLeDevice(final boolean enable) {
if (enable) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
if (mScanning) {
BleScan(false);
}
}
}, SCAN_PERIOD);
BleScan(true);
} else {
BleScan(false);
}
}
/**
* @param scan true to scan, and false to stop scanning for Bluetooth LE devices.
*/
private void BleScan(boolean scan) {
if (scan) {
mScanning = true;
boolean temp = mBluetoothAdapter.startLeScan(mLeScanCallback);
Log.i(TAG, "Started LE scan: " + temp);
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}
I tried to use BluetoothLeScanner startScan() and stopScan(), and using ScanCallback instead, but it didn't help:
ScanCallback mScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
Log.i(TAG, "New LE Device: " + result.getDevice().getName() + " @ " + result.getRssi());
}
@Override
public void onScanFailed(int errorCode) {
Log.i(TAG, "Scan Faild!!");
}
};
/**
* Starts and stops Bluetooth LE scanning for BLE devices.
*
* @param enable true to start scanning, false to stop scanning.
*/
public void scanLeDevice(final boolean enable) {
if (enable) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
if (mScanning) {
BleScan(false);
}
}
}, SCAN_PERIOD);
BleScan(true);
} else {
BleScan(false);
}
}
/**
* @param scan true to scan, and false to stop scanning for Bluetooth LE devices.
*/
private void BleScan(boolean scan) {
if (scan) {
mScanning = true;
mBluetoothLeScanner.startScan(mScanCallback);
Log.i(TAG, "Started LE scan");
} else {
mScanning = false;
mBluetoothLeScanner.stopScan(mScanCallback);
}
}
some say GPS needs to be turned on, so I turned on GPS. I tried rebooting the android device. Another BLE detection app can see the BLE device.
Why isn't the scan callback called?
Edit
I'm running this app on a nexus 5 device running Android 6.0.1 .
I tried adding location permission, but it didn't help:
( uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" )
Try to switch on the access to location data in app settings (Settings -> Applications -> Your App -> Rules -> Location Data)
I have similar problem. Same code, one tablet not work but other does. I have no solution but when I reset my tablet and reinstall apk, it's can work fine again.
I found this onLeScan from BluetoothAdapter.LeScanCallback not called in Android Marshmallow when I open gps, it's work...