通过连接到外部BLE设备,我能够发送最多20个字节的数据.如何发送大于20个字节的数据.我已经读过,我们必须将数据分段或将特征分割为所需的部分.如果我假设我的数据是32个字节,你能否告诉我需要在我的代码中进行的更改才能使其正常工作?以下是我的代码中所需的代码段:
public boolean send(byte[] data) {
if (mBluetoothGatt == null || mBluetoothGattService == null) {
Log.w(TAG, "BluetoothGatt not initialized");
return false;
}
BluetoothGattCharacteristic characteristic =
mBluetoothGattService.getCharacteristic(UUID_SEND);
if (characteristic == null) {
Log.w(TAG, "Send characteristic not found");
return false;
}
characteristic.setValue(data);
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
return mBluetoothGatt.writeCharacteristic(characteristic);
}
Run Code Online (Sandbox Code Playgroud)
这是我用来发送数据的代码."发送"功能用于以下onclick事件.
sendValueButton = (Button) findViewById(R.id.sendValue);
sendValueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = dataEdit.getText().toString();
yableeService.send(text.getBytes());
}
});
Run Code Online (Sandbox Code Playgroud)
当String text大于20个字节时,仅接收前20个字节.如何纠正这个?
为了测试发送多个特性我试过这个:
sendValueButton = (Button) findViewById(R.id.sendValue);
sendValueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void …Run Code Online (Sandbox Code Playgroud) 根据BLE专利,BLE包中的数据大小为47字节.但是,Android只公开20个字节的数据.
从远程BLE设备读取特征时,我们面临一个问题.
这个问题发生在Android OS 5.0上面.
以下几点产生问题:
- 使一个外围设备具有一个服务和一个特征.
- 特征只有读取权限.现在设置此特征的值超过20个字符,即20个字节.
- 现在让外围设备用一个服务和一个特征来广播自己.
- 现在从市场上推出任何BLE扫描仪应用程序并连接此外围设备.
- 一旦成功连接外围设备,只需尝试读取特征.
- 在这种情况下,它不会显示任何数据,并且在调试应用程序时它会显示它返回空数据.
- 以上相同的情况不适用于Android OS 5.0及以上版本.
- 同样的情况在android 4.4中工作.
因此Android OS 5.0及更高版本中存在一些变化,内部禁用readblob()可读取超过20个字符的数据的请求.