Search code examples
androidbluetooth-lowenergyandroid-bluetoothandroid-ibeaconandroid-ble

Android BLE- ScanCallBack is never called on BluetoothLeScanner.startScan()


I'm trying to develop a Beacon locator using the BluetoothLeScanner but the problem is that ScanCallBack is never called, to be precise neither of the methods onScanResult, onScanFailed nor onBatchScanResults is called. I have beacon emitter located near me which are detected by beacon locator apps available on play store. I'm not sure if I'm missing anything. Following is the code I'm using.

First I added following permissions in my manifest:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

Code to create an object of BluetoothLeScanner:

 Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
 getApplicationContext().startActivity(enableBtIntent);
 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
 mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
 mBluetoothLeScanner.startScan(mScanCallback);

ScanCallBack:

protected ScanCallback mScanCallback = new ScanCallback() {
@Override
    public void onScanResult(int callbackType, ScanResult result) {
    ScanRecord mScanRecord = result.getScanRecord();
    byte[] manufacturerData = mScanRecord.getManufacturerSpecificData(224);
    int mRssi = result.getRssi();
}

@Override
public  void  onScanFailed(int errorCode){
    Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_LONG);
}
};

I also made sure that none of the object values are null, all of them are perfect initialized. Also, tested on multiple devices with API 24 so I don't think that is also an issue. Appreciate your help.

Thanks


Solution

  • After doing quite a research, I found out that the best possible solution for Beacon locator is to use the altbeacon library. Various tutorials on how to use the library can be found online. I would suggest their official site would be best for reference : https://altbeacon.github.io/android-beacon-library/samples.html If anyone is interested in my code used to implement the beacon locator, please comment below and I would be more than happy to share it with you.