- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我无法从 ble 设备读取数据到 android 应用程序。如果我进行连续和同时的数据通信,无论哪种方式都会断开连接,尽管蓝牙已连接。
我正在尝试从 ble 设备连续接收 30 字节的数据。我尝试更改 mtu 大小但没有用。如果我开始从应用程序发送 30 个字节的数据,从 ble 设备到应用程序的数据接收将停止。我无法同时进行数据通信。有人可以帮我连续进行同步数据通信吗?我以 300 毫秒的速率向 ble 设备发送 30 个字节的数据,我必须以 1 秒的速率从 ble 设备接收 30 个字节的数据。我一次成功地能够读取或写入。但不是同时。
如果我将 mtu 大小固定为 20,那么我可以同时从 ble 设备读取 20 个字节。但是我必须从 ble 设备读取 30 个字节的数据。
我也想知道,有没有可能在ble上连续同时读写而不丢失数据?
我正在执行 gatt.requestMtu(512),当时 gatt 连接成功。
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 大小。我删除了错误处理、日志记录和一些特定于应用程序的代码。
它使用了两个特性: - command 特性用于将数据从手机发送到设备。 - feedback 特性用于接收设备发送的数据。
注意它的工作方式:
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!!)
}
}
关于java - 连续同步读写数据通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58165145/
我们已经通过C# winforms项目完成了SQL Server与远程MySQL(WEB)数据库(Unix平台)之间的数据通信。所有数据都通过定制软件进行更新。 我们想要的是,当我们更新、插入行或执行
我是一名优秀的程序员,十分优秀!