@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);
      }
    }
  }
 public void writeCharacteristic(BluetoothGattCharacteristic characteristic) {
   if (mBluetoothAdapter == null || mBluetoothGatt == null) {
     Log.w(TAG, "BluetoothAdapter not initialized");
     return;
   }
   mBluetoothGatt.writeCharacteristic(characteristic);
 }
Beispiel #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;
          }
        }
  public void writeCharacter(String address) {

    BluetoothGattService alarmService = null;

    BluetoothGattCharacteristic alarmCharacter = null;

    if (mBluetoothGatt != null) {
      alarmService = mBluetoothGatt.getService(SERVIE_UUID);
    }
    if (alarmService == null) {
      showMessage("link loss Alert service not found!");
      return;
    }
    alarmCharacter =
        alarmService.getCharacteristic(UUID.fromString("00002a06-0000-1000-8000-00805f9b34fb"));
    if (alarmCharacter == null) {
      connect(address);
      showMessage("link loss Alert Level charateristic not found!");
      return;
    }
    alarmCharacter.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
    byte[] value = {0x01};
    alarmCharacter.setValue(value);
    mBluetoothGatt.writeCharacteristic(alarmCharacter);
  }
Beispiel #5
0
 public synchronized void send(UUID uuid, byte[] value) {
   if (bluetoothGattService != null) {
     BluetoothGattCharacteristic characteristic = bluetoothGattService.getCharacteristic(uuid);
     characteristic.setValue(value);
     bluetoothGatt.writeCharacteristic(characteristic);
   }
 }
Beispiel #6
0
  public void writeLetter() {

    char letter = motEcrit.charAt(indiceMotEcrit);
    Log.i(tag, "         envoie  = " + letter);
    sendByte = new String(new char[] {letter}).getBytes();
    BluetoothGattCharacteristic characteristic =
        bluetoothGatt.getService(SERVICE).getCharacteristic(CHARACTERISTIC);
    characteristic.setValue(sendByte);
    bluetoothGatt.writeCharacteristic(characteristic);
  }
Beispiel #7
0
  private static boolean writeValue(
      BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, byte[] bytes) {
    if (gatt == null) {
      Log.e(TAG, "BluetoothAdapter not initialized");
      return false;
    }

    // PrintBytes(bytes);
    characteristic.setValue(bytes);
    return gatt.writeCharacteristic(characteristic);
  }
  private void turnOnService(BluetoothGatt gatt, UUID serviceUUID, UUID configUUID) {

    BluetoothGattService humService = gatt.getService(serviceUUID);
    BluetoothGattCharacteristic config = humService.getCharacteristic(configUUID);
    config.setValue(
        new byte[] {
          1
        }); // 01 for Humidity; 01 for +-2g deviation//NB: the config value is different for the
            // Gyroscope
    gatt.writeCharacteristic(config);
  }
Beispiel #9
0
        private void disableSensors(BluetoothGatt gatt) {
          SICRunning = false;
          BluetoothGattCharacteristic characteristic;
          Log.d(TAG, "Disabling Sensors");
          characteristic = gatt.getService(MAIN_SERVICE).getCharacteristic(CONFIG_CHAR);
          byte finisher = 0x00;
          characteristic.setValue(new byte[] {finisher});

          gatt.writeCharacteristic(characteristic);
          sicAct.stillRunning = true;
        }
 private void excuteCmd() {
   byte[] bytes = currentCmdPacket.getInputCmdPacket();
   //        LogUtil.i("executecmd=" + LogUtil.byte2HexString((bytes)));
   if (gattCharacteristics != null) {
     final BluetoothGattCharacteristic characteristic = gattCharacteristics.get(1);
     characteristic.setValue(bytes);
     if (bluetoothGatt == null) {
       return;
     }
     bluetoothGatt.writeCharacteristic(characteristic);
   }
 }
  public void doWrite(BluetoothGattCharacteristic characteristic, byte[] data) {

    // Log.d(LOG_TAG, "doWrite");

    characteristic.setValue(data);

    if (gatt.writeCharacteristic(characteristic)) {
      // Log.d(LOG_TAG, "doWrite completato");
    } else {
      Log.d(LOG_TAG, "errore doWrite");
    }
  }
