java - 连续和同时读写ble数据通信
问题描述
我无法从 ble 设备读取数据到 android 应用程序。如果我进行连续和同时的数据通信,尽管连接了蓝牙,但其中任何一种方式都会断开通信。
我正在尝试从 ble 设备连续接收 30 字节的数据。我尝试更改 mtu 大小但没有用。如果我开始从应用程序发送 30 字节的数据,则从 ble 设备到应用程序的数据接收将被停止。我无法同时进行数据通信。有人可以帮我连续进行同步数据通信吗?我以 300ms 的速率向 ble 设备发送 30 个字节的数据,我必须以 1 秒的速率从 ble 设备接收 30 个字节的数据。我一次能够成功地读取或写入。但不是同时两个。
如果我将 mtu 大小固定为 20,那么我可以同时从 ble 设备读取 20 个字节。但我必须从 ble 设备读取 30 个字节的数据。
我也想知道,有没有可能在ble上同时读写而不会丢失数据?
我在 gatt 连接成功时正在做 gatt.requestMtu(512)。
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED) {
intentAction = ACTION_GATT_CONNECTED;
mConnectionState = STATE_CONNECTED;
broadcastUpdate(intentAction);
gatt.requestMtu(512);
Intent i = new Intent("status").putExtra("status",staticConnectionStatus);
sendBroadcast(i);
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
intentAction = ACTION_GATT_DISCONNECTED;
mConnectionState = STATE_DISCONNECTED;
Intent intent = new Intent("status");
intent.putExtra("status", staticConnectionStatus);
sendBroadcast(intent);
Log.d(TAG, "Disconnected from GATT server.");
broadcastUpdate(intentAction);
}
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
Log.d(TAG, "ACTION_DATA_AVAILABLE" + ACTION_DATA_AVAILABLE);
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
byte[] charValue = characteristic.getValue();
byte flag = charValue[0];
} else if (status == BluetoothGatt.GATT_FAILURE) {
Log.d(TAG, "failed");
}
byte[] charValue = characteristic.getValue();
byte flag = charValue[0];
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
//gatt.requestMtu(185);
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
byte[] charValue = characteristic.getValue();
byte flag = charValue[0];
}
};
public void writeRXCharacteristic(byte[] value) {
if (mBluetoothGatt != null) {
// try {
// Thread.sleep(200);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
BluetoothGattService RxService = mBluetoothGatt.getService(UUID.fromString("0000FEFB-0000-1000-8000-00805F9B34FB"));
if (RxService == null) {
// showMessage("Rx service not found!");
broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
return;
}
BluetoothGattCharacteristic RxChar = RxService.getCharacteristic(UUID.fromString("00000001-0000-1000-8000-008025000000"));
if (RxChar == null) {
// showMessage("Rx charateristic not found!");
broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
return;
}
RxChar.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
RxChar.setValue(value);
if (mBluetoothGatt != null) {
boolean status = mBluetoothGatt.writeCharacteristic(RxChar);
} else {
broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
}
} else {
broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
}
}
//Im notifying the service UUID on services discovered
BluetoothGattCharacteristic TxChar = RxService.getCharacteristic(UUID.fromString("00000002-0000-1000-8000-008025000000"));
mBluetoothGatt.setCharacteristicNotification(TxChar, true);
解决方案
下面是一些可比较的 Kotlin 代码,它们适用于串行连接并增加了 MTU 大小。我删除了错误处理、日志记录和一些特定于应用程序的代码。
它使用两个特性: -命令特性用于将数据从手机发送到设备。-反馈特性用于接收设备发送的数据。
注意它的工作方式:
- 创建一个工作连接会经历几个阶段,并等待每个阶段完成,然后再开始下一个阶段:连接、发现服务、增加 MTU。
- 发现服务后,特征将保存在实例变量中。
- 更改 MTU 回调后连接就绪。
- 更改 MTU 后启用通知。
- 启用通知是通过setCharacteristicNotification完成的。无需摆弄描述符。
import android.bluetooth.BluetoothGatt
import android.bluetooth.BluetoothGattCallback
import android.bluetooth.BluetoothGattCharacteristic
import android.bluetooth.BluetoothProfile
class BleConnection(private val device: BluetoothDevice) : Connection {
private var deviceGatt: BluetoothGatt? = null
private var commandCharacteristic: BluetoothGattCharacteristic? = null
private var feedbackCharacteristic: BluetoothGattCharacteristic? = null
private val gattCallback = object: BluetoothGattCallback() {
override fun onConnectionStateChange(gatt: BluetoothGatt?, status: Int, newState: Int) {
this@BleConnection.onConnectionStateChange(gatt, newState)
}
override fun onServicesDiscovered(gatt: BluetoothGatt?, status: Int) {
this@BleConnection.onServicesDiscovered(gatt)
}
override fun onCharacteristicChanged(gatt: BluetoothGatt?, characteristic: BluetoothGattCharacteristic?) {
this@BleConnection.onCharacteristicChanged(characteristic)
}
override fun onMtuChanged(gatt: BluetoothGatt?, mtu: Int, status: Int) {
this@BleConnection.onMtuChange(gatt)
}
}
// Start to connect
override fun connect() {
deviceGatt = device.connectGatt(null, false, gattCallback)
}
// The connection state has changed
private fun onConnectionStateChange(gatt: BluetoothGatt?, newState: Int) {
when (newState) {
BluetoothProfile.STATE_CONNECTED -> {
gatt!!.discoverServices()
}
BluetoothProfile.STATE_DISCONNECTED -> {
...
}
}
}
// GATT services have been discovered
fun onServicesDiscovered(gatt: BluetoothGatt?) {
for (service in gatt!!.services) {
if (service.uuid == Constants.SERVICE_UUID) {
feedbackCharacteristic = service.getCharacteristic(Constants.FEEDBACK_CHAR_UUID)
commandCharacteristic = service.getCharacteristic(Constants.COMMAND_CHAR_UUID)
// Increase the MTU
gatt.requestMtu(256)
}
}
}
// The MTU has successfully been changed
fun onMtuChange(gatt: BluetoothGatt?) {
gatt!!.setCharacteristicNotification(feedbackCharacteristic, true)
// ... notify that connection is fully established and ready
}
// Characteristic has been changed (i.e. new data has been received)
fun onCharacteristicChanged(characteristic: BluetoothGattCharacteristic?) {
val data = characteristic!!.value
// ... process the received data
}
// may only be called after the connection has been fully established
// (see onMtuChange() )
override fun sendData(command: ByteArray) {
commandCharacteristic!!.value = command
deviceGatt!!.writeCharacteristic(commandCharacteristic!!)
}
}
推荐阅读
- excel - Excel VSTO 插件使用 ChromiumFX 问题
- arrays - ngFor 不更新视图,除非单击视图 Angular
- sqlite - 我应该按什么顺序在 SQLite3 数据库中调用 ANALYZE 和 VACUUM?
- php - 服务器端渲染mysql限制n行并分页
- reactjs - React Native App 将无法启动 - 反应原生版本不匹配和 RCTView 为 validAttributes 生成的视图配置与原生不匹配
- android - 结束通话时出现异常 - “pjsua_conf_disconnect(id, sink.id) 错误:值或参数无效”
- amazon-web-services - AWS CDK -- 找不到模块“@aws-cdk/aws-ec2”
- java - 如何计算数组中所有非负数的平均值
- java - 仅使用 maven 部署 jar 文件
- go - 没有返回值的 Go 函数如何修改数据?