Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is BluetoothLEScanner not calling its ScanCallback

I'm trying to implement a BLE scanner right now in a bare bones android app. I have been following: startLeScan replacement to current api and https://developer.android.com/guide/topics/connectivity/bluetooth-le

Unfortunately, the callback functions from my BLEScanCallback class don't seem to be getting called. I've just been trying Log.e to see if any of them are getting called.

I debugged the app and the app gets to bluetoothLeScanner.stopScan(scanCallback); within the run() function.

I have added BLUETOOTH, BLUETOOTH_ADMIN and ACCESS_COARSE_LOCATION to the manifest.

I'm not really sure what I've done wrong here. Any help with this would be much appreciated.

Thanks

Relevant code below:

public class DiscoverActivity extends AppCompatActivity {

private BluetoothAdapter bluetoothAdapter;
private boolean scanning;
private Handler handler;
static int REQUEST_ENABLE_BT = 1001;
private static final long SCAN_PERIOD = 10000;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_discover);
    handler = new Handler();
    scanning = false;

    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    bluetoothAdapter = bluetoothManager.getAdapter();
    // Ensures Bluetooth is available on the device and it is enabled. If not,
    // displays a dialog requesting user permission to enable Bluetooth.
    if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }
}

public boolean onOptionsItemSelected(MenuItem item) {
    boolean ret;
    int id = item.getItemId();

    switch (id){
        case R.id.scan:
            Log.e("test", "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
            scanBLEDevices(true);
            ret = true;
            break;
        default:
            ret = super.onOptionsItemSelected(item);
    }

    return ret;
}

private void scanBLEDevices(final boolean enable){
    final BluetoothLeScanner bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();

    final BLEScanCallback scanCallback = new BLEScanCallback();
    if (enable){
        // Stops scanning after a pre-defined scan period.
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                scanning = false;
                bluetoothLeScanner.stopScan(scanCallback);
            }
        }, SCAN_PERIOD);

         scanning = true;
         bluetoothLeScanner.startScan(scanCallback);
    }else{
        scanning = false;
        bluetoothLeScanner.stopScan(scanCallback);
    }
}

public class BLEScanCallback extends ScanCallback{
    @Override
    public void onScanResult(int callbackType, ScanResult result) {
        super.onScanResult(callbackType, result);
        Log.e("Scan Success", "Scan Success");
    }

    @Override
    public void onBatchScanResults(List<ScanResult> results) {
        super.onBatchScanResults(results);
        Log.e("Scan Success", "Scan Success Batch");
    }

    @Override
    public void onScanFailed(int errorCode) {
        super.onScanFailed(errorCode);
        Log.e("Scan Failed", "Error Code: " + errorCode);
    }
}

}

like image 665
Josh P. Avatar asked Jan 03 '23 10:01

Josh P.


1 Answers

Turns out that even though I added ACCESS_COURSE_LOCATION permissions from the manifest, this was not enough for API 23+. I think in lower api versions the request in the manifest is sufficient.

I had to add:

if (Build.VERSION.SDK_INT >= 23) {
        if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("This app needs location access");
            builder.setMessage("Please grant location access so this app can detect peripherals.");
            builder.setPositiveButton(android.R.string.ok, null);
            builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @RequiresApi(api = Build.VERSION_CODES.M)
                @Override
                public void onDismiss(DialogInterface dialog) {
                    requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
                }
            });
            builder.show();
        }
    }
like image 54
Josh P. Avatar answered Jan 05 '23 18:01

Josh P.