Beispiel #12
0
        private void writeSecurity(BluetoothGatt gatt) {
          Log.wtf(TAG, "Security Written");
          BluetoothGattCharacteristic securityCharacteristic = null;
          byte[] packet = new byte[1];
          int i = 153;

          packet[0] = (byte) i;
          securityCharacteristic = gatt.getService(MAIN_SERVICE).getCharacteristic(SECURITY_KEY);
          securityCharacteristic.setValue(packet);
          Log.wtf(TAG, packet[0] + "");
          gatt.writeCharacteristic(securityCharacteristic);
        }
  public void wirteCharacteristic(BluetoothGattCharacteristic characteristic) {

    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
      LogUtil.info(TAG, "BluetoothAdapter not initialized");
      return;
    }
    boolean isWrite = mBluetoothGatt.writeCharacteristic(characteristic);
    if (isWrite) {
      LogUtil.info(TAG, "写入数据成功");
    } else {
      LogUtil.info(TAG, "写入数据失败");
    }
  }
  public boolean wirteCharacteristic(BluetoothGattCharacteristic characteristic) {

    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
      Log.w(TAG, "BluetoothAdapter not initialized");
      return false;
    }
    count++;
    Log.w("qh", "qh:+count:" + count);
    //	boolean flag = mBluetoothGatt.executeReliableWrite();

    mBluetoothGatt.writeCharacteristic(characteristic);

    return true;
  }
  /**
   * Writes the characteristic value to the given characteristic.
   *
   * @param characteristic the characteristic to write to
   * @return true if request has been sent
   */
  protected final boolean writeCharacteristic(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_WRITE
                | BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE))
        == 0) return false;

    Logger.v(mLogSession, "Writing characteristic " + characteristic.getUuid());
    Logger.d(mLogSession, "gatt.writeCharacteristic(" + characteristic.getUuid() + ")");
    return gatt.writeCharacteristic(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);
     }
   }
 }
Beispiel #17
0
  private void activateLED(int led) {
    BluetoothGattCharacteristic characteristic;
    // new byte [] finisher = 0x01;

    characteristic = gatt2.getService(MAIN_SERVICE).getCharacteristic(FINISH);
    if (led == 1) {
      maintChar ^= 1 << 5;
    }
    if (led == 2) {
      maintChar ^= 1 << 4;
    }
    if (led == 3) {
      maintChar ^= 1 << 3;
    }
    if (led == 4) {
      maintChar ^= 1 << 2;
    }
    characteristic.setValue(new byte[] {maintChar});
    Log.wtf(TAG, "Value is: " + characteristic.getValue().toString());
    gatt2.writeCharacteristic(characteristic);
  }
  // ----------------------------------------------------------------------------------------------------------------
  // Write to a given characteristic. The completion of the write is reported asynchronously through
  // the
  // BluetoothGattCallback onCharacteristicWrire callback method.
  public void writeCharacteristic(BluetoothGattCharacteristic characteristic) {
    if (mBluetoothAdapter == null
        || mBluetoothGatt == null) { // Check that we have access to a Bluetooth radio
      Log.w(TAG, "BluetoothAdapter not initialized");
      return;
    }
    int test = characteristic.getProperties(); // Get the properties of the characteristic
    if ((test & BluetoothGattCharacteristic.PROPERTY_WRITE) == 0
        && (test & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE)
            == 0) { // Check that the property is writable
      return;
    }

    if (mBluetoothGatt.writeCharacteristic(
        characteristic)) { // Request the BluetoothGatt to do the Write
      Log.d(
          TAG,
          "writeCharacteristic successful"); // The request was accepted, this does not mean the
                                             // write completed
    } else {
      Log.d(
          TAG, "writeCharacteristic failed"); // Write request was not accepted by the BluetoothGatt
    }
  }
  public void write(
      UUID serviceUUID,
      UUID characteristicUUID,
      byte[] data,
      Callback successCallback,
      Callback failCallback,
      int writeType) {

    Log.d(LOG_TAG, "write interno peripheral");

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

    } else {

      BluetoothGattService service = gatt.getService(serviceUUID);
      BluetoothGattCharacteristic characteristic =
          findWritableCharacteristic(service, characteristicUUID, writeType);
      characteristic.setWriteType(writeType);

      if (characteristic == null) {
        failCallback.invoke("Characteristic " + characteristicUUID + " not found.");
      } else {

        if (writeQueue.size() > 0) {
          failCallback.invoke("Scrittura con byte ancora in coda");
        }

        if (writeCallback != null) {
          failCallback.invoke("Altra scrittura in corso");
        }

        if (writeQueue.size() == 0 && writeCallback == null) {

          if (BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT == writeType) {
            writeCallback = successCallback;
            writeFailCallback = failCallback;
          } else successCallback.invoke();

          if (data.length > 20) {
            int dataLength = data.length;
            int count = 0;
            byte[] firstMessage = null;
            while (count < dataLength && (dataLength - count > 20)) {
              if (count == 0) {
                firstMessage = Arrays.copyOfRange(data, count, count + 20);
              } else {
                byte[] splitMessage = Arrays.copyOfRange(data, count, count + 20);
                writeQueue.add(splitMessage);
              }
              count += 20;
            }
            if (count < dataLength) {
              // Rimangono byte in coda
              byte[] splitMessage = Arrays.copyOfRange(data, count, data.length);
              Log.d(LOG_TAG, "Lunghezza ultimo messaggio: " + splitMessage.length);
              writeQueue.add(splitMessage);
            }

            Log.d(LOG_TAG, "Messaggi in coda: " + writeQueue.size());
            doWrite(characteristic, firstMessage);
          } else {
            characteristic.setValue(data);

            if (gatt.writeCharacteristic(characteristic)) {
              Log.d(LOG_TAG, "write completato");
            } else {
              writeCallback = null;
              failCallback.invoke("Write failed");
            }
          }
        }
      }
    }
  }