-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide write failure as property of exception (#648)
- Loading branch information
Showing
8 changed files
with
243 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package com.juul.kable | ||
|
||
import android.bluetooth.BluetoothGatt | ||
import android.bluetooth.BluetoothStatusCodes.ERROR_GATT_WRITE_NOT_ALLOWED | ||
import android.bluetooth.BluetoothStatusCodes.ERROR_GATT_WRITE_REQUEST_BUSY | ||
import android.bluetooth.BluetoothStatusCodes.ERROR_MISSING_BLUETOOTH_CONNECT_PERMISSION | ||
import android.bluetooth.BluetoothStatusCodes.ERROR_PROFILE_SERVICE_NOT_BOUND | ||
import android.bluetooth.BluetoothStatusCodes.SUCCESS | ||
import android.os.Build | ||
import com.juul.kable.AndroidPeripheral.WriteResult | ||
|
||
internal fun BluetoothGatt.discoverServicesOrThrow() { | ||
if (!discoverServices()) { | ||
throw GattRequestRejectedException() | ||
} | ||
} | ||
|
||
internal fun BluetoothGatt.setCharacteristicNotificationOrThrow( | ||
characteristic: PlatformCharacteristic, | ||
enable: Boolean, | ||
) { | ||
if (!setCharacteristicNotification(characteristic, enable)) { | ||
throw GattRequestRejectedException() | ||
} | ||
} | ||
|
||
internal fun BluetoothGatt.readCharacteristicOrThrow( | ||
characteristic: PlatformCharacteristic, | ||
) { | ||
if (!readCharacteristic(characteristic)) { | ||
throw GattRequestRejectedException() | ||
} | ||
} | ||
|
||
internal fun BluetoothGatt.readDescriptorOrThrow( | ||
descriptor: PlatformDescriptor, | ||
) { | ||
if (!readDescriptor(descriptor)) { | ||
throw GattRequestRejectedException() | ||
} | ||
} | ||
|
||
internal fun BluetoothGatt.readRemoteRssiOrThrow() { | ||
if (!readRemoteRssi()) { | ||
throw GattRequestRejectedException() | ||
} | ||
} | ||
|
||
@Suppress("DEPRECATION") | ||
internal fun BluetoothGatt.writeCharacteristicOrThrow( | ||
characteristic: PlatformCharacteristic, | ||
data: ByteArray, | ||
writeType: Int, | ||
) { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | ||
val result = writeCharacteristic(characteristic, data, writeType) | ||
if (result != SUCCESS) { | ||
throw GattWriteException(writeResultFrom(result)) | ||
} | ||
} else { | ||
characteristic.value = data | ||
characteristic.writeType = writeType | ||
if (!writeCharacteristic(characteristic)) { | ||
throw GattWriteException(WriteResult.Unknown) | ||
} | ||
} | ||
} | ||
|
||
@Suppress("DEPRECATION") | ||
internal fun BluetoothGatt.writeDescriptorOrThrow( | ||
descriptor: PlatformDescriptor, | ||
data: ByteArray, | ||
) { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | ||
val result = writeDescriptor(descriptor, data) | ||
if (result != SUCCESS) { | ||
throw GattWriteException(writeResultFrom(result)) | ||
} | ||
} else { | ||
descriptor.value = data | ||
if (!writeDescriptor(descriptor)) { | ||
throw GattRequestRejectedException() | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Possible return value of [BluetoothGatt.writeCharacteristic] or [BluetoothGatt.writeDescriptor], | ||
* yet marked as `@hide` in Android source: | ||
* https://cs.android.com/android/platform/superproject/main/+/b7a389a145ff443550e1a942bf713c60c2bd6a14:packages/modules/Bluetooth/framework/java/android/bluetooth/BluetoothStatusCodes.java;l=45-50 | ||
*/ | ||
private const val ERROR_DEVICE_NOT_CONNECTED = 4 | ||
|
||
private fun writeResultFrom(value: Int): WriteResult = when (value) { | ||
ERROR_DEVICE_NOT_CONNECTED -> WriteResult.NotConnected | ||
ERROR_GATT_WRITE_NOT_ALLOWED -> WriteResult.WriteNotAllowed | ||
ERROR_GATT_WRITE_REQUEST_BUSY -> WriteResult.WriteRequestBusy | ||
ERROR_MISSING_BLUETOOTH_CONNECT_PERMISSION -> WriteResult.MissingBluetoothConnectPermission | ||
ERROR_PROFILE_SERVICE_NOT_BOUND -> WriteResult.ProfileServiceNotBound | ||
else -> WriteResult.Unknown | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.juul.kable | ||
|
||
import android.bluetooth.BluetoothDevice | ||
import android.bluetooth.BluetoothGatt | ||
import android.os.RemoteException | ||
import com.juul.kable.AndroidPeripheral.WriteResult | ||
|
||
public class ScanFailedException internal constructor( | ||
public val errorCode: Int, | ||
) : IllegalStateException("Bluetooth scan failed with error code $errorCode") | ||
|
||
/** | ||
* Thrown when underlying [BluetoothGatt] method call returns `false`. This can occur under the | ||
* following conditions: | ||
* | ||
* - Request isn't allowed (e.g. reading a non-readable characteristic) | ||
* - Underlying service or client interface is missing or invalid (e.g. `mService == null || mClientIf == 0`) | ||
* - Associated [BluetoothDevice] is unavailable | ||
* - Device is busy (i.e. a previous request is still in-progress) | ||
* - An Android internal failure occurred (i.e. an underlying [RemoteException] was thrown) | ||
*/ | ||
public open class GattRequestRejectedException internal constructor( | ||
message: String? = null, | ||
) : BluetoothException(message) | ||
|
||
/** | ||
* Thrown when underlying [BluetoothGatt] write operation call fails. | ||
* | ||
* The reason for the failure is available via the [result] property on Android 13 (API 33) and | ||
* newer. | ||
* | ||
* On Android prior to API 33, [result] is always [Unknown][WriteResult.Unknown], but the failure | ||
* may have been due to any of the conditions listed for [GattRequestRejectedException]. | ||
*/ | ||
public class GattWriteException internal constructor( | ||
public val result: WriteResult, | ||
) : GattRequestRejectedException("Write failed: $result") |
Oops, something went wrong.