我无法从手机上读取数据到android应用程序。如果我进行连续和同时的数据通信,通过蓝牙连接的任何一种方式都是分离的。
我正在不断地尝试从ble设备接收30字节的数据。我试着改变mtu的尺寸,但没有用。如果我开始发送30字节的数据从app,数据接收从布尔设备到应用程序,将停止。我无法同时进行数据通信。有谁能帮我做连续的同步数据通信吗?我是以300毫秒的速率从sending30设备接收到的数据字节,并且我必须以1秒的速度从ble设备接收30个字节的数据,我能够成功地一次读写。但一次不能兼而有之。
如果我将mtu大小修正为20,那么我可以同时从ble设备读取20个字节。但是我必须从ble设备读取30字节的数据。
我还想知道,是否有可能在不丢失数据的情况下连续地同时读写呢?
我正在做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);发布于 2019-09-30 19:27:20
下面是一些类似的Kotlin代码,用于串行连接和增加MTU大小。我已经删除了错误处理、日志记录和一些特定于应用程序的代码。
它使用两个特征:-命令特性用于将数据从电话发送到设备。-反馈特性用于接收设备发送的数据。
注意它的工作方式:
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!!)
}
}https://stackoverflow.com/questions/58165145
复制相似问题