1

I am creating an application to scan for peripheral devices. Here is my code for starting scan:

private void startScan(){
    bleScanner = bleAdapter.getBluetoothLeScanner();
    List<ScanFilter> filters = new ArrayList<ScanFilter>();
    filters.add(new ScanFilter.Builder().build());
    ScanSettings settings = new ScanSettings.Builder()
            .setScanMode(ScanSettings.SCAN_MODE_LOW_POWER)
            .setReportDelay(1000)
            .build();
    bleScanner.startScan(filters,settings,scanCallback);
    Log.i(TAG,"Scan started");
}

And here is the scan call back:

private ScanCallback scanCallback = new ScanCallback() {
    @Override
    public void onScanResult(int callbackType, ScanResult result) {
        Log.i("scan found something");
    }

    @Override
    public void onBatchScanResults(List<ScanResult> results) {
        if(!results.isEmpty()){
            BluetoothDevice advert = results.get(0).getDevice();
            Log.i("found device: "+advert.getAddress());
            stopScan();
            startClient(advert);
        }else{
            Log.i("scanning...");
        }
    }

    @Override
    public void onScanFailed(int errorCode) {
        Log.i("Scan failed to start");
    }
};

I have two android devices on which i am testing this application, one is running on API 21 and other on 23. When i test this app on device with API 22 the call backs are working correctly, but when tested on device with API 23 I do get the "Scan started" log, but scanCallBack is not working. I have added all the required permissions:

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

Any help would be appreciated, Thank you!

Hashim Ali
  • 141
  • 7
  • Possible duplicate of [Android runtime permissions- how to implement](https://stackoverflow.com/questions/35163953/android-runtime-permissions-how-to-implement) – Pavneet_Singh Aug 31 '17 at 14:36
  • Thank you @Pavneet_Singh that was the problem, i wasn't requesting permission at run time as one should if developing Android 6.0 or greater. – Hashim Ali Aug 31 '17 at 21:27

0 Answers0