コード例 #1
0
        @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);
                  }
                });
          }
        }
コード例 #2
0
 private void onClientCharacteristicConfigurationDescriptorFound(
     BluetoothGattCharacteristic characteristic, BluetoothGattDescriptor descriptor) {
   Log.d();
   mBluetoothGatt.setCharacteristicNotification(characteristic, true);
   descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
   mBluetoothGatt.writeDescriptor(descriptor);
 }
コード例 #3
0
 protected Uri getRideUri() {
   if (getCallbacks() == null) {
     Log.w("null callbacks");
     return null;
   }
   return getCallbacks().getRideUri();
 }
コード例 #4
0
 private void onHeartRateMeasurementCharacteristicFound(
     BluetoothGattCharacteristic characteristic) {
   Log.d();
   boolean found = false;
   for (BluetoothGattDescriptor descriptor : characteristic.getDescriptors()) {
     Log.d("descriptor=" + descriptor.getUuid());
     if (getAssignedNumber(descriptor.getUuid())
         == GATT_DESCRIPTOR_CLIENT_CHARACTERISTIC_CONFIGURATION) {
       onClientCharacteristicConfigurationDescriptorFound(characteristic, descriptor);
       found = true;
       break;
     }
   }
   if (!found) {
     onClientCharacteristicConfigurationDescriptorNotFound();
   }
 }
コード例 #5
0
 protected void onHeartRateServiceFound(BluetoothGattService service) {
   Log.d();
   for (BluetoothGattCharacteristic characteristic : service.getCharacteristics()) {
     Log.d("characteristic=" + characteristic.getUuid());
     boolean found = false;
     if (getAssignedNumber(characteristic.getUuid())
         == GATT_CHARACTERISTIC_HEART_RATE_MEASUREMENT) {
       // Found heart read measurement characteristic
       onHeartRateMeasurementCharacteristicFound(characteristic);
       found = true;
       break;
     }
     if (!found) {
       onHeartRateMeasurementCharacteristicNotFound();
     }
   }
 }
コード例 #6
0
 @Override
 public void onServicesDiscovered(BluetoothGatt gatt, int status) {
   Log.d("status=" + LogUtil.getConstantName(BluetoothGatt.class, status, "GATT_"));
   List<BluetoothGattService> services = gatt.getServices();
   boolean found = false;
   for (BluetoothGattService service : services) {
     Log.d(service.getUuid().toString());
     if (getAssignedNumber(service.getUuid()) == GATT_SERVICE_HEART_RATE) {
       // Found heart rate service
       onHeartRateServiceFound(service);
       found = true;
       break;
     }
   }
   if (!found) {
     onHeartRateServiceNotFound();
   }
 }
コード例 #7
0
 @Override
 public void disconnect() {
   Log.d();
   if (mBluetoothGatt != null) {
     mBluetoothGatt.close();
     mBluetoothGatt.disconnect();
     mBluetoothGatt = null;
   }
   mBluetoothDevice = null;
   onDisconnect();
 }
コード例 #8
0
  @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);
  }
コード例 #9
0
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
          Log.d(
              "status="
                  + LogUtil.getConstantName(BluetoothGatt.class, status, "GATT_")
                  + " newState="
                  + LogUtil.getConstantName(BluetoothProfile.class, newState, "STATE_"));
          switch (newState) {
            case BluetoothProfile.STATE_CONNECTED:
              mBluetoothGatt.discoverServices();
              break;

            case BluetoothProfile.STATE_DISCONNECTED:
              onDisconnect();
              break;
          }
        }
コード例 #10
0
 private void onClientCharacteristicConfigurationDescriptorNotFound() {
   Log.d();
   onError();
 }
コード例 #11
0
 private void onHeartRateMeasurementCharacteristicNotFound() {
   Log.d();
   onError();
 }
コード例 #12
0
 protected void onHeartRateServiceNotFound() {
   Log.d();
   onError();
 }