kopia lustrzana https://github.com/meshtastic/Meshtastic-Android
Merge pull request #164 from KenVanHoeylandt/bluetooth-state-receiver-readability
Improved readability on BluetoothStateReceiverpull/167/head
commit
a179c46b50
|
@ -371,7 +371,7 @@ class MainActivity : AppCompatActivity(), Logging,
|
|||
updateBluetoothEnabled()
|
||||
|
||||
/// We now want to be informed of bluetooth state
|
||||
registerReceiver(btStateReceiver, btStateReceiver.intent)
|
||||
registerReceiver(btStateReceiver, btStateReceiver.intentFilter)
|
||||
|
||||
// if (!isInTestLab) - very important - even in test lab we must request permissions because we need location perms for some of our tests to pass
|
||||
requestPermission()
|
||||
|
|
|
@ -10,23 +10,21 @@ import com.geeksville.util.exceptionReporter
|
|||
/**
|
||||
* A helper class to call onChanged when bluetooth is enabled or disabled
|
||||
*/
|
||||
class BluetoothStateReceiver(val onChanged: (Boolean) -> Unit) : BroadcastReceiver() {
|
||||
val intent =
|
||||
IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED) // Can be used for registering
|
||||
class BluetoothStateReceiver(
|
||||
private val onChanged: (Boolean) -> Unit
|
||||
) : BroadcastReceiver() {
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) =
|
||||
exceptionReporter {
|
||||
if (intent.action == BluetoothAdapter.ACTION_STATE_CHANGED) {
|
||||
when (intent.getIntExtra(
|
||||
BluetoothAdapter.EXTRA_STATE,
|
||||
-1
|
||||
)) {
|
||||
// Simulate a disconnection if the user disables bluetooth entirely
|
||||
BluetoothAdapter.STATE_OFF -> onChanged(
|
||||
false
|
||||
)
|
||||
BluetoothAdapter.STATE_ON -> onChanged(true)
|
||||
}
|
||||
val intentFilter get() = IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED) // Can be used for registering
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) = exceptionReporter {
|
||||
if (intent.action == BluetoothAdapter.ACTION_STATE_CHANGED) {
|
||||
when (intent.bluetoothAdapterState) {
|
||||
// Simulate a disconnection if the user disables bluetooth entirely
|
||||
BluetoothAdapter.STATE_OFF -> onChanged(false)
|
||||
BluetoothAdapter.STATE_ON -> onChanged(true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private val Intent.bluetoothAdapterState: Int get() = getIntExtra(BluetoothAdapter.EXTRA_STATE, -1)
|
||||
}
|
|
@ -195,7 +195,7 @@ class RadioInterfaceService : Service(), Logging {
|
|||
override fun onCreate() {
|
||||
runningService = this
|
||||
super.onCreate()
|
||||
registerReceiver(bluetoothStateReceiver, bluetoothStateReceiver.intent)
|
||||
registerReceiver(bluetoothStateReceiver, bluetoothStateReceiver.intentFilter)
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
|
|
Ładowanie…
Reference in New Issue