sputnikdev / bluetooth-manager

Java Bluetooth Manager. A library/framework for managing bluetooth adapters, bluetooth devices, GATT services and characteristics
Apache License 2.0
95 stars 22 forks source link

Problem writing value to characteristic #16

Open asaladino opened 3 years ago

asaladino commented 3 years ago

I created an iOS app peripheral and I am trying to create a java/kotlin cli app write data to the peripheral. The kotlin cli app can see the iOS app as a peripheral but whenever I call manager.getCharacteristicGovernor(url) the characteristic is never ready. Would you be able to look at my code and see if I am missing something?

import org.sputnikdev.bluetooth.manager.CharacteristicGovernor
import org.sputnikdev.bluetooth.manager.impl.BluetoothManagerBuilder

object BmSendTest {

    private val serviceId = "56a7c54d-fd39-4efe-9e10-4d832fa64b6f".toUpperCase()
    private val characteristic = "3348f88a-4972-4125-a6fb-6c70ef459e4d".toUpperCase()

    private val SEND_VALUE = "43966666".encodeToByteArray()

    @JvmStatic
    private fun sendValue(governor: CharacteristicGovernor): Boolean {
        println("sending: $SEND_VALUE")
        return governor.write(SEND_VALUE)
    }

    @Throws(Exception::class)
    @JvmStatic
    fun main(args: Array<String>) {
        println("Starting BLE Requests")
        val manager = BluetoothManagerBuilder()
            .withTinyBTransport(true)
            .withBlueGigaTransport("^*.$")
            .withIgnoreTransportInitErrors(true)
            .withDiscovering(true)
            .withRediscover(true)
            .build()

        manager.addDeviceDiscoveryListener { discoveredDevice ->
            if (discoveredDevice.displayName == "iOS Peripheral") {
                val d = manager.getDeviceGovernor(discoveredDevice.url)
                d.connectionControl = true
                println(
                    "=== Device: ${d.displayName}," +
                            " isOnline: ${d.isOnline}," +
                            " isAuthenticated: ${d.isAuthenticated}," +
                            " isBleEnabled: ${d.isBleEnabled}," +
                            " isBlocked: ${d.isBlocked}," +
                            " isConnected: ${d.isConnected}," +
                            " isServicesResolved: ${d.isServicesResolved}," +
                            " isReady:${d.isReady}"
                )
                val url = discoveredDevice.url.copyWith(serviceId, characteristic)
                println(url)
                // ISSUE: characteristic is never ready.
                manager.getCharacteristicGovernor(url).whenReady(BmSendTest::sendValue)
            }
        }
        manager.start(true)
    }
}

Console output:

Starting BLE Requests

(process:49679): GLib-CRITICAL **: 10:20:00.860: g_variant_iter_loop: assertion 'first_time || format_string == GVSI(iter)->loop_format' failed

(process:49679): GLib-CRITICAL **: 10:20:00.860: g_variant_iter_free: assertion 'is_valid_heap_iter (iter)' failed
=== Device: iOS Peripheral, isOnline: false, isAuthenticated: false, isBleEnabled: true, isBlocked: false, isConnected: false, isServicesResolved: false, isReady:true
tinyb:/XX:XX:XX:XX:XX:XX/57:50:6A:3B:53:0A/56a7c54d-fd39-4efe-9e10-4d832fa64b6f/3348f88a-4972-4125-a6fb-6c70ef459e4d
=== Device: iOS Peripheral, isOnline: true, isAuthenticated: false, isBleEnabled: true, isBlocked: false, isConnected: true, isServicesResolved: false, isReady:true
tinyb:/XX:XX:XX:XX:XX:XX/57:50:6A:3B:53:0A/56a7c54d-fd39-4efe-9e10-4d832fa64b6f/3348f88a-4972-4125-a6fb-6c70ef459e4d

I'm running this on manjaro and eventually raspbian.

Changes I've tried to resolve the issue:

I was able to perform a test write to the peripheral using LightBlue https://punchthrough.com/lightblue/

Thanks for your help.