/**
  * Request a read on a given {@code BluetoothGattCharacteristic}. The read result is reported
  * asynchronously through the {@code
  * BluetoothGattCallback#onCharacteristicRead(android.bluetooth.BluetoothGatt,
  * android.bluetooth.BluetoothGattCharacteristic, int)} callback.
  *
  * @param characteristic The characteristic to read from.
  */
 public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
   if (mBluetoothAdapter == null || mBluetoothGatt == null) {
     Log.w(TAG, "BluetoothAdapter not initialized");
     return;
   }
   mBluetoothGatt.readCharacteristic(characteristic);
 }
  @Override
  public void onWriteToA007(byte[] data) {

    if ((data[3] == 0x72 || data[3] == 0x73) && isBtnFlag) {
      isBeginFlag = true;
      isBtnFlag = false;
    } else {
      isBeginFlag = false;
    }
    LogUtil.e(
        "send commond is:"
            + LogUtil.byte2HexString(data)
            + " ; isBtnFlag="
            + isBtnFlag
            + " ; isBeginFlag="
            + isBeginFlag);

    if (gattCharacteristics != null) {
      if (data != null && data.length > 0 && data[data.length - 1] != (byte) 0x00) {
        final BluetoothGattCharacteristic characteristic = gattCharacteristics.get(2);
        //                LogUtil.e("准备请求uuid:" + characteristic.getUuid());
        byte[] bytes = currentCmdPacket.getInputDataPacket();
        if (bytes != null) {
          characteristic.setValue(bytes);
        }
        bluetoothGatt.writeCharacteristic(characteristic);
      } else {
        final BluetoothGattCharacteristic characteristic = gattCharacteristics.get(0);
        //                LogUtil.e("准备请求uuid:" + characteristic.getUuid());
        bluetoothGatt.readCharacteristic(characteristic);
      }
    }
  }
