【发布时间】: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