android.bluetooth.BluetoothGatt#GATT_INVALID_ATTRIBUTE_LENGTH源码实例Demo

下面列出了android.bluetooth.BluetoothGatt#GATT_INVALID_ATTRIBUTE_LENGTH 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@Override
public int writeCharacteristic(BluetoothGattCharacteristic characteristic, int offset, byte[] value) {
  if (offset != 0) {
    return BluetoothGatt.GATT_INVALID_OFFSET;
  }
  // Heart Rate control point is a 8bit characteristic
  if (value.length != 1) {
    return BluetoothGatt.GATT_INVALID_ATTRIBUTE_LENGTH;
  }
  if ((value[0] & 1) == 1) {
    getActivity().runOnUiThread(new Runnable() {
      @Override
      public void run() {
        mHeartRateMeasurementCharacteristic.setValue(INITIAL_EXPENDED_ENERGY,
            EXPENDED_ENERGY_FORMAT, /* offset */ 2);
        mEditTextEnergyExpended.setText(Integer.toString(INITIAL_EXPENDED_ENERGY));
      }
    });
  }
  return BluetoothGatt.GATT_SUCCESS;
}
 
源代码2 项目: bleYan   文件: BleUtils.java
public static String getGattStatus(int status) {
    switch (status) {
        case BluetoothGatt.GATT_SUCCESS:
            return "GATT_SUCCESS";

        case BluetoothGatt.GATT_READ_NOT_PERMITTED:
            return "GATT_READ_NOT_PERMITTED";

        case BluetoothGatt.GATT_WRITE_NOT_PERMITTED:
            return "GATT_WRITE_NOT_PERMITTED";

        case BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION:
            return "GATT_INSUFFICIENT_AUTHENTICATION";

        case BluetoothGatt.GATT_REQUEST_NOT_SUPPORTED:
            return "GATT_REQUEST_NOT_SUPPORTED";

        case BluetoothGatt.GATT_INSUFFICIENT_ENCRYPTION:
            return "GATT_INSUFFICIENT_ENCRYPTION";

        case BluetoothGatt.GATT_INVALID_OFFSET:
            return "GATT_INVALID_OFFSET";

        case BluetoothGatt.GATT_INVALID_ATTRIBUTE_LENGTH:
            return "GATT_INVALID_ATTRIBUTE_LENGTH";

        case BluetoothGatt.GATT_FAILURE:
            return "GATT_FAILURE";

        default:
            return "STATE_UNKNOWN: " + status;
    }
}
 
@Override
public int writeCharacteristic(BluetoothGattCharacteristic characteristic, int offset, byte[] value) {
  if (offset != 0) {
    return BluetoothGatt.GATT_INVALID_OFFSET;
  }
  // Measurement Interval is a 16bit characteristic
  if (value.length != 2) {
    return BluetoothGatt.GATT_INVALID_ATTRIBUTE_LENGTH;
  }
  ByteBuffer byteBuffer = ByteBuffer.wrap(value);
  byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
  final int newMeasurementIntervalValue = byteBuffer.getShort();
  if (!isValidMeasurementIntervalValue(newMeasurementIntervalValue)) {
    return BluetoothGatt.GATT_FAILURE;
  }
  getActivity().runOnUiThread(new Runnable() {
    @Override
    public void run() {
      mMeasurementIntervalCharacteristic.setValue(newMeasurementIntervalValue,
          MEASUREMENT_INTERVAL_FORMAT,
          /* offset */ 0);
      if (mMeasurementIntervalCCCDescriptor.getValue() == BluetoothGattDescriptor.ENABLE_INDICATION_VALUE) {
        resetTimer(newMeasurementIntervalValue);
        mTextViewNotifications.setText(R.string.notificationsEnabled);
      }
    }
  });
  return BluetoothGatt.GATT_SUCCESS;
}
 
