diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 710ee85e4e..e09f297a7b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1549,10 +1549,6 @@ STOP Selected RileyLink Scan - Bluetooth Low Energy not supported. - Bluetooth not enabled. - Location Is Not Enabled - For Bluetooth discovery to work on newer devices, location must be enabled. AAPS does not track your location and it can be disabled after pairing is successful. Enable No Scanning diff --git a/core/build.gradle b/core/build.gradle index 0a6122fa64..55829c53d1 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -42,7 +42,7 @@ dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'androidx.appcompat:appcompat:1.1.0' - implementation 'androidx.core:core-ktx:1.2.0' + implementation "androidx.preference:preference-ktx:1.1.1" implementation "com.google.dagger:dagger-android:$dagger_version" implementation "com.google.dagger:dagger-android-support:$dagger_version" diff --git a/core/src/main/AndroidManifest.xml b/core/src/main/AndroidManifest.xml index 46d6d492d4..0ff2fcd7b8 100644 --- a/core/src/main/AndroidManifest.xml +++ b/core/src/main/AndroidManifest.xml @@ -1,2 +1,5 @@ + package="info.nightscout.androidaps.core" > + + + diff --git a/core/src/main/java/info/nightscout/androidaps/plugins/pump/common/ble/BlePreCheck.kt b/core/src/main/java/info/nightscout/androidaps/plugins/pump/common/ble/BlePreCheck.kt new file mode 100644 index 0000000000..497ec61fbc --- /dev/null +++ b/core/src/main/java/info/nightscout/androidaps/plugins/pump/common/ble/BlePreCheck.kt @@ -0,0 +1,87 @@ +package info.nightscout.androidaps.plugins.pump.common.ble + +import android.Manifest +import android.bluetooth.BluetoothAdapter +import android.content.Context +import android.content.Intent +import android.content.pm.PackageManager +import android.location.LocationManager +import android.provider.Settings +import androidx.appcompat.app.AppCompatActivity +import androidx.core.app.ActivityCompat +import androidx.core.content.ContextCompat +import info.nightscout.androidaps.core.R +import info.nightscout.androidaps.utils.alertDialogs.OKDialog +import info.nightscout.androidaps.utils.resources.ResourceHelper +import javax.inject.Inject +import javax.inject.Singleton + +@Singleton +class BlePreCheck @Inject constructor( + val resourceHelper: ResourceHelper +) { + + companion object { + private const val PERMISSION_REQUEST_COARSE_LOCATION = 30241 // arbitrary. + } + + fun prerequisitesCheck(activity: AppCompatActivity): Boolean { + if (!activity.packageManager.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { + OKDialog.show(activity, resourceHelper.gs(R.string.message), resourceHelper.gs(R.string.rileylink_scanner_ble_not_supported)) + return false + } else { + // Use this check to determine whether BLE is supported on the device. Then + // you can selectively disable BLE-related features. + if (ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { + // your code that requires permission + ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION), PERMISSION_REQUEST_COARSE_LOCATION) + } + + val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter() + // Ensures Bluetooth is available on the device and it is enabled. If not, + // displays a dialog requesting user permission to enable Bluetooth. + if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled) { + OKDialog.show(activity, resourceHelper.gs(R.string.message), resourceHelper.gs(R.string.rileylink_scanner_ble_not_enabled)) + return false + } else { + // Will request that GPS be enabled for devices running Marshmallow or newer. + if (!isLocationEnabled(activity)) { + requestLocation(activity) + return false + } + } + } + return true + } + + /** + * Determine if GPS is currently enabled. + * + * + * On Android 6 (Marshmallow), location needs to be enabled for Bluetooth discovery to work. + * + * @param context The current app context. + * @return true if location is enabled, false otherwise. + */ + private fun isLocationEnabled(context: Context): Boolean { + val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager + return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || + locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) + } + + /** + * Prompt the user to enable GPS location if it isn't already on. + * + * @param activity The currently visible activity. + */ + private fun requestLocation(activity: AppCompatActivity) { + if (isLocationEnabled(activity)) { + return + } + + // Shamelessly borrowed from http://stackoverflow.com/a/10311877/868533 + OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.location_not_found_title), resourceHelper.gs(R.string.location_not_found_message), Runnable { + activity.startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)) + }) + } +} \ No newline at end of file diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index b89cf096bd..a0d615ea71 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -7,4 +7,9 @@ Cancel DISMISS + Bluetooth Low Energy not supported. + Bluetooth not enabled. + Location Is Not Enabled + For Bluetooth discovery to work on newer devices, location must be enabled. AAPS does not track your location and it can be disabled after pairing is successful. + \ No newline at end of file