[英]BLE Android write characteristic data
我正在使用Android进行BLE项目。 我想写入特征数据并将其发送到BLE芯片。
我想改写ISEN_Toulon
我使用此代码编写了特征数据,但是"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;
}
};
我也遇到过同样的问题。 解决问题的详细方式我不记得了。 但是我有一个代码可以将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();
}
};
试图覆盖回调函数
BluetoothGattCallback mGattCallback = new BluetoothGattCallback(){
//something callback function like:
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
//do something read/write
}
您需要弄清楚BLE回调函数的工作原理!!! 检查这个出来。
因为BLE写入/读取只能一次完成。 这意味着如果我从特征读取数据。 在上一个操作完成后,我只能执行另一个写/读操作。
您的情况是将多个字节的数据写入特征到蓝牙设备,该规则很少:
我知道这很老了,但是由于我今天有一个类似的问题,其他人可能希望得到答案。
您设置WriteType 3次的问题-实际上覆盖了第一个设置的值。 您的代码:
...
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.