Skip to content

Instantly share code, notes, and snippets.

@bdivljak91
Last active April 23, 2021 05:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bdivljak91/196e31f8e87d878dc0eaa65e315f9e88 to your computer and use it in GitHub Desktop.
Save bdivljak91/196e31f8e87d878dc0eaa65e315f9e88 to your computer and use it in GitHub Desktop.
Bluetooth state broadcast receiver - Clean Architecture
import android.bluetooth.BluetoothAdapter
import android.content.BroadcastReceiver
import android.content.Intent
import android.content.Context
import dagger.android.AndroidInjection
import io.reactivex.Observable
import io.reactivex.subjects.BehaviorSubject
import javax.inject.Inject
class BluetoothStateBroadcastReceiver @Inject constructor() : BroadcastReceiver() {
private var bluetoothState: BluetoothState = BluetoothState.StateOff()
set(value) {
field = value
bluetoothStateObservable.onNext(value)
}
private var bluetoothStateObservable = BehaviorSubject.createDefault(bluetoothState)
override fun onReceive(context: Context, intent: Intent) {
AndroidInjection.inject(this, context)
val action = intent.action
if (action == BluetoothAdapter.ACTION_STATE_CHANGED) {
val state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)
when (state) {
BluetoothAdapter.STATE_OFF -> bluetoothState = BluetoothState.StateOff()
BluetoothAdapter.STATE_TURNING_OFF -> bluetoothState = BluetoothState.StateTurningOff()
BluetoothAdapter.STATE_ON -> bluetoothState = BluetoothState.StateOn()
BluetoothAdapter.STATE_TURNING_ON -> bluetoothState = BluetoothState.StateTurningOn()
}
}
}
fun setInitialBluetoothState(state: BluetoothState) {
bluetoothState = state
}
fun observeBluetoothState(): Observable<BluetoothState> = bluetoothStateObservable
}
import android.arch.lifecycle.*
import com.example.bluetoothserver.domain.interactor.bluetoothstate.BluetoothState
import com.example.bluetoothserver.domain.interactor.bluetoothstate.GetBluetoothState
import com.example.bluetoothserver.presentation.base.ScreenState
import io.reactivex.observers.DisposableObserver
import javax.inject.Inject
class BluetoothViewModel @Inject constructor(
private val getBluetoothState: GetBluetoothState
) : ViewModel() {
private val bluetoothStateLiveData = MutableLiveData<ScreenState<BluetoothState>>()
fun startObservingBluetoothConnectionState() {
getBluetoothState.invoke(object : DisposableObserver<BluetoothState>(){
override fun onNext(t: BluetoothState) {
bluetoothStateLiveData.value = ScreenState.Render(t)
}
override fun onError(e: Throwable) {
bluetoothStateLiveData.value = ScreenState.Render(BluetoothState.BluetoothStateError(e))
}
override fun onComplete() {
// Nothing
}
})
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
override fun onCleared() {
super.onCleared()
getBluetoothState.dispose()
}
fun observeBluetoothConnectionState(): LiveData<ScreenState<BluetoothState>> = bluetoothStateLiveData
}
import android.app.Application
import android.bluetooth.BluetoothAdapter
import android.content.IntentFilter
import com.example.bluetoothserver.domain.executor.PostExecutionThread
import com.example.bluetoothserver.domain.executor.ThreadExecutor
import com.example.bluetoothserver.domain.interactor.ObservableUseCase
import io.reactivex.Observable
import javax.inject.Inject
class GetBluetoothState @Inject constructor(
private val application: Application,
private val bluetoothAdapter: BluetoothAdapter,
private val bluetoothStateBroadcastReceiver: BluetoothStateBroadcastReceiver,
threadExecutor: ThreadExecutor,
postExecutionThread: PostExecutionThread
) : ObservableUseCase<BluetoothState, Nothing>(threadExecutor, postExecutionThread) {
override fun buildUseCase(params: Nothing?): Observable<BluetoothState> {
application.registerReceiver(bluetoothStateBroadcastReceiver, IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED))
if(bluetoothAdapter.isEnabled) bluetoothStateBroadcastReceiver.setInitialBluetoothState(BluetoothState.StateOn())
else bluetoothStateBroadcastReceiver.setInitialBluetoothState(BluetoothState.StateOff())
return bluetoothStateBroadcastReceiver.observeBluetoothState()
}
}
<receiver android:name=".presentation.bluetooth.BluetoothStateBroadcastReceiver" android:exported="true">
<intent-filter>
<action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
</intent-filter>
</receiver>
bluetoothViewModel?.startObservingBluetoothConnectionState()
bluetoothViewModel?.observeBluetoothConnectionState()?.observe(this, Observer { screenState ->
screenState ?: return@Observer
when (screenState) {
is ScreenState.Render -> renderBluetoothState(screenState.renderState)
}
})
private fun renderBluetoothState(renderState: BluetoothState) {
when (renderState) {
is BluetoothState.StateOff -> setUIBluetoothOff()
is BluetoothState.StateOn -> setUIBluetoothOn()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment