/** Write the hello input descriptor to the device */
 private void writeHelloInputCharacteristic() {
   // Set the enable/disable notification settings
   BluetoothGattCharacteristic notifyCharacteristic =
       GattUtils.getCharacteristic(
           mPickedDeviceGatt,
           Constants.HELLO_SERVICE_UUID,
           Constants.HELLO_CHARACTERISTIC_INPUT_UUID);
   int notificationState = mSpinnerHelloInput.getSelectedItemPosition();
   if (notificationState >= 0 && notificationState <= 2) {
     BluetoothGattDescriptor descriptor =
         GattUtils.getDescriptor(
             mPickedDeviceGatt,
             Constants.HELLO_SERVICE_UUID,
             Constants.HELLO_CHARACTERISTIC_INPUT_UUID,
             Constants.CLIENT_CONFIG_DESCRIPTOR_UUID);
     if (notificationState > 0) {
       mPickedDeviceGatt.setCharacteristicNotification(notifyCharacteristic, true);
       if (notificationState == 1) {
         descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
       } else if (notificationState == 2) {
         descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
       }
     } else {
       mPickedDeviceGatt.setCharacteristicNotification(notifyCharacteristic, false);
       descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
     }
     mRequestQueue.addWriteDescriptor(mPickedDeviceGatt, descriptor);
     mRequestQueue.execute();
   }
 }
Beispiel #2
0
  private static boolean setCharacteristicNotification(
      BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic characteristic, boolean enabled) {
    if (bluetoothGatt == null) {
      Log.w(TAG, "BluetoothAdapter not initialized");
      return false;
    }

    bluetoothGatt.setCharacteristicNotification(characteristic, enabled);

    try {
      BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(UUIDDes));
      if (descriptor != null) {
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        return (bluetoothGatt.writeDescriptor(descriptor));
      } else {
        Log.e(TAG, "descriptor is null");
        return false;
      }
    } catch (NullPointerException e) {
      e.printStackTrace();
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    }

    return true;
  }
 // Called when services have been discovered on the remote device.
 // It seems to be necessary to wait for this discovery to occur before
 // manipulating any services or characteristics.
 @Override
 public void onServicesDiscovered(BluetoothGatt gatt, int status) {
   super.onServicesDiscovered(gatt, status);
   if (status == BluetoothGatt.GATT_SUCCESS) {
     // writeLine("Service discovery completed!");
   } else {
     // writeLine("Service discovery failed with status: " + status);
   }
   // Save reference to each characteristic.
   tx = gatt.getService(UART_UUID).getCharacteristic(TX_UUID);
   rx = gatt.getService(UART_UUID).getCharacteristic(RX_UUID);
   // Setup notifications on RX characteristic changes (i.e. data received).
   // First call setCharacteristicNotification to enable notification.
   if (!gatt.setCharacteristicNotification(rx, true)) {
     // writeLine("Couldn't set notifications for RX characteristic!");
   }
   // Next update the RX characteristic's client descriptor to enable notifications.
   if (rx.getDescriptor(CLIENT_UUID) != null) {
     BluetoothGattDescriptor desc = rx.getDescriptor(CLIENT_UUID);
     desc.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
     if (!gatt.writeDescriptor(desc)) {
       // writeLine("Couldn't write RX client descriptor value!");
     }
   } else {
     // writeLine("Couldn't get RX client descriptor!");
   }
 }
 private void onClientCharacteristicConfigurationDescriptorFound(
     BluetoothGattCharacteristic characteristic, BluetoothGattDescriptor descriptor) {
   Log.d();
   mBluetoothGatt.setCharacteristicNotification(characteristic, true);
   descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
   mBluetoothGatt.writeDescriptor(descriptor);
 }
  /**
   * Enables or disables notification on a give characteristic.
   *
   * @param characteristic Characteristic to act on.
   * @param enabled If true, enable notification. False otherwise.
   */
  public void setCharacteristicNotification(
      BluetoothGattCharacteristic characteristic, boolean enabled) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
      Log.w(TAG, "BluetoothAdapter not initialized");
      return;
    }
    mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

    // This is specific to Heart Rate Measurement.
    if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
      BluetoothGattDescriptor descriptor =
          characteristic.getDescriptor(
              UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
      descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
      mBluetoothGatt.writeDescriptor(descriptor);
    }

    // specific for pm2.5
    if (UUID_FFE0.equals(characteristic.getUuid())) {
      BluetoothGattDescriptor descriptor =
          characteristic.getDescriptor(
              UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
      descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
      mBluetoothGatt.writeDescriptor(descriptor);
    }
  }
  public void removeNotify(
      UUID serviceUUID, UUID characteristicUUID, Callback success, Callback fail) {

    Log.d(LOG_TAG, "removeNotify");

    if (gatt == null) {
      fail.invoke("BluetoothGatt is null");
      return;
    }

    BluetoothGattService service = gatt.getService(serviceUUID);
    BluetoothGattCharacteristic characteristic =
        findNotifyCharacteristic(service, characteristicUUID);
    // String key = generateHashKey(serviceUUID, characteristic);

    if (characteristic != null) {

      if (gatt.setCharacteristicNotification(characteristic, false)) {
        success.invoke();
      } else {
        // TODO we can probably ignore and return success anyway since we removed the notification
        // callback
        fail.invoke("Failed to stop notification for " + characteristicUUID);
      }

    } else {
      fail.invoke("Characteristic " + characteristicUUID + " not found");
    }
  }
Beispiel #7
0
        /*
         * Enable notification of changes on the data characteristic for each sensor
         * by writing the ENABLE_NOTIFICATION_VALUE flag to that characteristic's
         * configuration descriptor.
         */
        private void setNotifyNextSensor(BluetoothGatt gatt) {
          BluetoothGattCharacteristic characteristic;
          switch (mState) {
            case 0:
              Log.d(TAG, "Set notify on Data characteristic");
              characteristic = gatt.getService(MAIN_SERVICE).getCharacteristic(DATA_CHAR);
              break;

            default:
              // mHandler.sendEmptyMessage(MSG_DISMISS);
              Log.i(TAG, "All Sensors Enabled");
              reset();
              return;
          }

          bytesp1 = new ArrayList<>();
          bytesp2 = new ArrayList<>();
          bytesv = new ArrayList<>();

          // Enable local notifications
          gatt.setCharacteristicNotification(characteristic, true);
          // Enabled remote notifications
          BluetoothGattDescriptor desc = characteristic.getDescriptor(CONFIG_DESCRIPTOR);
          desc.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
          gatt.writeDescriptor(desc);
        }
  private void enableNotifications(BluetoothGatt gatt, UUID serviceUuid, UUID dataUuid) {
    UUID CCC = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");

    BluetoothGattService service = gatt.getService(serviceUuid);
    BluetoothGattCharacteristic dataCharacteristic = service.getCharacteristic(dataUuid);
    gatt.setCharacteristicNotification(dataCharacteristic, true); // Enabled locally

    BluetoothGattDescriptor config = dataCharacteristic.getDescriptor(CCC);
    config.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    gatt.writeDescriptor(config); // Enabled remotely
  }
Beispiel #9
0
 @Override
 public void onServicesDiscovered(final BluetoothGatt gatt, final int status) {
   Log.i(tag, "onServicesDiscovered");
   BluetoothGattCharacteristic characteristic =
       gatt.getService(SERVICE).getCharacteristic(CHARACTERISTIC);
   if (characteristic != null) {
     gatt.setCharacteristicNotification(characteristic, true);
     bluetoothGatt = gatt;
     characFound = true;
   } else characFound = false;
   connected = true;
 }
 private boolean checkCloudWatchServiceSupported(BluetoothGatt gatt) {
   BluetoothGattService cloudWatchGattService =
       gatt.getService(UUID.fromString(BleUuid.SERVICE_DEVICE_INFORMATION));
   if (cloudWatchGattService == null) {
     LogTool.e(TAG, "Cloud Watch Service不支持");
   } else {
     BluetoothGattCharacteristic rfcommCharacteristic =
         cloudWatchGattService.getCharacteristic(
             UUID.fromString(BleUuid.CHAR_MANUFACTURER_NAME_STRING));
     if (rfcommCharacteristic == null) {
       LogTool.e(TAG, "RFCOMM BLUETOOTH COMMAND不支持");
     } else {
       gatt.setCharacteristicNotification(rfcommCharacteristic, true);
     }
     BluetoothGattCharacteristic heartbeatCharacteristic =
         cloudWatchGattService.getCharacteristic(
             UUID.fromString(BleUuid.CHAR_MODEL_NUMBER_STRING));
     if (heartbeatCharacteristic == null) {
       LogTool.e(TAG, "HEARTBEAT OR DISCONNECT COMMAND不支持");
     } else {
       gatt.setCharacteristicNotification(heartbeatCharacteristic, true);
     }
   }
   BluetoothGattService currentTimeGattService =
       gatt.getService(UUID.fromString(BleUuid.SERVICE_CURRENT_TIME));
   if (currentTimeGattService == null) {
     LogTool.e(TAG, "Current Time Service不支持");
   } else {
     BluetoothGattCharacteristic syncTimeCharacteristic =
         currentTimeGattService.getCharacteristic(UUID.fromString(BleUuid.CHAR_CURRENT_TIME));
     if (syncTimeCharacteristic == null) {
       LogTool.e(TAG, "SYNCTIME COMMAND不支持");
     } else {
       gatt.setCharacteristicNotification(syncTimeCharacteristic, true);
     }
   }
   return true;
 }
 /**
  * Enables or disables notification on a give characteristic.
  *
  * @param characteristic Characteristic to act on.
  * @param enabled If true, enable notification. False otherwise.
  */
 public void setCharacteristicNotification(
     BluetoothGattCharacteristic characteristic, boolean enabled) {
   if (mBluetoothAdapter == null || mBluetoothGatt == null) {
     Log.w(TAG, "BluetoothAdapter not initialized");
     return;
   }
   mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
   BluetoothGattDescriptor descriptor =
       characteristic.getDescriptor(UUID.fromString(GattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
   if (descriptor != null) {
     System.out.println("write descriptor");
     descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
     mBluetoothGatt.writeDescriptor(descriptor);
   }
 }
  /**
   * This method tries to enable notifications on the Battery Level characteristic.
   *
   * @param enable <code>true</code> to enable battery notifications, false to disable
   * @return true if request has been sent
   */
  public boolean setBatteryNotifications(final boolean enable) {
    final BluetoothGatt gatt = mBluetoothGatt;
    if (gatt == null) {
      return false;
    }

    final BluetoothGattService batteryService = gatt.getService(BATTERY_SERVICE);
    if (batteryService == null) return false;

    final BluetoothGattCharacteristic batteryLevelCharacteristic =
        batteryService.getCharacteristic(BATTERY_LEVEL_CHARACTERISTIC);
    if (batteryLevelCharacteristic == null) return false;

    // Check characteristic property
    final int properties = batteryLevelCharacteristic.getProperties();
    if ((properties & BluetoothGattCharacteristic.PROPERTY_NOTIFY) == 0) return false;

    gatt.setCharacteristicNotification(batteryLevelCharacteristic, enable);
    final BluetoothGattDescriptor descriptor =
        batteryLevelCharacteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID);
    if (descriptor != null) {
      if (enable) {
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        Logger.a(mLogSession, "Enabling battery level notifications...");
        Logger.v(mLogSession, "Enabling notifications for " + BATTERY_LEVEL_CHARACTERISTIC);
        Logger.d(
            mLogSession,
            "gatt.writeDescriptor("
                + CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID
                + ", value=0x01-00)");
      } else {
        descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
        Logger.a(mLogSession, "Disabling battery level notifications...");
        Logger.v(mLogSession, "Disabling notifications for " + BATTERY_LEVEL_CHARACTERISTIC);
        Logger.d(
            mLogSession,
            "gatt.writeDescriptor("
                + CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID
                + ", value=0x00-00)");
      }
      return gatt.writeDescriptor(descriptor);
    }
    return false;
  }
Beispiel #13
0
  /**
   * Enables or disables notification on a give characteristic.
   *
   * @param characteristic Characteristic to act on.
   * @param enabled If true, enable notification. False otherwise.
   */
  public void setCharacteristicNotification(
      BluetoothGattCharacteristic characteristic, boolean enabled) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
      Log.w(TAG, "BluetoothAdapter not initialized");
      return;
    }
    mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

    // This is specific to Ajna Device

    if (UUID_QUATERNERION_PACKET_CHAR.equals(characteristic.getUuid())
        || UUID_QUATERNERION_DATA_CHAR.equals(characteristic.getUuid())
        || UUID_OTHER_DATA_CHAR.equals(characteristic.getUuid())) {
      BluetoothGattDescriptor descriptor =
          characteristic.getDescriptor(
              UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
      descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
      mBluetoothGatt.writeDescriptor(descriptor);
    }
  }
  // ----------------------------------------------------------------------------------------------------------------
  // Enable notification on a characteristic
  // For information only. This application uses Indication, not Notification
  public void setCharacteristicNotification(
      BluetoothGattCharacteristic characteristic, boolean enabled) {
    if (mBluetoothAdapter == null
        || mBluetoothGatt == null) { // Check that we have a GATT connection
      Log.w(TAG, "BluetoothAdapter not initialized");
      return;
    }
    mBluetoothGatt.setCharacteristicNotification(
        characteristic, enabled); // Enable notification and indication for the characteristic

    //        if (UUID_MLDP_DATA_PRIVATE_CHARACTERISTIC.equals(characteristic.getUuid())) {
    BluetoothGattDescriptor descriptor =
        characteristic.getDescriptor(
            UUID_CHARACTERISTIC_NOTIFICATION_CONFIG); // Get the descripter that enables
                                                      // notification on the server
    descriptor.setValue(
        BluetoothGattDescriptor
            .ENABLE_NOTIFICATION_VALUE); // Set the value of the descriptor to enable notification
    mBluetoothGatt.writeDescriptor(descriptor); // Write the descriptor
    //        }
  }
  private boolean enableNotification(boolean onoff, BluetoothGattCharacteristic charac) {
    if (btGatt == null) return false;

    if (!btGatt.setCharacteristicNotification(charac, onoff)) {
      System.err.println("btGatt.setCharacteristicNotification() failed");
      return false;
    }

    BluetoothGattDescriptor clientConfig = charac.getDescriptor(CCC);
    if (clientConfig == null) {
      System.err.println("clientConfig == null");
      return false;
    }

    if (onoff) {
      clientConfig.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    } else {
      clientConfig.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
    }
    return btGatt.writeDescriptor(clientConfig);
  }
  /**
   * Enables indications on given characteristic
   *
   * @return true is the request has been sent, false if one of the arguments was <code>null</code>
   *     or the characteristic does not have the CCCD.
   */
  protected final boolean enableIndications(final BluetoothGattCharacteristic characteristic) {
    final BluetoothGatt gatt = mBluetoothGatt;
    if (gatt == null || characteristic == null) return false;

    // Check characteristic property
    final int properties = characteristic.getProperties();
    if ((properties & BluetoothGattCharacteristic.PROPERTY_INDICATE) == 0) return false;

    gatt.setCharacteristicNotification(characteristic, true);
    final BluetoothGattDescriptor descriptor =
        characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID);
    if (descriptor != null) {
      descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
      Logger.v(mLogSession, "Enabling indications for " + characteristic.getUuid());
      Logger.d(
          mLogSession,
          "gatt.writeDescriptor("
              + CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID
              + ", value=0x02-00)");
      return gatt.writeDescriptor(descriptor);
    }
    return false;
  }
  // This seems way too complicated
  public void registerNotify(
      UUID serviceUUID, UUID characteristicUUID, Callback success, Callback fail) {

    Log.d(LOG_TAG, "registerNotify");

    if (gatt == null) {
      fail.invoke("BluetoothGatt is null");
      return;
    }

    BluetoothGattService service = gatt.getService(serviceUUID);
    BluetoothGattCharacteristic characteristic =
        findNotifyCharacteristic(service, characteristicUUID);
    // String key = generateHashKey(serviceUUID, characteristic);

    if (characteristic != null) {
      Log.d(LOG_TAG, "characteristic ok");

      if (gatt.setCharacteristicNotification(characteristic, true)) {

        BluetoothGattDescriptor descriptor =
            characteristic.getDescriptor(UUID.fromString(CHARACTERISTIC_NOTIFICATION_CONFIG));
        if (descriptor != null) {
          Log.d(LOG_TAG, "trovato descriptor");

          // prefer notify over indicate
          if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) {
            Log.d(LOG_TAG, "Characteristic " + characteristicUUID + " set NOTIFY");
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
          } else if ((characteristic.getProperties()
                  & BluetoothGattCharacteristic.PROPERTY_INDICATE)
              != 0) {
            Log.d(LOG_TAG, "Characteristic " + characteristicUUID + " set INDICATE");
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
          } else {
            Log.d(
                LOG_TAG,
                "Characteristic "
                    + characteristicUUID
                    + " does not have NOTIFY or INDICATE property set");
          }

          if (gatt.writeDescriptor(descriptor)) {
            // Tutto ok
            Log.d(LOG_TAG, "registerNotify completato");
            success.invoke();
          } else {
            fail.invoke(
                "Failed to set client characteristic notification for " + characteristicUUID);
          }

        } else {
          fail.invoke("Set notification failed for " + characteristicUUID);
        }

      } else {
        fail.invoke("Failed to register notification for " + characteristicUUID);
      }

    } else {
      fail.invoke("Characteristic " + characteristicUUID + " not found");
    }
  }
  private void enableNotify() {
    BluetoothGattService service = getSupportedGattService();
    if (null == service) {
      LogUtil.info(TAG, "BluetoothGattService service is null");
      boolean isconnectok = connect();
      if (!isconnectok) {
        connect();
      }
      return;
    }
    BluetoothGattCharacteristic gattCharacteristic = null;
    switch (DEVICE_TYPE) {
      case 1:
        gattCharacteristic =
            service.getCharacteristic(UUID.fromString(BLEConstants.UUID_CHARACTERISTIC));
        break;
      case 2:
        gattCharacteristic =
            service.getCharacteristic(
                UUID.fromString(BLEConstants.UUID_BLOOD_PRESSURE_CHARACTERISTIC));
        break;
      case 3:
        gattCharacteristic =
            service.getCharacteristic(UUID.fromString(BLEConstants.UUID_BLOODSUGAR_CHARACTERISTIC));
        break;
      default:
        gattCharacteristic =
            service.getCharacteristic(UUID.fromString(BLEConstants.UUID_CHARACTERISTIC));
        break;
    }
    if (null == gattCharacteristic) {
      LogUtil.info(TAG, "BluetoothGattCharacteristic gattCharacteristic is null");
      return;
    }

    boolean bool = mBluetoothGatt.setCharacteristicNotification(gattCharacteristic, true);
    LogUtil.info(TAG, "setCharacteristicNotification:" + bool);
    BluetoothGattDescriptor descriptor = null;
    switch (DEVICE_TYPE) {
      case 1:
        descriptor =
            gattCharacteristic.getDescriptor(UUID.fromString(BLEConstants.UUID_DESCRIPTER));
      case 2:
        descriptor =
            gattCharacteristic.getDescriptor(
                UUID.fromString(BLEConstants.UUID_BLOOD_PRESSURE_DESCRIPTER));
        break;
      case 3:
        descriptor =
            gattCharacteristic.getDescriptor(
                UUID.fromString(BLEConstants.UUID_BLOODSUGAR_DESCRIPTER));
        break;

      default:
        descriptor =
            gattCharacteristic.getDescriptor(UUID.fromString(BLEConstants.UUID_DESCRIPTER));
        break;
    }
    if (null != descriptor) {
      descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
      mBluetoothGatt.writeDescriptor(descriptor);
    }
  }