源代码4 项目: beacons-android   文件: EddystoneGattService.java
public int writeCharacteristic(BluetoothDevice device, BluetoothGattCharacteristic characteristic, byte[] value) {
//        UUID uuid = characteristic.getUuid();
        if (isLocked()) {
            if (characteristic == mUnlockCharacteristic) {
                if (value.length == 16) {
                    byte[] token = aes_transform(true, characteristic.getValue(), 0, 16);
                    if (Arrays.equals(token, value)) {
                        log(String.format("Unlocked by %s", device));

                        mOwnerDevice = device;
                        mGattServer.disconnectAll(device);
                        characteristic.setValue((byte[]) null);
                        mLockStateCharacteristic.setValue(new byte[] { LOCK_STATE_UNLOCKED});

                        return BluetoothGatt.GATT_SUCCESS;
                    }
                    else log("Unlock failed!");
                }
                else log(String.format(Locale.US, "Unlock: expected 16 bytes, got %d", value.length));
            }

            log("Beacon locked - write request denied");
            return BluetoothGatt.GATT_WRITE_NOT_PERMITTED;
        }

        if (characteristic == mLockStateCharacteristic) {
            if (LOCK_STATE_LOCKED == value[0] && 17 == value.length) {
                mLockKey = aes_transform(false, value, 1, 16);
                mConfigCallback.setLockKey(mLockKey);
                log("Lock key changed");
            }
            characteristic.setValue(new byte[]{value[0]});
        } else if (characteristic == mActiveSlotCharacteristic) {
            log("Request to change active slot to " + value[0]);
            if (value[0] != 1) {
                // Beacon Tools tries to change the active slot to 1
                return BluetoothGatt.GATT_INVALID_ATTRIBUTE_LENGTH;
            }
        } else if (characteristic == mRadioTxPowerCharacteristic) {
            if (value.length == 1) {
                int txPower = mConfigCallback.setRadioTxPower(value[0]);
                characteristic.setValue(txPower, BluetoothGattCharacteristic.FORMAT_SINT8, 0);

                // if Radio TX has changed, then Advertised TX has also changed
                mAdvertisedTxPowerCharacteristic.setValue(mConfigCallback.getAdvertisedTxPower(), BluetoothGattCharacteristic.FORMAT_SINT8, 0);
                log(String.format(Locale.US, "Radio TX Power %d was requested. Actual value is now %d",
                        value[0], txPower));
            }
            else {
                log("Invalid Radio TX power value size: " + value.length);
            }
        } else if (characteristic == mAdvertiseIntervalCharacteristic) {
            if (value.length == 2) {
                int wantedAdvertiseInterval = unpackShort(value);
                int actualAdvertiseInterval = mConfigCallback.setAdvertiseInterval(wantedAdvertiseInterval);
                characteristic.setValue(toBigEndian(actualAdvertiseInterval), BluetoothGattCharacteristic.FORMAT_UINT16, 0);
                log(String.format(Locale.US, "Advertise Interval %d was requested. Actual value is now %d",
                        wantedAdvertiseInterval, actualAdvertiseInterval));
            }
            else {
                log("Invalid Advertise Interval value size: " + value.length);
            }
        } else if (characteristic == mAdvSlotDataCharacteristic) {
            handleWriteAdvertiseSlotData(value);
        } else if (characteristic == mFactoryResetCharacteristic) {
            if (0x0B == value[0]) {
                factoryReset();
            }
        }

        return BluetoothGatt.GATT_SUCCESS;
    }
 
源代码5 项目: ble-test-peripheral-android   文件: Peripheral.java
@Override
public void onDescriptorWriteRequest(BluetoothDevice device, int requestId,
    BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded,
    int offset,
    byte[] value) {
  super.onDescriptorWriteRequest(device, requestId, descriptor, preparedWrite, responseNeeded,
      offset, value);
  Log.v(TAG, "Descriptor Write Request " + descriptor.getUuid() + " " + Arrays.toString(value));
  int status = BluetoothGatt.GATT_SUCCESS;
  if (descriptor.getUuid() == CLIENT_CHARACTERISTIC_CONFIGURATION_UUID) {
    BluetoothGattCharacteristic characteristic = descriptor.getCharacteristic();
    boolean supportsNotifications = (characteristic.getProperties() &
        BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0;
    boolean supportsIndications = (characteristic.getProperties() &
        BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0;

    if (!(supportsNotifications || supportsIndications)) {
      status = BluetoothGatt.GATT_REQUEST_NOT_SUPPORTED;
    } else if (value.length != 2) {
      status = BluetoothGatt.GATT_INVALID_ATTRIBUTE_LENGTH;
    } else if (Arrays.equals(value, BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE)) {
      status = BluetoothGatt.GATT_SUCCESS;
      mCurrentServiceFragment.notificationsDisabled(characteristic);
      descriptor.setValue(value);
    } else if (supportsNotifications &&
        Arrays.equals(value, BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE)) {
      status = BluetoothGatt.GATT_SUCCESS;
      mCurrentServiceFragment.notificationsEnabled(characteristic, false /* indicate */);
      descriptor.setValue(value);
    } else if (supportsIndications &&
        Arrays.equals(value, BluetoothGattDescriptor.ENABLE_INDICATION_VALUE)) {
      status = BluetoothGatt.GATT_SUCCESS;
      mCurrentServiceFragment.notificationsEnabled(characteristic, true /* indicate */);
      descriptor.setValue(value);
    } else {
      status = BluetoothGatt.GATT_REQUEST_NOT_SUPPORTED;
    }
  } else {
    status = BluetoothGatt.GATT_SUCCESS;
    descriptor.setValue(value);
  }
  if (responseNeeded) {
    mGattServer.sendResponse(device, requestId, status,
        /* No need to respond with offset */ 0,
        /* No need to respond with a value */ null);
  }
}