diff --git a/rxbluetooth/src/main/java/com/github/ivbaranov/rxbluetooth/RxBluetooth.java b/rxbluetooth/src/main/java/com/github/ivbaranov/rxbluetooth/RxBluetooth.java index 5044865..331cf73 100644 --- a/rxbluetooth/src/main/java/com/github/ivbaranov/rxbluetooth/RxBluetooth.java +++ b/rxbluetooth/src/main/java/com/github/ivbaranov/rxbluetooth/RxBluetooth.java @@ -25,7 +25,7 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; -import android.os.Build; +import android.content.pm.PackageManager; import android.text.TextUtils; import com.github.ivbaranov.rxbluetooth.events.AclEvent; import com.github.ivbaranov.rxbluetooth.events.BondStateEvent; @@ -43,6 +43,9 @@ import java.util.UUID; import java.util.concurrent.Callable; +import static android.Manifest.permission.ACCESS_COARSE_LOCATION; +import static android.os.Build.VERSION.SDK_INT; + /** * Enables clients to listen to bluetooth events using RxJava Observables. */ @@ -77,6 +80,20 @@ public boolean isBluetoothEnabled() { return bluetoothAdapter.isEnabled(); } + /** + * Return true if Location permission is granted. + * + * @return true if the local permission is granted. Pre 23 it will always return true. Post 22 + * it will ask the Context whether the permission has been granted or not. + */ + public boolean isLocationPermissionGranted() { + if (SDK_INT >= 23) { + return context.checkSelfPermission(ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED; + } + + return true; + } + /** * This will issue a request to enable Bluetooth through the system settings (without stopping * your application) via ACTION_REQUEST_ENABLE action Intent. @@ -513,7 +530,7 @@ public Observable observeConnectDevice(final BluetoothDevice bl try { bluetoothSocket.close(); } catch (IOException suppressed) { - if (Build.VERSION.SDK_INT >= 19) { + if (SDK_INT >= 19) { e.addSuppressed(suppressed); } }