Skip to content

Commit

Permalink
fix(android): do not reject disconnect call when already disconnected
Browse files Browse the repository at this point in the history
(cherry picked from commit 82c3e6e)
  • Loading branch information
pwespi committed Apr 10, 2021
1 parent fd4c7f8 commit 63808ef
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ class BluetoothLe : Plugin() {

@PluginMethod
fun disconnect(call: PluginCall) {
val device = getDevice(call) ?: return
val device = getOrCreateDevice(call) ?: return
device.disconnect { response ->
run {
if (response.success) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,11 @@ class Device(
* - request MTU
*/
fun connect(callback: (CallbackResponse) -> Unit) {
callbackMap["connect"] = callback
val key = "connect"
callbackMap[key] = callback
bluetoothGatt = device.connectGatt(context, false, gattCallback)
connectionState = STATE_CONNECTING
setConnectionTimeout("connect", "Connection timeout.", bluetoothGatt)
setConnectionTimeout(key, "Connection timeout.", bluetoothGatt)
}

fun isConnected(): Boolean {
Expand Down Expand Up @@ -234,26 +235,29 @@ class Device(
}

fun disconnect(callback: (CallbackResponse) -> Unit) {
callbackMap["disconnect"] = callback
val key = "disconnect"
callbackMap[key] = callback
if (bluetoothGatt == null) {
reject("disconnect", "Not connected to device.")
resolve(key, "Disconnected.")
return
}
bluetoothGatt?.disconnect()
setTimeout("disconnect", "Disconnection timeout.")
setTimeout(key, "Disconnection timeout.")
}

fun read(serviceUUID: UUID, characteristicUUID: UUID, callback: (CallbackResponse) -> Unit) {
val key = "read|$serviceUUID|$characteristicUUID"
callbackMap[key] = callback
val service = bluetoothGatt?.getService(serviceUUID)
val characteristic = service?.getCharacteristic(characteristicUUID)
if (characteristic != null) {
val result = bluetoothGatt?.readCharacteristic(characteristic)
if (result != true) {
reject(key, "Reading characteristic failed.")
}
} else {
if (characteristic == null) {
reject(key, "Characteristic not found.")
return
}
val result = bluetoothGatt?.readCharacteristic(characteristic)
if (result != true) {
reject(key, "Reading characteristic failed.")
return
}
setTimeout(key, "Read timeout.")
}
Expand All @@ -269,16 +273,17 @@ class Device(
callbackMap[key] = callback
val service = bluetoothGatt?.getService(serviceUUID)
val characteristic = service?.getCharacteristic(characteristicUUID)
if (characteristic != null) {
val bytes = stringToBytes(value)
characteristic.value = bytes
characteristic.writeType = writeType
val result = bluetoothGatt?.writeCharacteristic(characteristic)
if (result != true) {
reject(key, "Writing characteristic failed.")
}
} else {
if (characteristic == null) {
reject(key, "Characteristic not found.")
return
}
val bytes = stringToBytes(value)
characteristic.value = bytes
characteristic.writeType = writeType
val result = bluetoothGatt?.writeCharacteristic(characteristic)
if (result != true) {
reject(key, "Writing characteristic failed.")
return
}
setTimeout(key, "Write timeout.")
}
Expand Down

0 comments on commit 63808ef

Please sign in to comment.