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