android - Android ble:无法发送大数据

我正在尝试实现一个用于在ble设备之间传输某些字符串的应用程序(目前,一台设备充当中央设备,另一台设备充当外围设备),但是没有成功。

这就是我的外围设备(服务器)的设置方式。 特征构建

fun buildCharacteristic(
    characteristicUUID: UUID,
): BluetoothGattCharacteristic {

    var properties = BluetoothGattCharacteristic.PROPERTY_READ or
                     BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE or 
                     BluetoothGattCharacteristic.PROPERTY_NOTIFY

    var permission = BluetoothGattCharacteristic.PERMISSION_READ or
                     BluetoothGattCharacteristic.PERMISSION_WRITE

    var characteristic = BluetoothGattCharacteristic(
                             characteristicUUID, 
                             properties, 
                             permission
                         )

    return characteristic

}

服务建设

fun buildService(
    serviceUUID: UUID,
    serviceType: Int,
    characteristics: List<BluetoothGattCharacteristic>
) {
    bluetoothGattService = BluetoothGattService(
                               serviceUUID, 
                               BluetoothGattService.SERVICE_TYPE_PRIMARY
                           )
    for (characteristic in characteristics) {
        bluetoothGattService.addCharacteristic(characteristic)
    }
}

这就是我启动服务器的方式(我省略了回调的实现)

 fun startServer(
    bleAdapter: BluetoothAdapter,
    btManager: BluetoothManager,
    context: Context
) {
    bleAdvertiser = bleAdapter.bluetoothLeAdvertiser

    bleGattServer = btManager.openGattServer(context, gattServerCallback)
    bleGattServer.addService(bluetoothGattService)

    var settings = AdvertiseSettings.Builder().apply {
            setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY)
            setConnectable(true)
            setTimeout(0)
            setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH)
        }

    var data = AdvertiseData.Builder().apply {
            setIncludeDeviceName(true)
    }

    bleAdvertiser.startAdvertising(settings.build(), data.build(), advertiseCallback)
}

在中央(客户端)端,当触发onScanResult时,我尝试与设备连接:

fun connectToDevice(device: BluetoothDevice) {
    device.connectGatt(
        context,
        false,
        createGattCallback()
    )
}

其中createGattCallback()是一个函数,该函数返回BluetoothGattCallback对象。 在此回调中,调用onConnectionStateChange时,我将调用服务发现,而当发现服务时,我尝试将数据写入外围设备

override fun onServicesDiscovered(gatt: BluetoothGatt?, status: Int) {
    super.onServicesDiscovered(gatt, status)
    if (gatt?.services != null) {
        var serviceFound = false
        for (service in gatt.services) {
            if (service.uuid == Consts.SERVICE_UUID) {
                serviceFound = true
                var bluetoothGattCharacteristic =    service.getCharacteristic(Consts.CHARACTERISTIC_UUID)

                writeCharacteristic(
                    gatt, 
                    bluetoothGattCharacteristic
                )

             }
         }
         if (!serviceFound) {
             gatt.disconnect()
         }
     }
}


fun writeCharacteristic(
    gatt: BluetoothGatt, 
    characteristic: BluetoothGattCharacteristic
) {

    var toSendString = "A random string for testing purpose only"
    var toSendByteArray = toSendString.toByteArray(Charsets.UTF_8)

    val chunkSize = 18
    val numberOfPackets = ceil(
        (toSendByteArray.size).toDouble() / chunkSize.toDouble()
    )

    for (i in 0 until numberOfPackets.toInt()) {
        var startIndex = i * chunkSize
        var endIndex = if (startIndex + chunkSize <= toSendByteArray.size) {
            startIndex + chunkSize
        } else {
            toSendByteArray.size
        }

        var packet = toSendByteArray.copyOfRange(startIndex, endIndex)
        characteristic.value = packet
        gatt.writeCharacteristic(characteristic)
        Thread.sleep(250)
    }
}

我的代码似乎不起作用,在外围设备上我没有收到整个字符串,而只收到了前18个字节。 我哪里错了?

您需要等待onCharacteristicWrite,然后才能发送下一个值。 请参阅Android BLE BluetoothGatt.writeDescriptor()有时返回false 而且您的睡眠无法解决任何问题。

转载请注明来自askonline.tech,本文标题:android - Android ble:无法发送大数据


 Top