我正在使用Android开展BLE项目。我想写特征数据并将其发送到BLE芯片。
我想重写ISEN_Toulon
我使用此代码编写特征数据,但数据"ISEN_Toulon"
未按预期替换为"TEST"
。
private BluetoothGattCharacteristic mWriteCharacteristic;
private final ExpandableListView.OnChildClickListener servicesListClickListner =
new ExpandableListView.OnChildClickListener()
{
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,int childPosition, long id)
{
if (mGattCharacteristics != null) {
final BluetoothGattCharacteristic characteristic = mGattCharacteristics.get(groupPosition).get(childPosition);
final int charaProp = characteristic.getProperties();
.....
// READ
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
// If there is an active notification on a characteristic, clear
// it first so it doesn't update the data field on the user interface.
if (mNotifyCharacteristic != null) {
mBluetoothLeService.setCharacteristicNotification(
mNotifyCharacteristic, false);
mNotifyCharacteristic = null;
}
mBluetoothLeService.readCharacteristic(characteristic);
Log.d("myTag", "1");
}
// NOTIFY
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
mNotifyCharacteristic = characteristic;
mBluetoothLeService.setCharacteristicNotification(
characteristic, true);
Log.d("myTag", "2");
}
// WRITE
characteristic.setWriteType(BluetoothGattCharacteristic.PROPERTY_WRITE);
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
characteristic.setWriteType(BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE);
mWriteCharacteristic = characteristic;
String str = "TEST";
byte[] strBytes = str.getBytes();
characteristic.setValue(strBytes);
writeCharacteristic(characteristic)
return true;
}
return false;
}
};
答案 0 :(得分:1)
我遇到了同样的问题。解决问题的细节方法我不记得了。但我有一个代码可以将BLE特性写入BLE设备,如下所示:(至少它对我有用)
public OnClickListener clickListener = new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (mGattCharacteristics != null) {
if(charas.size()>=6){
BluetoothGattCharacteristic characteristic =
charas.get(0);
BluetoothGattCharacteristic characteristic_write = charas.get(5);
int charaProp_write = characteristic_write.getProperties();
int charaProp = characteristic.getProperties();
//String getUUID = characteristic.getUuid().toString();
if (((charaProp_write | BluetoothGattCharacteristic.PROPERTY_WRITE) > 0)
&&
(characteristic_write != null)){
if (mNotifyCharacteristic != null) {
mBluetoothLeService.setCharacteristicNotification(
mNotifyCharacteristic, false);
mNotifyCharacteristic = null;
}
byte[] writev = new byte[SIMPLEPROFILE_CHAR6_LEN];
Arrays.fill(writev, (byte)'0');
byte[] getedit1 = edit1.getText().toString().getBytes();//edit1.getText().toString().getBytes();
byte[] getedit2 = edit2.getText().toString().getBytes();
byte[] getedit3 = edit3.getText().toString().getBytes();
//System.out.println(getedit1 + " " + getedit2 + " " + getedit3);
if ((getedit1.length + getedit2.length + getedit3.length) <= (max_syvlength+max_spvlength+max_tuvlength)) {
//this 'if' can be canceled
writev[0] = (byte)'1';
writev[1] = (byte)(getedit1.length);
writev[2] = (byte)(getedit2.length);
writev[3] = (byte)(getedit3.length);
System.arraycopy(getedit1, 0, writev, 4, getedit1.length);
System.arraycopy(getedit2, 0, writev, 4 + getedit1.length, getedit2.length);
System.arraycopy(getedit3, 0, writev, 4 + getedit1.length + getedit2.length, getedit3.length);
writev[4 + getedit1.length + getedit2.length + getedit3.length] = '@';
}
/*
for(int i = 0;i<15;i++){
Log.d(TAG, "is:" + writev[i]);
}
*/
characteristic_write.setValue(writev);
characteristic_write.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
mBluetoothLeService.writeCharacteristic(characteristic_write);
}
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
// If there is an active notification on a characteristic, clear
// it first so it doesn't update the data field on the user interface.
if (mNotifyCharacteristic != null) {
mBluetoothLeService.setCharacteristicNotification(
mNotifyCharacteristic, false);
mNotifyCharacteristic = null;
}
mBluetoothLeService.readCharacteristic(characteristic);
}
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
mNotifyCharacteristic = characteristic;
mBluetoothLeService.setCharacteristicNotification(
characteristic, true);
}
}
}
GotoMyActivity();
}
};
答案 1 :(得分:0)
尝试覆盖
中的回调函数BluetoothGattCallback mGattCallback = new BluetoothGattCallback(){
//something callback function like:
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
//do something read/write
}
您需要弄清楚BLE回调函数的工作原理!!! 检出this。
因为BLE写/读只能一次完成。这意味着如果我从特征读取数据。在上一个操作完成后,我只能执行另一个写/读操作。
答案 2 :(得分:0)
您的情况是将多个字节的数据写入特征到蓝牙设备,该规则很少:
答案 3 :(得分:0)
我知道这很老了,但是由于我今天遇到类似的问题,其他人可能希望得到答案。
您设置了3次WriteType的问题-实际上覆盖了第一个设置的两个值。 您的代码:
...
characteristic.setWriteType(BluetoothGattCharacteristic.PROPERTY_WRITE);
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
characteristic.setWriteType(BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE);
两个问题:
PROPERTY_
开头的值不是写类型,因此只有WRITE_TYPE_NO_RESPONSE
是有效的setWriteType()
的第三次调用将使用无效值覆盖有效值,然后Android堆栈将忽略您的写入请求。除非有特定原因使用WRITE_NO_RESPONSE
,否则应使用普通写入(WRITE_TYPE_DEFAULT
)。 Android堆栈有时会错过一次写操作-例如在应用程序中会调用写操作5次,但是只有1或2个写操作是通过空中发送的。即使onCharacteristicWrite()
回调报告成功也是如此。
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);