BluetoothLeScanner could not find callback wrapper
Asked Answered
D

4

14

Because of I had problems with Bluetooth on Android Lollipop, I have tried to change the scanner method.
So I have tried to use the new package. In the previous version, I called startScan(mLeScanCallback) and everything works but now, when I call startScan(mScanCallback) I have the error: "D/BluetoothLeScanner: could not find callback wrapper".
No devices are found and the ListAdapter, I use to show the devices, is empty.

The comment lines are the previous code (and it worked!). This my code:

public class Selection extends ListActivity implements ServiceConnection {

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mHandler = new Handler();

        // Initializes a Bluetooth adapter through BluetoothManager.
        final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter = bluetoothManager.getAdapter();



        getApplicationContext().bindService(new Intent(this, MetaWearBleService.class), this, Context.BIND_AUTO_CREATE);

}

private void scanLeDevice(final boolean enable) {
    final BluetoothLeScanner bluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();

    if (enable) {

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {

                //mBluetoothAdapter.stopLeScan(mLeScanCallback);
                bluetoothLeScanner.stopScan(mScanCallback);
                setListAdapter(listAdapter);

            }
        }, SCAN_PERIOD);


        //mBluetoothAdapter.startLeScan(mLeScanCallback);
        bluetoothLeScanner.startScan(mScanCallback);

   } else {

        //mBluetoothAdapter.stopLeScan(mLeScanCallback);
        bluetoothLeScanner.stopScan(mScanCallback);
        setListAdapter(listAdapter);

    }


}

private ScanCallback mScanCallback =
        new ScanCallback() {

            public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        listAdapter.addDevice(device);

                    }
                });
            }
        };

Instead the ListAdapter extends BaseAdapter and use a ViewHolder. If it necessary, I post it.

So what does it mean "D/BluetoothLeScanner: could not find callback wrapper"? What is it wrong?

Otherwise how I can't resolve the problem of scanning with the Android Lollipop? In Lollipop I have often errors about BluetoothGatt. I don't know to minized it (or solve it).

Thanks

Diadromous answered 29/11, 2016 at 9:56 Comment(0)
H
27

The log message D/BluetoothLeScanner: could not find callback wrapper appears whenever Android's bluetooth scanning APIs are told top stop scanning for an app when they think scanning has not started. You can see this by looking at the source code of Android's BluetoothLeScanner here.

This is usually safe to ignore as there are lot of reasons that scanning my not have actually started (it was already stopped, bluetooth is off, permissions have not been granted, etc.) Client software that does scanning often stops scanning on a timer regardless of whether it has been successfully started, or whether it was manually stopped before the timer goes off. Android's example code (and the code shown above) does exactly this, often causing these log messages to show up.

If you really want to minimize these messages, you need to keep track of whether scanning actually started and only stop scanning if it actually did. Unfortunately, you don't get a return code if scanning starts successfully, and you only get an asynchronous callback to onScanFailed(errorCode) if you cannot start successfully. So one approach would be to set scanStartCount++; when you call start scan, and set scanStartCount--; when you get a callback to onScanFailed(errorCode). Then when your timer goes off to stop the scan, only actually stop it if the scanStartCount > 0.

Keep in mind that you can only minimize these messages coming from your application. Other applications on the phone doing bluetooth scanning may be causing these messages to be emitted as well.

Hickman answered 15/3, 2017 at 21:45 Comment(0)
H
4

for the same problem

I had just add permissions :

Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.
Manifest.permission.
Manifest.permission.BLUETOOTH_PRIVILEGED,

in your activity call this methods :

checkPermissions(MainActivity.this, this);


public static void checkPermissions(Activity activity, Context context){
    int PERMISSION_ALL = 1;
    String[] PERMISSIONS = {
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.BLUETOOTH,
            Manifest.permission.BLUETOOTH_ADMIN,
            Manifest.permission.BLUETOOTH_PRIVILEGED,
    };

    if(!hasPermissions(context, PERMISSIONS)){
        ActivityCompat.requestPermissions( activity, PERMISSIONS, PERMISSION_ALL);
    }
}

public static boolean hasPermissions(Context context, String... permissions) {
    if (context != null && permissions != null) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
    }
    return true;
}

hope it's help

Hangman answered 7/5, 2019 at 8:6 Comment(0)
M
2

I had the same problem with android m.It was due to lack of permissions.Make sure you go to settings and grant location permission to your app

Muskeg answered 1/4, 2017 at 9:14 Comment(0)
W
0

for location permission, only ACCESS_FINE_LOCATION worked. ACCESS_COARSE_LOCATION had the same problem.

Whity answered 3/9, 2018 at 8:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.