fix: properly handle connecting to bonded devices
This commit is contained in:
@@ -45,16 +45,18 @@ class KirbyDevice(
|
||||
private val queue: RequestQueue = Volley.newRequestQueue(context)
|
||||
|
||||
fun subscribe() {
|
||||
|
||||
connectionManager.enableNotification(
|
||||
bleDevice, SERVICE_UUID, CHAR_UUID
|
||||
)
|
||||
if(statuses.contains(DeviceStatus.CONNECTED)) {
|
||||
connectionManager.enableNotification(
|
||||
bleDevice, SERVICE_UUID, CHAR_UUID
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun readIaq() {
|
||||
connectionManager.readChar(bleDevice, SERVICE_UUID, CHAR_UUID)
|
||||
}
|
||||
|
||||
|
||||
override fun onSuccessfulCharRead(
|
||||
gatt: BluetoothGatt,
|
||||
characteristic: BluetoothGattCharacteristic
|
||||
@@ -66,13 +68,20 @@ class KirbyDevice(
|
||||
override fun onScanResult(callbackType: Int, result: ScanResult) {
|
||||
rssi = result.rssi
|
||||
onStateChange(this)
|
||||
|
||||
if(result.isConnectable) {
|
||||
connectionManager.connect(bleDevice)
|
||||
connectionManager.discoverServices(bleDevice)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onConnect(gatt: BluetoothGatt) {
|
||||
statuses.add(DeviceStatus.CONNECTED)
|
||||
statuses.remove(DeviceStatus.MISSING)
|
||||
|
||||
subscribe()
|
||||
onStateChange(this)
|
||||
|
||||
}
|
||||
|
||||
override fun onConnectToBondedFailed(gatt: BluetoothGatt) {
|
||||
@@ -136,12 +145,13 @@ class KirbyDevice(
|
||||
if (measurement == null) {
|
||||
payload = Payload(hexPayload)
|
||||
} else {
|
||||
measurement.deviceId = bleDevice.address
|
||||
payload = Payload(measurement.toString())
|
||||
Log.i("BleListener", "Char received: $payload")
|
||||
val base64Payload = Base64.getEncoder().encodeToString(characteristic.value)
|
||||
publishMeasurement(base64Payload)
|
||||
|
||||
loggerDb.writeLog( measurement)
|
||||
loggerDb.writeLog(measurement)
|
||||
}
|
||||
|
||||
measurements.add(payload)
|
||||
@@ -155,6 +165,11 @@ class KirbyDevice(
|
||||
private fun publishMeasurement(payload: String) {
|
||||
val accessKey = BuildConfig.API_KEY
|
||||
val url = BuildConfig.API_BASE_URL
|
||||
|
||||
if(url.isEmpty()) {
|
||||
return
|
||||
}
|
||||
|
||||
val eui = "0000${bleDevice.address.replace(":", "")}"
|
||||
|
||||
val postData = JSONObject()
|
||||
|
||||
Reference in New Issue
Block a user