Exemple #3
0
        /*
         * Send an enable command to each sensor by writing a configuration
         * characteristic.  This is specific to the SensorTag to keep power
         * low by disabling sensors you aren't using.
         */
        private void enableNextSensor(BluetoothGatt gatt) {
          BluetoothGattCharacteristic characteristic;
          gatt2 = gatt;
          Log.wtf("mState is", mState + "");
          switch (mState) {
            case 0:
              Log.wtf(TAG, "Renaming");
              characteristic = gatt.getService(MAIN_SERVICE).getCharacteristic(ERROR_CHAR);

              gatt.readCharacteristic(characteristic);
              break;

            case 1:
              Log.d(TAG, "Enabling pressure cal");
              characteristic = gatt.getService(MAIN_SERVICE).getCharacteristic(CONFIG_CHAR);
              characteristic.setValue(new byte[] {0x01});
              gatt.writeCharacteristic(characteristic);
              break;

            default:
              mHandler.sendEmptyMessage(MSG_DISMISS);
              Log.i(TAG, "All Sensors Enabled");
              return;
          }
        }
  // ----------------------------------------------------------------------------------------------------------------
  // Request a read of a given BluetoothGattCharacteristic. The Read result is reported
  // asynchronously through the
  // BluetoothGattCallback onCharacteristicRead callback method.
  // For information only. This application uses Indication to receive updated characteristic data,
  // not Read
  public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
    if (mBluetoothAdapter == null
        || mBluetoothGatt == null) { // Check that we have access to a Bluetooth radio
      Log.w(TAG, "BluetoothAdapter not initialized");
      return;
    }

    mBluetoothGatt.readCharacteristic(
        characteristic); // Request the BluetoothGatt to Read the characteristic
  }
  @Override
  public void onReadFromA009(byte[] data) {
    LogUtil.e("card response is:" + LogUtil.byte2HexString(data));
    if (gattCharacteristics != null) {
      if (data.length == 1 && data[0] == (byte) 0xFC) {
        // 读取完毕,最后遇到0xfc标识
        currentCmdPacket.parseOutputDataPacket(outputDatas);

        isBusy = false;

        if (!outputDatas.isEmpty()) {
          outputDatas.clear();
        }

        getCmd();
        isReady = false;

        //                final BluetoothGattCharacteristic characteristic =
        // gattCharacteristics.get(0);
        //                LogUtil.e("准备请求uuid:" + characteristic.getUuid());
        //                bluetoothGatt.readCharacteristic(characteristic);

      } else if (data.length == 3 && data[0] == 0x00) {
        // 返回错误码
        // no data return, sw return only
        // add this case for speed up, skip read FC
        byte[] swOnly = {0x01, 0x02, data[1], data[2]};
        outputDatas.add(swOnly);
        currentCmdPacket.parseOutputDataPacket(outputDatas);

        isReady = true;
        final BluetoothGattCharacteristic characteristic = gattCharacteristics.get(0);
        //                LogUtil.e("准备请求uuid:" + characteristic.getUuid());
        bluetoothGatt.readCharacteristic(characteristic);
      } else {
        // 继续读取其他数据
        outputDatas.add(data);
        final BluetoothGattCharacteristic characteristic = gattCharacteristics.get(3);
        //                LogUtil.e("准备请求uuid:" + characteristic.getUuid());
        bluetoothGatt.readCharacteristic(characteristic);
      }
    }
  }
  /**
   * Sends the read request to the given characteristic.
   *
   * @param characteristic the characteristic to read
   * @return true if request has been sent
   */
  protected final boolean readCharacteristic(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_READ) == 0) return false;

    Logger.v(mLogSession, "Reading characteristic " + characteristic.getUuid());
    Logger.d(mLogSession, "gatt.readCharacteristic(" + characteristic.getUuid() + ")");
    return gatt.readCharacteristic(characteristic);
  }
 @Override
 public void onWriteToA008(byte[] data) {
   LogUtil.e("inputData is:" + LogUtil.byte2HexString(data));
   if (gattCharacteristics != null) {
     byte[] bytes = currentCmdPacket.getInputDataPacket();
     if (bytes != null) {
       final BluetoothGattCharacteristic characteristic = gattCharacteristics.get(2);
       //                LogUtil.e("准备请求uuid:" + characteristic.getUuid());
       characteristic.setValue(bytes);
       bluetoothGatt.writeCharacteristic(characteristic);
     } else {
       final BluetoothGattCharacteristic characteristic = gattCharacteristics.get(0);
       //                LogUtil.e("准备请求uuid:" + characteristic.getUuid());
       bluetoothGatt.readCharacteristic(characteristic);
     }
   }
 }
  @Override
  public void run() {

    for (Characteristiques ch : MainActivity.propertiesToDisplay) {
      BluetoothGattCharacteristic characteristic =
          service.getCharacteristic(UUID.fromString(ch.getCharacteristic()));
      if (characteristic != null) {
        gatt.readCharacteristic(characteristic);
        try {
          Thread.sleep(200);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        displayData(characteristic, ch);
      }
    }
  }
        /*
         * from Samsung HRPService.java
         */
        private void DummyReadForSecLevelCheck(BluetoothGatt btGatt) {
          if (btGatt == null) return;

          BluetoothGattService disService = btGatt.getService(DIS_UUID);
          if (disService == null) {
            reportConnectFailed("Dis service not found");
            return;
          }
          BluetoothGattCharacteristic firmwareIdCharc =
              disService.getCharacteristic(FIRMWARE_REVISON_UUID);
          if (firmwareIdCharc == null) {
            reportConnectFailed("firmware revison charateristic not found!");
            return;
          }

          if (btGatt.readCharacteristic(firmwareIdCharc) == false) {
            reportConnectFailed("firmware revison reading is failed!");
          }
          // continue in onCharacteristicRead
        }
  public void readBatteryCharacteristic() {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
      Log.w(TAG, "BluetoothAdapter not initialized");
      return;
    }
    BluetoothGattService alarmService = null;

    BluetoothGattCharacteristic batteryCharacter = null;

    if (mBluetoothGatt != null) {
      alarmService = mBluetoothGatt.getService(BATTERY_SERVICE_UUID);
    }

    if (alarmService != null) {
      batteryCharacter =
          alarmService.getCharacteristic(UUID.fromString("00002a19-0000-1000-8000-00805f9b34fb"));
    }

    if (batteryCharacter != null) {
      mBluetoothGatt.readCharacteristic(batteryCharacter);
    }
  }
  public void read(
      UUID serviceUUID, UUID characteristicUUID, Callback successCallback, Callback failCallback) {

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

    BluetoothGattService service = gatt.getService(serviceUUID);
    BluetoothGattCharacteristic characteristic =
        findReadableCharacteristic(service, characteristicUUID);

    if (characteristic == null) {
      failCallback.invoke("Characteristic " + characteristicUUID + " not found.");
    } else {
      readCallback = successCallback;
      readFailCallback = failCallback;
      if (!gatt.readCharacteristic(characteristic)) {
        readCallback = null;
        failCallback.invoke("Read failed");
      }
    }
  }
  @Override
  public void onReadFromA006(byte[] data) {
    LogUtil.e("A006:" + LogUtil.byte2HexString(data));
    if (gattCharacteristics != null) {

      // for didGetButton
      if (isBeginFlag == true) {
        //                LogUtil.i("isBtnFlag=true && data=" + LogUtil.byte2HexString(data));
        if (data[data.length - 1] == (byte) 0x80) {
          // button pressed
          if (isReady) {
            isBusy = false;
            if (!outputDatas.isEmpty()) {
              outputDatas.clear();
            }

            getCmd();
            isReady = false;

          } else {
            final BluetoothGattCharacteristic characteristic = gattCharacteristics.get(3);
            //                LogUtil.e("准备请求uuid:" + characteristic.getUuid());
            bluetoothGatt.readCharacteristic(characteristic);
          }
        } else {
          // No button pressed yet
          new Thread() {
            @Override
            public void run() {
              try {
                Thread.sleep(100);
              } catch (InterruptedException e) {
                e.printStackTrace();
              } finally {
                final BluetoothGattCharacteristic characteristic = gattCharacteristics.get(0);
                //                            LogUtil.e("准备请求uuid:" + characteristic.getUuid());
                bluetoothGatt.readCharacteristic(characteristic);
              }
            }
          }.start();
        }

      } else {

        // 一般cmd,ff跟81都是忙碌;00 is ready;81 is button presses;
        if (data[data.length - 1] == (byte) 0xFF || data[data.length - 1] == (byte) 0x81) {
          new Thread() {
            @Override
            public void run() {
              try {
                Thread.sleep(100);
              } catch (InterruptedException e) {
                e.printStackTrace();
              } finally {
                final BluetoothGattCharacteristic characteristic = gattCharacteristics.get(0);
                //                            LogUtil.e("准备请求uuid:" + characteristic.getUuid());
                bluetoothGatt.readCharacteristic(characteristic);
              }
            }
          }.start();
        } else {
          if (isReady) {
            isBusy = false;

            if (!outputDatas.isEmpty()) {
              outputDatas.clear();
            }

            getCmd();
            isReady = false;
          } else {
            final BluetoothGattCharacteristic characteristic = gattCharacteristics.get(3);
            //                LogUtil.e("准备请求uuid:" + characteristic.getUuid());
            bluetoothGatt.readCharacteristic(characteristic);
          }
        }
      }
    }
  }
 @Override
 public void onServicesDiscovered(BluetoothGatt gatt, int status) {
   List<BluetoothGattService> services = gatt.getServices();
   Log.i("onServicesDiscovered", services.toString());
   gatt.readCharacteristic(services.get(1).getCharacteristics().get(0));
 }
Exemple #14
0
 public synchronized void read(UUID uuid) {
   if (bluetoothGattService != null) {
     BluetoothGattCharacteristic characteristic = bluetoothGattService.getCharacteristic(uuid);
     bluetoothGatt.readCharacteristic(characteristic);
   }
 }