android - BluetoothGattCallback only calls onConnectionStateChange -
my device.connectgatt() triggers onconnectionstatechage. status 0, , connection established. tested on 4.4 , 5.1 system - same result. code:
private final bluetoothgattcallback mycallback = new bluetoothgattcallback() { @override public void onconnectionstatechange(bluetoothgatt gatt, int status, int newstate) { super.onconnectionstatechange(gatt, status, newstate); //bluetoothgattcharacteristic tempchar = null; //tempchar.setvalue("test"); if(status == 0) { gatt.connect(); //gatt.disconnect(); } } @override public void onservicesdiscovered(bluetoothgatt gatt, int status) { super.onservicesdiscovered(gatt, status); } @override public void oncharacteristicread(bluetoothgatt gatt, bluetoothgattcharacteristic characteristic, int status) { super.oncharacteristicread(gatt, characteristic, status); } @override public void oncharacteristicwrite(bluetoothgatt gatt, bluetoothgattcharacteristic characteristic, int status) { super.oncharacteristicwrite(gatt, characteristic, status); // try send data device characteristic.setvalue("test"); gatt.writecharacteristic(characteristic); } @override public void oncharacteristicchanged(bluetoothgatt gatt, bluetoothgattcharacteristic characteristic) { super.oncharacteristicchanged(gatt, characteristic); } @override public void ondescriptorread(bluetoothgatt gatt, bluetoothgattdescriptor descriptor, int status) { super.ondescriptorread(gatt, descriptor, status); } @override public void ondescriptorwrite(bluetoothgatt gatt, bluetoothgattdescriptor descriptor, int status) { super.ondescriptorwrite(gatt, descriptor, status); } @override public void onreliablewritecompleted(bluetoothgatt gatt, int status) { super.onreliablewritecompleted(gatt, status); } @override public void onreadremoterssi(bluetoothgatt gatt, int rssi, int status) { super.onreadremoterssi(gatt, rssi, status); } @override public void onmtuchanged(bluetoothgatt gatt, int mtu, int status) { super.onmtuchanged(gatt, mtu, status); } @override protected object clone() throws clonenotsupportedexception { return super.clone(); } @override public boolean equals(object o) { return super.equals(o); } @override protected void finalize() throws throwable { super.finalize(); } @override public int hashcode() { return super.hashcode(); } @override public string tostring() { return super.tostring(); } };
i added break-point calls , 1 gets hit onconnectionstatechange. supposed work this? write either characteristic or description , push ble module? also, ble module (hc-10 controlled arduino) sending data out (tested different app) ... expect hit oncharacteristiconchage method well. missing ?
upon connection, android not aware of services or characteristics on device.
hence, must discoverservices()
once connected:
@override public void onconnectionstatechange(bluetoothgatt gatt, int status, int newstate) { if (newstate == bluetoothprofile.state_connected) { gatt.discoverservices(); } }
once callback, can write:
@override public void onservicesdiscovered(bluetoothgatt gatt, int status) { if (status == bluetoothgatt.gatt_success) { bluetoothgattcharacteristic writechar = mbluetoothgatt.getservice(myserviceuuid) .getcharacteristic(mywritecharuuid); byte[] data = new byte[10]; writechar.setvalue(data); gatt.writecharacteristic(writechar); } }
Comments
Post a Comment