Milind Vyas
Milind Vyas

Reputation: 294

BLE startLeScan(): null

Right now I am working on BLE.I found one issue that it's showing me log of startLeScan(): null and scanning also not working I try everything for that and also search about startLeScan(): null but didn't get any solution yet.

Here is my code for BLE connection :

    private void scanLeDevice(boolean enable) {

            if (enable) {
                mBluetoothAdapter.startLeScan(mLeScanCallback);
            } else {
                mBluetoothAdapter.stopLeScan(mLeScanCallback);
            }
}

And Here is my Log at which i found this thing

01-27 13:12:21.357 4657-4657/com.icuisine D/BluetoothAdapter: stopLeScan()
01-27 13:12:23.003 4657-4657/com.icuisine D/BluetoothAdapter: startLeScan(): null
01-27 13:12:23.004 4657-4657/com.icuisine D/BluetoothAdapter: STATE_ON
01-27 13:12:23.007 4657-4668/com.icuisine D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=5

I go though with lots of websites and found that lesson is now deprecated but don't know how can I get alternate way to do scanning.

I am testing in Nexus 5 Ver Android 6.1 and Also Apply runtime permission for Bluetooth.

I know this is Duplicate question but I didn't found any solution in original one that's why i repost it!

Hope this will be sufficient details. Need Help to do this thing.

Thanks in advance.

Upvotes: 3

Views: 3538

Answers (3)

Shiv Buyya
Shiv Buyya

Reputation: 4130

Call below function checkLocationPermission() in your onCreate() of your main activity.

public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;

    public boolean checkLocationPermission() {
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {

            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.ACCESS_FINE_LOCATION)) {

                // Show an explanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.
                new AlertDialog.Builder(this)
                        .setTitle(R.string.title_location_permission)
                        .setMessage(R.string.text_location_permission)
                        .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                //Prompt the user once explanation has been shown
                                ActivityCompat.requestPermissions(DeviceControlActivity.this,
                                        new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                                        MY_PERMISSIONS_REQUEST_LOCATION);
                            }
                        })
                        .create()
                        .show();
            } else {
                // No explanation needed, we can request the permission.
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                        MY_PERMISSIONS_REQUEST_LOCATION);
            }
            return false;
        } else {
            return true;
        }
    }

Also Add below permissions in Manifest file.

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

Upvotes: 1

Berkay92
Berkay92

Reputation: 588

I encountered the same issue, I could not find the reason but when I clear cache to the Bluetooth application, my code starts working. It's worth to try.

You can follow these steps to clear Bluetooth application cache;

  1. Go to Settings
  2. Select "Apps & notifications"
  3. Click on "See all # apps"
  4. Select "Show system" option from the top right menu
  5. Find "Bluetooth" app and click on it
  6. Select the Storage option
  7. Clear cache

Upvotes: 0

Ali Shirvani
Ali Shirvani

Reputation: 570

BluetoothAdapter.startLeScan is deprecated, may be using BluetoothAdapter.bluetoothLeScanner.startScan solve the problem.

Upvotes: 1

Related Questions