如果我执行写操作,则表示将数据从手机发送到BLE。 ClientConfig.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE)无法运行,应用程序将停止。 当我写作时,它应该调用setCharacteristicnotification吗? 此外,当我写作时,oncharacteristicwrite staus = 133.我不明白写得很好。这是我的代码。
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();
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
if (mNotifyCharacteristic != null) {
mBluetoothLeService.setCharacteristicNotification(
mNotifyCharacteristic, false);
mNotifyCharacteristic = null;
}
mBluetoothLeService.readCharacteristic(characteristic);
}
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_WRITE) > 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.writeCharacteristic(characteristic);
}
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
mNotifyCharacteristic = characteristic;
System.out.println("kkkkkkkkkk+="+characteristic.getUuid());
mBluetoothLeService.setCharacteristicNotification(
characteristic, true);
}
tv_uuid.setText(characteristic.getUuid().toString());
Intent intent=new Intent();
b.putString("CONNET_SATE", status);
b.putString("UUID", characteristic.getUuid().toString());
intent.putExtras(b);
intent.setClass(MyGattDetail.this, FunctionActivity.class);
startActivity(intent);
return true;
}
return false;
}
};
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
boolean enabled) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
BluetoothGattDescriptor clientConfig = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
if (enabled) {
clientConfig.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
} else {
clientConfig.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
}
mBluetoothGatt.writeDescriptor(clientConfig);
}
public static void write(String s){
final int charaProp = target_chara.getProperties();
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {
//注意: 以下读取的值 通过 BluetoothGattCallback#onCharacteristicRead() 函数返回
target_chara.setValue(s);
mBluetoothLeService.writeCharacteristic(target_chara);
}
}
public void writeCharacteristic(BluetoothGattCharacteristic characteristic) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
mBluetoothGatt.writeCharacteristic(characteristic);
}
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
Log.i(TAG, "--onCharacteristicRead called--");
byte[] sucString=characteristic.getValue();
String string= new String(sucString);
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
if (mOnDataAvailableListener!=null)
mOnDataAvailableListener.onCharacteristicRead(gatt, characteristic, status);
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
System.out.println("++++++++++++++++");
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) {
// TODO Auto-generated method stub
//super.onCharacteristicWrite(gatt, characteristic, status);
Log.w(TAG, "--onCharacteristicWrite--: " + status);
//以下语句实现 发送完数据或也显示到界面上
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
答案 0 :(得分:0)
133 = 0x5;
BluetoothGatt.java
/** Insufficient authentication for a given operation */
public static final int GATT_INSUFFICIENT_AUTHENTICATION = 0x5;
所以133表示对给定操作的身份验证不足。
ClientConfig可以为null。
BluetoothGattDescriptor clientConfig = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
if(clientConfig !=null){
if (enabled) {
clientConfig.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
} else {
clientConfig.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
}
mBluetoothGatt.writeDescriptor(clientConfig);
}