Skip to content

Instantly share code, notes, and snippets.

@shauvik
Last active May 6, 2019 21:22
Show Gist options
  • Save shauvik/8ebf50b5b1556346418408b02a45718c to your computer and use it in GitHub Desktop.
Save shauvik/8ebf50b5b1556346418408b02a45718c to your computer and use it in GitHub Desktop.
Bluetooth handling in FixApp
package com.fixdapp.android
import android.bluetooth.BluetoothDevice
/* Interfaces */
data class ErrorCode(val code: String)
interface SensorService {
fun scanForSensors(): List<Sensor>
fun connectToSensor(sensor: Sensor): DiagnosticInterface
}
interface Sensor {
val name: String
val macAddress: String
}
interface DiagnosticInterface {
fun readCodes(): List<ErrorCode>
}
/* Real Implementations */
class BluetoothSensor(private val bluetoothDevice: BluetoothDevice): Sensor {
override val name: String
get() = bluetoothDevice.name
override val macAddress: String
get() = bluetoothDevice.address
}
class BluetoothSensorService : SensorService {
override fun scanForSensors(): List<BluetoothSensor> {
TODO("Android OS implementation")
}
override fun connectToSensor(sensor: Sensor): DiagnosticInterface {
sensor as BluetoothSensor
TODO("Android OS implementation")
}
}
class BluetoothDiagosticInterface(private val sensor: BluetoothSensor) : DiagnosticInterface {
override fun readCodes(): List<ErrorCode> {
TODO("Android OS implementation")
}
}
/* Mock Implementations */
object MockSensor : Sensor {
override val name = "MOCK"
override val macAddress = "AA:BB:CC:DD:EE:FF"
}
class MockSensorService : SensorService {
override fun scanForSensors(): List<Sensor> = listOf(MockSensor)
override fun connectToSensor(sensor: Sensor): DiagnosticInterface = MockDiagnosticInterface()
}
class MockDiagnosticInterface : DiagnosticInterface {
override fun readCodes(): List<ErrorCode> = listOf(ErrorCode("P0100"))
}
/* Dependency Injection */
object SensorServiceProvider {
fun getSensorService(): SensorService {
return if (BuildConfig.TEST_MODE) {
MockSensorService()
} else {
BluetoothSensorService()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment