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);
  }
示例#2
0
  public ProgramInfo(byte[] mImage, SensorTagDevice device) {
    // this.mFileImgHdr = mFileImgHdr;
    mFileImgHdr = new ImageHdr(mImage);
    mFileBuffer = mImage;
    this.mBtLeService = BluetoothLeService.getInstance();
    this.deviceAddress = device.getAddress();

    BluetoothGattService mOadService = device.getOadService();
    BluetoothGattService mConnControlService = device.getConnControlService();
    // Characteristics list
    List<BluetoothGattCharacteristic> mCharListOad = mOadService.getCharacteristics();
    List<BluetoothGattCharacteristic> mCharListCc = mConnControlService.getCharacteristics();

    mServiceOk = mCharListOad.size() == 2 && mCharListCc.size() >= 3;
    if (mServiceOk) {
      mCharIdentify = mCharListOad.get(0);
      mCharBlock = mCharListOad.get(1);
      mCharBlock.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
      mCharConnReq = mCharListCc.get(1);

      mBtLeService.setCharacteristicNotification(deviceAddress, mCharBlock, true);

      if (Build.VERSION.SDK_INT >= 21) {
        mBtLeService.requestConnectionPriority(
            deviceAddress, BluetoothGatt.CONNECTION_PRIORITY_HIGH);
      }

      setConnectionParameters();
    }
  }
  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");
            }
          }
        }
      }
    }
  }