LeandroSQ / android-ble-made-easy

An Android Library for handling Bluetooth Low Energy on Android Easy
MIT License
88 stars 29 forks source link

Can not read from Characteristic #10

Closed dononcharles closed 2 years ago

dononcharles commented 2 years ago

Hi, I find very interesting your library but am facing some issue. What i want to do:

I want to read the fullname cause during the scanning process, i got only 8 bytes of the name.

But when i passe the characteristic to it, instead of reading the fullname of the devices, it sends "null"

// call in fragment
fun startScanning(bl: BLE, scope: CoroutineScope) {
        val scanFilterList = mutableListOf<ScanFilter>()
        scanFilterList.add(ScanFilter.Builder().setServiceUuid(ParcelUuid(SERVICE_UUID), parcelUuidMask).build())

        val scanSettings = ScanSettings.Builder()
            .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
            .setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES)
            .setNumOfMatches(ScanSettings.MATCH_NUM_ONE_ADVERTISEMENT)
            .setMatchMode(ScanSettings.MATCH_MODE_AGGRESSIVE).build()
        scope.launch {
            bl.scanAsync(
                filters = scanFilterList,
                settings = scanSettings,
                duration = 20000,
                onDiscover = { device ->
                    Logger.warn("BT: $device")
                    connectId(bl, device)
                },
                onFinish = { devices ->
                    Logger.warn("BTS: $devices")
                },
                onError = { errorCode ->
                    Logger.warn("ERROR: $errorCode")
                }
            )
        }
    }
private fun connectId(bl: BLE, device: BLEDevice) {
        presenterScope.launch {
            bl.connect(device)?.let { connection ->
                if (connection.isActive) {
                    //connection.write(AllGattCharacteristics.DEVICE_NAME, byteArrayOf(0))
                    val bytes = connection.read(AllGattCharacteristics.DEVICE_NAME)
                    Logger.warn("BT_: ${bytes?.let { String(it) }}") // it sends null instead of the device name
                }
                connection.close()
            }
        }
    }

do you think, what am trying to archive can work?

Thx