通过BLE将字节数组发送到MCU

时间:2018-07-03 17:59:49

标签: java android-studio bluetooth-lowenergy gatt

我正在将字节数组的内容从Android移动应用程序传输到MCU。我能够成功地逐字节传输数据(多个数据包),但是我无法成功地整体发送阵列(作为一个数据包)。应当注意的是,数据将通过GATT配置文件传输,并且数组已成功传递到代码的这一部分。

    public void writeCustomUsernameCharacteristic(byte[] byteArray) {
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {
            Log.w(TAG, "BluetoothAdapter not initialized");
            return;
        }
        BluetoothGattService mCustomService = mBluetoothGatt.getService(UUID.fromString("00002a37-0000-1000-8000-00805f9b34fb"));

        if (mCustomService == null) {
            Log.w(TAG, "Custom BLE Service not found");
            return;
        }
        mBluetoothGatt.requestMtu(244); 

        for (int i = 0; i < credentials.length; i++) {
            individualBytes = byteArray[i];

            BluetoothGattCharacteristic mWriteCharacteristic = mCustomService.getCharacteristic(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb")); 
            mWriteCharacteristic.setValue(individualBytes, BluetoothGattCharacteristic.FORMAT_UINT8, 0);  
            mBluetoothGatt.writeCharacteristic(mWriteCharacteristic);

            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                System.out.println("got interrupted!");
            }
            if (mBluetoothGatt.writeCharacteristic(mWriteCharacteristic) == false) {
                Log.w(TAG, "Failed to write characteristic");
            }
        }
    }

但是,如果我尝试将值设置为字节数组本身,则无法发送信息。应该注意的是,在应用程序端没有报告错误,并且MCU端没有报告任何接收到的数据包。

public void writeCustomUsernameCharacteristic(byte[] byteArray) {
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {
            Log.w(TAG, "BluetoothAdapter not initialized");
            return;
        }
        BluetoothGattService mCustomService = mBluetoothGatt.getService(UUID.fromString("4880c12c-fdcb-4077-8920-a450d7f9b907"));

        if (mCustomService == null) {
            Log.w(TAG, "Custom BLE Service not found");
            return;
        }
        mBluetoothGatt.requestMtu(244);

        BluetoothGattCharacteristic mWriteCharacteristic = mCustomService.getCharacteristic(UUID.fromString("fec26ec4-6d71-4442-9f81-55bc21d658d6"));
        mWriteCharacteristic.setValue(byteArray);
        mWriteCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
        mBluetoothGatt.writeCharacteristic(mWriteCharacteristic);
    }

谁能提供有关如何在数据包中传输此字节数组的任何建议?提前致谢。

响应可能的重复项。似乎该链接引用了我在第二个块中已经尝试过的代码。问题是 mBluetoothGatt.writeCharacteristic(mWriteCharacteristic); 没有将数据包发送到MCU。

public void writeCustomUsernameCharacteristic(byte[] byteArray) {
            if (mBluetoothAdapter == null || mBluetoothGatt == null) {
                Log.w(TAG, "BluetoothAdapter not initialized");
                return;
            }
            BluetoothGattService mCustomService = mBluetoothGatt.getService(UUID.fromString("4880c12c-fdcb-4077-8920-a450d7f9b907"));

            if (mCustomService == null) {
                Log.w(TAG, "Custom BLE Service not found");
                return;
            }
            mBluetoothGatt.requestMtu(244);

            BluetoothGattCharacteristic mWriteCharacteristic = mCustomService.getCharacteristic(UUID.fromString("fec26ec4-6d71-4442-9f81-55bc21d658d6"));
            mWriteCharacteristic.setValue(byteArray);
            mWriteCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
            mBluetoothGatt.writeCharacteristic(mWriteCharacteristic);

            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                System.out.println("got interrupted!");
            }

2 个答案:

答案 0 :(得分:1)

一次只能有一个未完成的GATT请求。您需要等待onMtuChanged回调才能执行写操作。然后,您需要等待onCharacteristicWrite回调,然后再执行一次写操作。

请参阅我在Android BLE BluetoothGatt.writeDescriptor() return sometimes false上的答案,以获得更详尽的解释。

答案 1 :(得分:0)

如果您可以按字节发送字节,并且设备接受MTU,则如下所示:

mCustomService.writeCharacteristic(mWriteCharacteristic,byteArray );

mBluetoothGatt.writeCharacteristic(mWriteCharacteristic,byteArray);

要检查是否存在MTU大小的问题,请发送一个小的(<20字节)字节数组。