【问题标题】:Sending a byte array via BLE to an MCU通过 BLE 向 MCU 发送字节数组
【发布时间】:2018-07-03 17:59:49
【问题描述】:

我正在将字节数组的内容从 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");
            }
        }
    }

但是,如果我尝试将值设置为字节数组本身,我将无法发送信息。需要注意的是,app端没有报错,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!");
            }

【问题讨论】:

  • @Chisko 我不相信这是重复的,因为我已经在实施 gatt.writeCharacteristic() 无济于事,除非我在检查链接时错过了一些东西。你有没有特别提到的东西?

标签: java android-studio bluetooth-lowenergy gatt


【解决方案1】:

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

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

【讨论】:

  • 虽然这似乎是我应该在我的代码中考虑的问题,但我不确定这是否是阻止我的代码将信息作为单个数据包发送的实际问题.我相信这是因为我已经验证 MTU 已成功更改并且我已经能够成功地将数据作为多个数据包传输。知道为什么它不能将数据作为单个数据包发送吗?
  • 您能否更新您的问题,使代码符合我提到的要求?
  • 对不起,我今天/明天的日程安排很忙,有时间会更新我的代码。但是,似乎即使我删除 requestMtu(244) 并在 MCU 端考虑它,我仍然无法将 byteArray 写入字符,除非它是单个元素。您是否仍然认为唯一的错误是 GATT 服务器仍在处理另一个任务?感谢您的帮助,我会尽快添加等待功能
  • 你尝试写入的字节数组的长度是多少?
  • 我能够解决这个问题(见上文)。感谢您的所有帮助!
【解决方案2】:

如果您可以逐字节发送并且设备接受 MTU,那就这么简单:

mCustomService.writeCharacteristic(mWriteCharacteristic,byteArray );

mBluetoothGatt.writeCharacteristic(mWriteCharacteristic,byteArray);

要检查是否存在 MTU 大小问题,请发送一个小(

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-04
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    • 2019-01-29
    • 1970-01-01
    • 2023-03-11
    • 2014-02-14
    相关资源
    最近更新 更多