BluetoothLeGatt sample, missing ACCESS_FINE_LOCATION?
In the BluetoothLeGatt sample, I am unable to see any BLE devices after the initial scan. I ran the sample on OnePlus 5T running API version 29. I had tried to run this sample with added permission ACCESS_FINE_LOCATION and after the scan, I have seen a list of available devices. I propose for this permission to be added to the Manifest and to be correctly asked for at the app launch.
Thanks @divjad---
@divjad--- pls list all the new permissions you added, because with original code, I'm unable to detect available devices.
First add this library by google: implementation 'pub.devrel:easypermissions:3.0.0'
Then Add these lines to add location permission :
private final int REQUEST_LOCATION_PERMISSION = 1;
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// Forward results to EasyPermissions
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
@AfterPermissionGranted(REQUEST_LOCATION_PERMISSION)
public void requestLocationPermission() {
String[] perms = {Manifest.permission.ACCESS_FINE_LOCATION};
if(EasyPermissions.hasPermissions(this, perms)) {
Toast.makeText(this, "Permission already granted", Toast.LENGTH_SHORT).show();
}
else {
EasyPermissions.requestPermissions(this, "Please grant the location permission", REQUEST_LOCATION_PERMISSION, perms);
}
}
Guys, I just built that project and tried to get it work, but unfortunately, even with the changes posted by khuranakaran 20h ago, I don't see any BT devices. Could you probably post the file...or send me the entire code or project?
I'm testing with a Google Pixel (1) with Android 10, Oneplus One with Android 10 and a Oneplus 7T with Android 10.... :-(
what permissions you are using ??
I'm also working on same bluetooth connect, as soon as i have a proper working code, I'll post .
what permissions you are using ??
At the moment the following should work, according to google's code samples, but it doesn't:
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
I have just added ACCESS_FINE_LOCATION and asked for it normally as in here https://developer.android.com/training/permissions/requesting without using any external library.
Have you asked permissions at run time ??
Yes, I have asked for permission at runtime.
Maybe a stupid question, but which devices should then be listed, only BLE devices, or e.g. when I run the app on my pixel, should I then see also my Oneplus One and 7T, or only a e.g. BLE device like a Heart Rate or a Temperature sensor?
My aim is to see my Xiaomi Mi Band4 and get the Heart Rate data...
This will show Classic Bluetooth devices only
This will show Classic Bluetooth devices only
Well, then I should see all my discoverable devices (notebook, smartphones, speakers,...) which I don't.
@divjad--- Could you send me your apk and/or source? Just to see if that's doing the job also on my side...
Add these lines as well, to turn on GPS as well without it your bluetooth won't detect nearby devices.
protected void createLocationRequest() { LocationRequest locationRequest = LocationRequest.create(); locationRequest.setInterval(10000); locationRequest.setFastestInterval(5000); locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
SettingsClient client = LocationServices.getSettingsClient(this);
Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
task.addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() {
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
// All location settings are satisfied. The client can initialize
// location requests here.
// ...
Toast.makeText(MainActivity2.this, "Gps already open",
Toast.LENGTH_LONG).show();
Log.d("location settings",locationSettingsResponse.toString());
}
});
task.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if (e instanceof ResolvableApiException) {
// Location settings are not satisfied, but this can be fixed
// by showing the user a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
ResolvableApiException resolvable = (ResolvableApiException) e;
resolvable.startResolutionForResult(MainActivity2.this,
11);
} catch (IntentSender.SendIntentException sendEx) {
// Ignore the error.
}
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==11){
if(resultCode==RESULT_OK){
Toast.makeText(this, "Gps opened", Toast.LENGTH_SHORT).show();
//if user allows to open gps
Log.d("result ok",data.toString());
}else if(resultCode==RESULT_CANCELED){
Toast.makeText(this, "refused to open gps",
Toast.LENGTH_SHORT).show();
// in case user back press or refuses to open gps
Log.d("result cancelled",data.toString());
}
}
}
thanks for the code @khuranakaran - I inserted it with some adaptions, but still it doesn't see any BT devices.