@Override public void onCharacteristicChanged( BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { Log.d("characteristic=" + characteristic.getUuid()); int format; int flag = characteristic.getProperties(); if ((flag & 0x01) != 0) { format = BluetoothGattCharacteristic.FORMAT_UINT16; } else { format = BluetoothGattCharacteristic.FORMAT_UINT8; } int previousValue = mLastValue; int value = characteristic.getIntValue(format, 1); if (value < 50) { // This is probably a false measurement, consider this as a disconnect if (mStatus == Status.CONNECTED) { // Disconnect onDisconnect(); } return; } mLastValue = value; Log.d("heartRate=" + mLastValue); if (mStatus != Status.CONNECTED) { mStatus = Status.CONNECTED; // Inform listeners mListeners.dispatch( new Dispatcher<HeartRateListener>() { @Override public void dispatch(HeartRateListener listener) { listener.onConnected(); } }); } if (previousValue != mLastValue) { // Inform listeners mListeners.dispatch( new Dispatcher<HeartRateListener>() { @Override public void dispatch(HeartRateListener listener) { listener.onHeartRateChange(mLastValue); } }); } }
private void onError() { mListeners.dispatch( new Dispatcher<HeartRateListener>() { @Override public void dispatch(HeartRateListener listener) { listener.onError(); } }); }
private void onDisconnect() { mStatus = Status.DISCONNECTED; mLastValue = -1; mListeners.dispatch( new Dispatcher<HeartRateListener>() { @Override public void dispatch(HeartRateListener listener) { listener.onDisconnected(); } }); }
@Override public void setBluetoothDevice(BluetoothDevice bluetoothDevice) { Log.d(); mStatus = Status.CONNECTING; // Inform listeners mListeners.dispatch( new Dispatcher<HeartRateListener>() { @Override public void dispatch(HeartRateListener listener) { listener.onConnecting(); } }); mBluetoothDevice = bluetoothDevice; mBluetoothGatt = mBluetoothDevice.connectGatt(mContext, true, mBluetoothGattCallback); }
@Override public void removeListener(HeartRateListener listener) { mListeners.remove(listener); }
@Override public void addListener(HeartRateListener listener) { mListeners.add(listener); }