Java Code Examples for android.bluetooth.BluetoothGatt#GATT_INVALID_OFFSET

The following examples show how to use android.bluetooth.BluetoothGatt#GATT_INVALID_OFFSET . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HeartRateServiceFragment.java    From ble-test-peripheral-android with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 2
Source File: BleManagerHandler.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void sendResponse(@NonNull final BluetoothGattServer server,
						  @NonNull final BluetoothDevice device, final int status,
						  final int requestId, final int offset,
						  @Nullable final byte[] response) {
	String msg;
	switch (status) {
		case BluetoothGatt.GATT_SUCCESS: 				msg = "GATT_SUCCESS"; break;
		case BluetoothGatt.GATT_REQUEST_NOT_SUPPORTED: 	msg = "GATT_REQUEST_NOT_SUPPORTED"; break;
		case BluetoothGatt.GATT_INVALID_OFFSET: 		msg = "GATT_INVALID_OFFSET"; break;
		default: throw new InvalidParameterException();
	}
	log(Log.DEBUG, "server.sendResponse(" + msg + ", offset=" + offset + ", value=" + ParserUtils.parseDebug(response) + ")");
	server.sendResponse(device, requestId, status, offset, response);
	log(Log.VERBOSE, "[Server] Response sent");
}
 
Example 3
Source File: BleUtils.java    From bleYan with GNU General Public License v2.0 5 votes vote down vote up
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;
    }
}
 
Example 4
Source File: HealthThermometerServiceFragment.java    From ble-test-peripheral-android with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 5
Source File: BleManagerHandler.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
final void onCharacteristicWriteRequest(@NonNull final BluetoothGattServer server,
										@NonNull final BluetoothDevice device, final int requestId,
										@NonNull final BluetoothGattCharacteristic characteristic,
										final boolean preparedWrite, final boolean responseNeeded,
										final int offset, @NonNull final byte[] value) {
	log(Log.DEBUG, "[Server callback] Write " + (responseNeeded ? "request" : "command")
			+ " to characteristic " + characteristic.getUuid()
			+ " (requestId=" + requestId + ", prepareWrite=" + preparedWrite + ", responseNeeded="
			+ responseNeeded + ", offset: " + offset + ", value=" + ParserUtils.parseDebug(value) + ")");
	if (offset == 0) {
		final String type = responseNeeded ? "WRITE REQUEST" : "WRITE COMMAND";
		final String option = preparedWrite ? "Prepare " : "";
		log(Log.INFO, "[Server] " + option + type + " for characteristic " + characteristic.getUuid()
				+ " received, value: " + ParserUtils.parse(value));
	}

	if (responseNeeded) {
		sendResponse(server, device, BluetoothGatt.GATT_SUCCESS, requestId, offset, value);
	}

	// If Prepare Write or Long Write is sent, store the data in a temporary queue until it's executed.
	if (preparedWrite) {
		if (preparedValues == null) {
			preparedValues = new LinkedList<>();
		}
		if (offset == 0) {
			// Add new value to the operations.
			preparedValues.offer(new Pair<>(characteristic, value));
		} else {
			// Concatenate the value to the end of previous value, if the previous request was
			// also for the same characteristic.
			final Pair<Object, byte[]> last = preparedValues.peekLast();
			if (last != null && characteristic.equals(last.first)) {
				preparedValues.pollLast();
				preparedValues.offer(new Pair<>(characteristic, Bytes.concat(last.second, value, offset)));
			} else {
				prepareError = BluetoothGatt.GATT_INVALID_OFFSET;
			}
		}
	} else {
		// Otherwise, save the data immediately.
		if (assignAndNotify(device, characteristic, value) || checkCondition()) {
			nextRequest(true);
		}
	}
}
 
Example 6
Source File: BleManagerHandler.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
final void onDescriptorWriteRequest(@NonNull final BluetoothGattServer server,
									@NonNull final BluetoothDevice device, final int requestId,
									@NonNull final BluetoothGattDescriptor descriptor,
									final boolean preparedWrite, final boolean responseNeeded,
									final int offset, @NonNull final byte[] value) {
	log(Log.DEBUG, "[Server callback] Write " + (responseNeeded ? "request" : "command")
			+ " to descriptor " + descriptor.getUuid()
			+ " (requestId=" + requestId + ", prepareWrite=" + preparedWrite + ", responseNeeded="
			+ responseNeeded + ", offset: " + offset + ", value=" + ParserUtils.parseDebug(value) + ")");
	if (offset == 0) {
		final String type = responseNeeded ? "WRITE REQUEST" : "WRITE COMMAND";
		final String option = preparedWrite ? "Prepare " : "";
		log(Log.INFO, "[Server] " + option + type + " request for descriptor " + descriptor.getUuid()
				+ " received, value: " + ParserUtils.parse(value));
	}

	if (responseNeeded) {
		sendResponse(server, device, BluetoothGatt.GATT_SUCCESS, requestId, offset, value);
	}

	// If Prepare Write or Long Write is sent, store the data in a temporary queue until it's executed.
	if (preparedWrite) {
		if (preparedValues == null) {
			preparedValues = new LinkedList<>();
		}
		if (offset == 0) {
			// Add new value to the operations.
			preparedValues.offer(new Pair<>(descriptor, value));
		} else {
			// Concatenate the value to the end of previous value, if the previous request was
			// also for the same descriptor.
			final Pair<Object, byte[]> last = preparedValues.peekLast();
			if (last != null && descriptor.equals(last.first)) {
				preparedValues.pollLast();
				preparedValues.offer(new Pair<>(descriptor, Bytes.concat(last.second, value, offset)));
			} else {
				prepareError = BluetoothGatt.GATT_INVALID_OFFSET;
			}
		}
	} else {
		// Otherwise, save the data immediately.
		if (assignAndNotify(device, descriptor, value) || checkCondition()) {
			nextRequest(true);
		}
	}
}