具体情况是这样,app发送数据到蓝牙板子上在没有数据返回时是没有问题的,但如果蓝牙板子上有数据返回时,app再发送数据就会把从蓝牙板子上接受到的部分数据夹杂在发送的数据之中,这是怎么回事啊,有木有大神来解答一下,可以有偿QQ940389487
蓝牙接受和发送数据应该是两个线程,数据不应该混吧,蓝牙发送和读取的代码贴出来看看
private void displayGattServices(List<BluetoothGattService> gattServices) {
if (gattServices == null)
return;
BluetoothGattCharacteristic Characteristic_cur = null;
for (BluetoothGattService gattService : gattServices) {
// -----Service的字段信息----//
int type = gattService.getType();
Log.e(TAG, "-->service type:" + Utils.getServiceType(type));
Log.e(TAG, "-->includedServices size:"
+ gattService.getIncludedServices().size());
Log.e(TAG, "-->service uuid:" + gattService.getUuid());
// -----Characteristics的字段信息----//
List<BluetoothGattCharacteristic> gattCharacteristics = gattService
.getCharacteristics();
for (final BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
Log.e(TAG, "---->char uuid:" + gattCharacteristic.getUuid());
int permission = gattCharacteristic.getPermissions();
Log.e(TAG,
"---->char permission:"
+ Utils.getCharPermission(permission));
int property = gattCharacteristic.getProperties();
Log.e(TAG,
"---->char property:"
+ Utils.getCharPropertie(property));
byte[] data = gattCharacteristic.getValue();
if (data != null && data.length > 0) {
Log.e(TAG, "---->char value:" + new String(data));
}
if (gattCharacteristic.getUuid().toString().equals(UUID_CHAR6)) {
// 把char1 保存起来�?以方便后面读写数据时使用
gattCharacteristic_char6 = gattCharacteristic;
Characteristic_cur = gattCharacteristic;
mBLE.setCharacteristicNotification(gattCharacteristic, true);
Log.i(TAG, "+++++++++UUID_CHAR6");
}
if (gattCharacteristic.getUuid().toString()
.equals(UUID_KEY_DATA)) {
// 把heartrate 保存起来�?以方便后面读写数据时使用
gattCharacteristic_keydata = gattCharacteristic;
Characteristic_cur = gattCharacteristic;
// 接受Characteristic被写的�?�?收到蓝牙模块的数据后会触发mOnDataAvailable.onCharacteristicWrite()
mBLE.setCharacteristicNotification(gattCharacteristic, true);
Log.i(TAG, "+++++++++UUID_KEY_DATA");
}
// -----Descriptors的字段信�?----//
List<BluetoothGattDescriptor> gattDescriptors = gattCharacteristic
.getDescriptors();
for (BluetoothGattDescriptor gattDescriptor : gattDescriptors) {
Log.e(TAG, "-------->desc uuid:" + gattDescriptor.getUuid());
int descPermission = gattDescriptor.getPermissions();
Log.e(TAG,
"-------->desc permission:"
+ Utils.getDescPermission(descPermission));
byte[] desData = gattDescriptor.getValue();
if (desData != null && desData.length > 0) {
Log.e(TAG, "-------->desc value:" + new String(desData));
}
}
}
}//
Intent resultIntent = new Intent();
resultIntent.putExtra("mac_name", bluetoothName);
resultIntent.putExtra("mac_addr", bluetoothAddress);
resultIntent.putExtra("char_uuid", Characteristic_cur.getUuid().toString());
setResult(2, resultIntent);
DevicesList.this.finish();//问题可能在这里,因为这一句把DeviceScanActivity给onDestroy了,所以再次调用的时候可能没有UUID_CHAR6了。
}
// 发现BLE终端的Service时回�?
mBLE.setOnServiceDiscoverListener(mOnServiceDiscover);
// 收到BLE终端数据交互的事件
mBLE.setOnDataAvailableListener(mOnDataAvailable);
//发送hex
static public void writeChar76(char send1) {
Log.i(TAG, "gattCharacteristic_char6 = " + gattCharacteristic_char6);
if (gattCharacteristic_char6 != null) {
// writeValue[0] = writeValue_char1++;
// Log.i(TAG, "gattCharacteristic_char6.setValue writeValue[0] =" +
// writeValue[0]);
boolean bRet = gattCharacteristic_char6.setValue(send1, BluetoothGattCharacteristic.FORMAT_UINT8, 0);
mBLE.writeCharacteristic(gattCharacteristic_char6);
Log.i(TAG, "yes_yes_3");
}
}
/**
* 搜索到BLE终端服务的事件
*/
private BluetoothLeClass.OnServiceDiscoverListener mOnServiceDiscover = new OnServiceDiscoverListener() {
@Override
public void onServiceDiscover(BluetoothGatt gatt) {
displayGattServices(mBLE.getSupportedGattServices());
}
};
/**
* 收到BLE终端数据交互的事件
*/
private BluetoothLeClass.OnDataAvailableListener mOnDataAvailable = new OnDataAvailableListener() {
/**
* BLE终端数据被读的事件
*/
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) {
// 执行 mBLE.readCharacteristic(gattCharacteristic); 后就会收到数�? if
// (status == BluetoothGatt.GATT_SUCCESS)
Log.e(TAG,
"onCharRead " + gatt.getDevice().getName() + " read "
+ characteristic.getUuid().toString() + " -> "
+ Utils.bytesToHexString(characteristic.getValue()));
//获取当前正在运行的activity
runningActivity = getRunningActivityName();
MainActivity.char6_display(Utils.bytesToString(characteristic
.getValue()), characteristic.getValue(), characteristic
.getUuid().toString(),runningActivity);
}
/**
* 收到BLE终端写入数据回调
*
*
*/
@Override
public void onCharacteristicWrite(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
Log.e(TAG, "onCharWrite " + gatt.getDevice().getName() + " write "
+ characteristic.getUuid().toString() + " -> "
+ new String(characteristic.getValue()));
//获取当前正在运行的activity
runningActivity = getRunningActivityName();
// OtherActivity.char6_display(Utils.bytesToHexString(characteristic.getValue()));
MainActivity.char6_display(Utils.bytesToString(characteristic
.getValue()), characteristic.getValue(), characteristic
.getUuid().toString(),runningActivity);
}
};
求帮助,大神们快来啊