3

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.

Milind Vyas
  • 294
  • 5
  • 12
  • that log message is ok. See `BluetoothAdapter.java` source code to understand why. http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/bluetooth/BluetoothAdapter.java#BluetoothAdapter.startLeScan%28java.util.UUID%5B%5D%2Candroid.bluetooth.BluetoothAdapter.LeScanCallback%29 The problem is somewhere else. Also how do you know that scanning is not working? How exactly it is not working? doesn't it find anything, or what? – Vladyslav Matviienko Jan 27 '17 at 08:10
  • Hello Vlad, thanks for your comment! I make one list of BLE device in Bluetooth callback and I didn't get any device over there from this i get that scanning is not working. this problem I face sometimes only. sometimes only found null apart from this application is working perfectly! – Milind Vyas Jan 27 '17 at 09:14
  • check this question http://stackoverflow.com/questions/33043582/bluetooth-low-energy-startscan-on-android-6-0-does-not-find-devices – Dhaiyur Jan 27 '17 at 13:53

3 Answers3

1

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

Ali Shirvani
  • 533
  • 6
  • 15
1

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" />
Shiv Buyya
  • 3,770
  • 2
  • 30
  • 25
0

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
Berkay92
  • 552
  • 6
  • 21