JuulLabs / kable

Kotlin Asynchronous Bluetooth Low-Energy

Home Page:https://juullabs.github.io/kable

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cast exception when using Peripheral.identifier when extending AndroidPeripheral for testing

rsicarelli opened this issue · comments

Context

I am getting a java.lang.ClassCastException when using the AndroidPeripheral interface for tests:

class FakeAndroidPeripheral(
    override val address: String = "43:09:19:39:99",
    ...
) : AndroidPeripheral

val identifier = FakeAndroidPeripheral().identifier

throws:

java.lang.ClassCastException: class FakeAndroidPeripheral cannot be cast to class com.juul.kable.BluetoothDeviceAndroidPeripheral (FakeAndroidPeripheral and com.juul.kable.BluetoothDeviceAndroidPeripheral are in unnamed module of loader 'app')
	at com.juul.kable.PeripheralKt.getIdentifier(Peripheral.kt:467)

Do you have any ideas on how to work around this problem?

Thanks a lot!

The identifier property is an extension function (legacy design before each platform got its own peripheral interface — which was done in Kable 0.23.0).

I'll drop the extension function and promote identifier as a property on the Peripheral interface so that it can be overridden (faked).

@rsicarelli, #509 should resolve this issue. You can test it (before it is available in the next Kable release) with:

repositories {
    maven("https://oss.sonatype.org/content/repositories/snapshots")
}

dependencies {
    implementation("com.juul.kable:core:0.24.0-issue-508-1-SNAPSHOT")
}

@twyatt Works like a charm, thanks for the quick response and fix!