Ken Wood
Ken Wood

Reputation: 41

BluetoothLeScanner never calls any of its callback methods

I'm very new to Android and Kotlin so I may be getting something very simple wrong, but as far as I can see when I call BluetoothLeScanner.startScan() none of the possible callback methods of the ScanCallback class which I've created is ever called.

I've understood that at API level 23 & above just putting the location permissions in the manifest may not be enough so I've written code to handle that & am satisfied that my App has both COARSE and FINE location permissions

Here's my override of the OnScanResult method:

override fun onScanResult(callbackType: Int, result: ScanResult?) {
    super.onScanResult(callbackType, result)
    mScan = true
}

I've put a break point in each of the callback methods and when I hover over these breakpoints while the code is running, I see the message "No executable code found at line..." That's a pretty disturbing message (and I suspect is pointing to where the problem lies) but (a) how can there be no code there when everything builds OK and (b) what do you do about it? Update on that: I think that message is a red herring. I've now moved the break points to elsewhere within the callback functions and I no longer see the 'no executable code' message. Looks like Android Studio lets you put a break point on a line with no actual code in it! So we're back to the original question - why are we getting no callbacks?

Looks like this is now solved: (1) I did find a setting on the phone as distinct from turning on Location. It was enable Bluetooth scanning. However it actually made no difference (2) What looks to have been the real issue is a misunderstanding of the meaning of the string which you pass to the ScanFilter Builder with setDeviceName(). There is a string in our hardware Bluetooth module which we're trying to scan for which is called device name, and I was scanning for that. When I looked instead for the Beacon advertising data, it found it.

Many thanks for suggestions (only 1 I think)

Upvotes: 2

Views: 581

Answers (1)

Thomas Morris
Thomas Morris

Reputation: 822

Giving permissions in the manifest is not the same as the app using it. For ble you need to give the location and bluetooth permission. Then:

  • in the app(on the phone) browse your open apps
  • find your app and click the 3 dots in the top left
  • Click app info
  • Permissions
  • Toggle location to on

Also the following is a handy bit of code:

public void checkPermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && checkSelfPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

            } else {
                ActivityCompat.requestPermissions(this, new String[]{
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.ACCESS_COARSE_LOCATION,}, 1);
            }
        }
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
        } else {
            checkPermission();
        }
    }

Ps Be safe all

Upvotes: 2

Related Questions