android - Is the procedure correct to write Characteristic in Bluetooth Low Energy? -
i have button in mainactivity used write byte[]
ble device:
<button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="write char" android:id="@+id/button2" android:onclick="onclickwrite" />
and function onclickwrite
is
public void onclickwrite(view v){ if(mbluetoothleservice != null) { byte [] data= new byte[3]; data[0] = (byte)(0 & 0xff); data[1] = (byte)(255 & 0xff); data[2] = (byte)(0 & 0xff); mbluetoothleservice.senddata(data); } }
where senddata
modified function in class [bluetoothleservice][1]
. worked when press button. however, let careful @ senddata
function. searches service , characteristic again when press button. correct procedure write characteristic in ble?
public void senddata(byte[] data){ string lservice = "00001c00-d102-11e1-9b23-00025b00a5a5"; string lcharacteristic = "00001c03-d102-11e1-9b23-00025b00a5a5"; bluetoothgattservice mbluetoothleservice = null; bluetoothgattcharacteristic mbluetoothgattcharacteristic = null; (bluetoothgattservice service : mbluetoothgatt.getservices()) { if ((service == null) || (service.getuuid() == null)) { log.d("tag","something null"); continue; } if (lservice.equalsignorecase(service.getuuid().tostring())) { log.d("tag","service.getuuid().tostring()="+service.getuuid().tostring()); mbluetoothleservice = service; } } if(mbluetoothleservice!=null) { mbluetoothgattcharacteristic = mbluetoothleservice.getcharacteristic(uuid.fromstring(lrgbcharacteristic)); } else{ log.d("tag","mbluetoothleservice null"); } if(mbluetoothgattcharacteristic!=null) { mbluetoothgattcharacteristic.setvalue(data); boolean write = mbluetoothgatt.writecharacteristic(mbluetoothgattcharacteristic); log.d("tag","writecharacteristic:"+write); } else{ log.d("tag", "mbluetoothgattcharacteristic null"); } }
yes.
(filling unnecessarily required length)
Comments
Post a Comment