My device.connectGatt() only triggers onConnectionStateChage. The status is 0, and the connection is established. I tested it on a 4.4 and 5.1 system - with the same result. This is my 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 to send some data to the 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 a break-point to all the calls and the only one that gets hit is the onConnectionStateChange. Is this supposed to work like this? Where do I get to write either a characteristic or a description and push it to the BLE module? Also, the BLE module (HC-10 controlled by an Arduino) is sending data out (tested it with a different app) ... so I would expect it to hit the onCharacteristicOnChage method as well. What am I missing ?
Upon connection, Android is not aware of any services or characteristics on the device.
Hence, you must discoverServices()
once connected:
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
gatt.discoverServices();
}
}
Once you get a callback, you can do a 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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With