bluetooth lowenergy - android BLE,write operation,in the function(Setcharacteristicnotification) -
if carry out write operations,it means send data phone ble. clientconfig.setvalue(bluetoothgattdescriptor.enable_notification_value) can't run,and app stop. when writes,should call setcharacteristicnotification? besides,when write,oncharacteristicwrite staus= 133.i don't understand write good.this code.
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 active notification on characteristic, clear // first doesn't update data field on 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); }
133 = 0x5;
bluetoothgatt.java
/** insufficient authentication given operation */ public static final int gatt_insufficient_authentication = 0x5;
so 133 means insufficient authentication given operation.
clientconfig can 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); }
Comments
Post a Comment