feat: support BLE low power mode
This commit is contained in:
@@ -16,8 +16,6 @@ import com.android.volley.toolbox.JsonObjectRequest
|
||||
import com.android.volley.toolbox.Volley
|
||||
import org.json.JSONException
|
||||
import org.json.JSONObject
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.ByteOrder
|
||||
import java.time.LocalDateTime
|
||||
import java.time.format.DateTimeFormatter
|
||||
import java.util.Base64
|
||||
@@ -35,6 +33,7 @@ enum class DeviceStatus {
|
||||
|
||||
@SuppressLint("MissingPermission")
|
||||
class KirbyDevice(
|
||||
|
||||
private val context: Context,
|
||||
private val connectionManager: ConnectionManager,
|
||||
private val bleDevice: BluetoothDevice,
|
||||
@@ -45,7 +44,11 @@ class KirbyDevice(
|
||||
|
||||
|
||||
) : BleListener(bleDevice.address), DeviceListEntry {
|
||||
private val tag = "KirbyDevice"
|
||||
private var lastSeen: Long = 0
|
||||
private val queue: RequestQueue = Volley.newRequestQueue(context)
|
||||
private val reconnectionDelayMs = 10_000
|
||||
private val settings = settingsRepository.getSettings()
|
||||
|
||||
fun subscribe() {
|
||||
if(statuses.contains(DeviceStatus.CONNECTED)) {
|
||||
@@ -56,8 +59,14 @@ class KirbyDevice(
|
||||
}
|
||||
|
||||
fun connect() {
|
||||
connectionManager.connect(bleDevice)
|
||||
connectionManager.discoverServices(bleDevice)
|
||||
val now = System.currentTimeMillis()
|
||||
if (now - lastSeen > reconnectionDelayMs) {
|
||||
Log.i(tag, "Connecting to device " + bleDevice.address)
|
||||
connectionManager.connect(bleDevice)
|
||||
connectionManager.discoverServices(bleDevice)
|
||||
} else{
|
||||
Log.i(tag, "Waiting before reconnecting to device " + bleDevice.address)
|
||||
}
|
||||
}
|
||||
|
||||
fun readIaq() {
|
||||
@@ -69,7 +78,9 @@ class KirbyDevice(
|
||||
gatt: BluetoothGatt,
|
||||
characteristic: BluetoothGattCharacteristic
|
||||
) {
|
||||
|
||||
addMeasurement(characteristic)
|
||||
|
||||
onStateChange(this)
|
||||
}
|
||||
|
||||
@@ -96,10 +107,18 @@ class KirbyDevice(
|
||||
statuses.remove(DeviceStatus.CONNECTED)
|
||||
statuses.remove(DeviceStatus.SUBSCRIBED)
|
||||
onStateChange(this)
|
||||
Log.i(tag, "Disconnected")
|
||||
}
|
||||
|
||||
override fun onCharChange(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic) {
|
||||
addMeasurement(characteristic)
|
||||
lastSeen = System.currentTimeMillis()
|
||||
|
||||
if(settings.lowPowerMode){
|
||||
Log.i(tag, "Received data, closing connection")
|
||||
connectionManager.teardownConnection(bleDevice)
|
||||
}
|
||||
|
||||
onStateChange(this)
|
||||
}
|
||||
|
||||
@@ -145,7 +164,7 @@ class KirbyDevice(
|
||||
private fun addMeasurement(characteristic: BluetoothGattCharacteristic) {
|
||||
val hexPayload = characteristic.value.toHexString().substring(2)
|
||||
val measurement = DecoderIaq.parseMeasurement(hexPayload)
|
||||
var payload : Payload
|
||||
val payload : Payload
|
||||
if (measurement == null) {
|
||||
payload = Payload(hexPayload)
|
||||
} else {
|
||||
@@ -167,7 +186,6 @@ class KirbyDevice(
|
||||
}
|
||||
|
||||
private fun publishMeasurement(payload: String) {
|
||||
val settings = settingsRepository.readSettings()
|
||||
val accessKey = settings.apiKey
|
||||
val url = settings.apiUrl
|
||||
|
||||
@@ -376,26 +394,11 @@ data class Payload(
|
||||
.format(DateTimeFormatter.ofPattern("dd.MM.yy HH:mm:ss"))
|
||||
)
|
||||
|
||||
fun bytesToUInt16(arr: ByteArray, start: Int): Int {
|
||||
return ByteBuffer.wrap(arr, start, 2)
|
||||
.order(ByteOrder.LITTLE_ENDIAN).short.toInt() and 0xFFFF
|
||||
}
|
||||
|
||||
fun bytesToInt16(arr: ByteArray, start: Int): Short {
|
||||
return ByteBuffer.wrap(arr, start, 2)
|
||||
.order(ByteOrder.LITTLE_ENDIAN).short
|
||||
}
|
||||
|
||||
fun bytesToInt32(arr: ByteArray, start: Int): Int {
|
||||
return ByteBuffer.wrap(arr, start, 4)
|
||||
.order(ByteOrder.LITTLE_ENDIAN).int
|
||||
}
|
||||
|
||||
|
||||
private fun payloadToMeasurements(payload: Payload): List<Measurement> {
|
||||
return listOf(object : Measurement {
|
||||
override fun getLabel(): String {
|
||||
return payload.ts.toString()
|
||||
return payload.ts
|
||||
}
|
||||
|
||||
override fun getFormattedValue(): String {
|
||||
|
||||
Reference in New Issue
Block a user