EN: Add methods to check if advertising/scanning is supported by device

This commit is contained in:
Marvin W 2020-12-01 18:27:07 +01:00
parent 00a8e83540
commit f7190091af
No known key found for this signature in database
GPG Key ID: 072E9235DB996F2A
2 changed files with 53 additions and 0 deletions

View File

@ -232,5 +232,31 @@ class AdvertiserService : LifecycleService() {
fun isNeeded(context: Context): Boolean {
return ExposurePreferences(context).enabled
}
fun isSupported(context: Context): Boolean? {
val adapter = BluetoothAdapter.getDefaultAdapter()
return when {
adapter == null -> {
Log.d(TAG, "LE advertising not supported via adapter")
false
}
Build.VERSION.SDK_INT >= 26 && (adapter.isLeExtendedAdvertisingSupported || adapter.isLePeriodicAdvertisingSupported) -> {
Log.d(TAG, "LE advertising supported via feature")
true
}
adapter.state != BluetoothAdapter.STATE_ON -> {
Log.d(TAG, "LE advertising unknown via state")
null
}
adapter.bluetoothLeAdvertiser != null -> {
Log.d(TAG, "LE advertising supported via advertiser")
true
}
else -> {
Log.d(TAG, "LE advertising not supported via advertiser")
false
}
}
}
}
}

View File

@ -10,6 +10,7 @@ import android.annotation.TargetApi
import android.app.AlarmManager
import android.app.PendingIntent
import android.app.Service
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothAdapter.*
import android.bluetooth.le.*
import android.content.BroadcastReceiver
@ -171,5 +172,31 @@ class ScannerService : LifecycleService() {
fun isNeeded(context: Context): Boolean {
return ExposurePreferences(context).enabled
}
fun isSupported(context: Context): Boolean? {
if (AdvertiserService.isSupported(context) == true) {
Log.d(TAG, "LE scanning supported via advertiser")
return true
}
val adapter = BluetoothAdapter.getDefaultAdapter()
return when {
adapter == null -> {
Log.d(TAG, "LE scanning not supported via adapter")
false
}
adapter.state != BluetoothAdapter.STATE_ON -> {
Log.d(TAG, "LE scanning unknown via state")
null
}
adapter.bluetoothLeScanner != null -> {
Log.d(TAG, "LE scanning supported via scanner")
true
}
else -> {
Log.d(TAG, "LE scanning not supported via scanner")
false
}
}
}
}
}