public class MyGattCallback extends BluetoothGattCallback {
public MyGattCallback() {
}
/**
* Call whenever BluetoothGatt changes it's state
*
* @param gatt mGatt object
* @param status current status of Gatt
* @param newState new stata of Gatt
*/
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
EventBus.getDefault().post(new Events.DeviceConnectedEvent());
EventBus.getDefault().post(new Events.StateConnectedEvent(gatt));
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
EventBus.getDefault().post(new Events.DeviceDisconnectedEvent());
}
}
/**
* calls after new service discovered in GATT
*
* @param gatt mGatt object
* @param status current status
*/
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
EventBus.getDefault().post(new Events.ServicesDiscoveredEvent(gatt));
}
/**
* calls when write to gatt failed
*
* @param gatt mGatt object
* @param characteristic characteristic that failed
* @param status current status
*/
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
if (status != 0) {
EventBus.getDefault().post(new Events.GattWriteFailedEvent(gatt));
}
}
/**
* calls whenever wired to notification GATT service's characteristics
* changed it's own state
*
* @param gatt mGatt object
* @param characteristic changed characteristics object
*/
@Override
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic c) {
EventBus.getDefault().post(new Events.CharacteristicChangedEvent(c));
}
}