googlearchive / android-BluetoothLeGatt

Migrated:
https://github.com/android/connectivity
Apache License 2.0
924 stars 589 forks source link

BLE scan not working #38

Closed hemapanjakshram closed 7 years ago

hemapanjakshram commented 7 years ago

07-15 23:50:47.130 13302-13302/com.example.android.bluetoothlegatt D/BluetoothAdapter: startLeScan(): null 07-15 23:50:47.132 13302-13302/com.example.android.bluetoothlegatt D/BluetoothAdapter: STATE_ON 07-15 23:50:47.134 13302-13314/com.example.android.bluetoothlegatt D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=5 07-15 23:50:47.135 13302-13314/com.example.android.bluetoothlegatt W/Binder: Caught a RuntimeException from the binder stub implementation. java.lang.SecurityException: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to get scan results at android.os.Parcel.readException(Parcel.java:1599) at android.os.Parcel.readException(Parcel.java:1552) at android.bluetooth.IBluetoothGatt$Stub$Proxy.startScan(IBluetoothGatt.java:772) at android.bluetooth.le.BluetoothLeScanner$BleScanCallbackWrapper.onClientRegistered(BluetoothLeScanner.java:324) at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:56) at android.os.Binder.execTransact(Binder.java:453) 07-15 23:50:57.129 13302-13302/com.example.android.bluetoothlegatt D/BluetoothAdapter: stopLeScan() 07-15 23:50:57.131 13302-13302/com.example.android.bluetoothlegatt D/BluetoothAdapter: STATE_ON

zeevox commented 7 years ago

Can you please try to understand the log before you post an issue?

It clearly says that

java.lang.SecurityException: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to get scan results

So that means that you need to add

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

to your AndroidManifest.xml file. Then, request permission to access location if you are on API 23+ by calling

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
      requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
}

Where PERMISSION_REQUEST_COARSE_LOCATION is any app-defined int constant random number, for example

private static final int PERMISSION_REQUEST_COARSE_LOCATION = 456;

Once the user accepts / denies the permission prompt, carry out the following code

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST_COARSE_LOCATION: {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission granted, yay! Start the Bluetooth device scan.
            } else {
                 // Alert the user that this application requires the location permission to perform the scan.
            }
        }
    }
}
NivedithaKabbur commented 7 years ago

I agree with you. I was also suggesting on adding the Location permissions in order to support the API 23+ devices. It is working for me now. Anyways, Thanks for the solution.

fmatosqg commented 7 years ago

Shouldn't this be closed after merging a pull request? I'd be glad to submit one if this is repoened.

NivedithaKabbur commented 7 years ago

34 also has a similar issue and it is open with a Pull request. It can be addressed.

fmatosqg commented 7 years ago

@NivedithaKabbur I'll try to continue the pending work on PR https://github.com/googlesamples/android-BluetoothLeGatt/pull/20, apparently the only problem is a merge conflict on gradle that should be solved with a pull rebase.