bluetooth lowenergy - Android BLE, can not really write characteristic -
i'm working on android project connect nexus 7 , bio sensor through ble link. problem that, can detect , list of services , characteristics of sensor. when write data specific characteristic, oncharacteristicwrite
automatically called , showed me writing operation successful. however, sensor never receive tablet. , if use similar app on iphone, works fine. there's no problem device. have idea of problem?
here code write:
private final bluetoothgattcallback mgattcallback = new bluetoothgattcallback() { @override public void onconnectionstatechange(bluetoothgatt gatt, int status, int newstate) { if (newstate == bluetoothprofile.state_connected) { mconnected = true; log.i(tag, "connected gatt server."); // attempts discover services after successful connection. log.i(tag, "attempting start service discovery:" + mbluetoothgatt.discoverservices()); } else if (newstate == bluetoothprofile.state_disconnected) { mconnected = false; log.i(tag, "disconnected gatt server."); } } @override public void onservicesdiscovered(bluetoothgatt gatt, int status) { if (status == bluetoothgatt.gatt_success) { //once detected services, write characteristic 6 times. int count =6; while(count>0){ writecharacteristic(); count--; } } else { log.w(tag, "onservicesdiscovered received: " + status); } } @override public void oncharacteristicwrite(bluetoothgatt gatt, bluetoothgattcharacteristic characteristic, int status){ if (status == bluetoothgatt.gatt_success){ log.d(tag,"write characteristic success! !"); } } }; public boolean writecharacteristic(){ //check mbluetoothgatt available if (mbluetoothadapter == null || mbluetoothgatt == null) { log.w(tag, "bluetoothadapter not initialized"); return false; } bluetoothgattservice service = mbluetoothgatt.getservice(uuid_my_service); if (service == null) { log.e(tag, "service not found!"); return false; } bluetoothgattcharacteristic characteristic = service .getcharacteristic(uuid_my_characteristic); if (characteristic == null) { log.e(tag, "char not found!"); return false; } byte[] value = {(byte)300,(byte)100,(byte)100}; characteristic.setvalue(value); boolean status = mbluetoothgatt.writecharacteristic(characteristic); return status; }
the output shows "write characteristic success! !" 6 times, writing operation succeeded. however, device shows nothing been received tablet. tried write 1 byte @ time, or add timer let tablet write sensor every 2 seconds. none of them worked. ideas?
(answered question edit. converted community wiki answer. see what appropriate action when answer question added question itself? )
the op wrote:
follow up:
the problem solved manually pairing tablet device first in setting instead of pairing code.
so using code snippet of connecting gatt provided android not enough pair device. should add code found online pair devices if don't want pair them manually every time:
private void pairdevice(bluetoothdevice device) { try { log.d("pairdevice()", "start pairing..."); method m = device.getclass() .getmethod("createbond", (class[]) null); m.invoke(device, (object[]) null); log.d("pairdevice()", "pairing finished."); } catch (exception e) { log.e("pairdevice()", e.getmessage()); } }
Comments
Post a Comment