本文整理匯總了Java中android.bluetooth.BluetoothGattCharacteristic.setValue方法的典型用法代碼示例。如果您正苦於以下問題:Java BluetoothGattCharacteristic.setValue方法的具體用法?Java BluetoothGattCharacteristic.setValue怎麽用?Java BluetoothGattCharacteristic.setValue使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類android.bluetooth.BluetoothGattCharacteristic
的用法示例。
在下文中一共展示了BluetoothGattCharacteristic.setValue方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: setLights
import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
public void setLights(BluetoothUtil bluetoothUtil,int state) {
lightMode.set(state);
BluetoothGattCharacteristic lc = null;
ByteBuffer v = ByteBuffer.allocate(2);
lc = bluetoothUtil.getCharacteristic(OWDevice.OnewheelCharacteristicLightingMode);
v.putShort((short) state);
if (lc != null) {
lc.setValue(v.array());
lc.setWriteType(2);
bluetoothUtil.writeCharacteristic(lc);
EventBus.getDefault().post(new DeviceStatusEvent("LIGHTS SET TO STATE:" + state));
}
}
示例2: notifyRegisteredDevices
import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
/**
* Send a time service notification to any devices that are subscribed
* to the characteristic.
*/
private void notifyRegisteredDevices(long timestamp, byte adjustReason) {
if (mRegisteredDevices.isEmpty()) {
Log.i(TAG, "No subscribers registered");
return;
}
byte[] exactTime = TimeProfile.getExactTime(timestamp, adjustReason);
Log.i(TAG, "Sending update to " + mRegisteredDevices.size() + " subscribers");
for (BluetoothDevice device : mRegisteredDevices) {
BluetoothGattCharacteristic timeCharacteristic = mBluetoothGattServer
.getService(TimeProfile.TIME_SERVICE)
.getCharacteristic(TimeProfile.CURRENT_TIME);
timeCharacteristic.setValue(exactTime);
mBluetoothGattServer.notifyCharacteristicChanged(device, timeCharacteristic, false);
}
}
示例3: setOpCode
import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
/**
* Writes given operation parameters to the characteristic
*
* @param characteristic
* the characteristic to write. This must be the Record Access Control Point characteristic
* @param opCode
* the operation code
* @param operator
* the operator (see {@link #OPERATOR_NULL} and others
* @param params
* optional parameters (one for >=, <=, two for the range, none for other operators)
*/
private void setOpCode(final BluetoothGattCharacteristic characteristic, final int opCode, final int operator, final Integer... params) {
final int size = 2 + ((params.length > 0) ? 1 : 0) + params.length * 2; // 1 byte for opCode, 1 for operator, 1 for filter type (if parameters exists) and 2 for each parameter
characteristic.setValue(new byte[size]);
// write the operation code
int offset = 0;
characteristic.setValue(opCode, BluetoothGattCharacteristic.FORMAT_UINT8, offset);
offset += 1;
// write the operator. This is always present but may be equal to OPERATOR_NULL
characteristic.setValue(operator, BluetoothGattCharacteristic.FORMAT_UINT8, offset);
offset += 1;
// if parameters exists, append them. Parameters should be sorted from minimum to maximum. Currently only one or two params are allowed
if (params.length > 0) {
// our implementation use only sequence number as a filer type
characteristic.setValue(FILTER_TYPE_SEQUENCE_NUMBER, BluetoothGattCharacteristic.FORMAT_UINT8, offset);
offset += 1;
for (final Integer i : params) {
characteristic.setValue(i, BluetoothGattCharacteristic.FORMAT_UINT16, offset);
offset += 2;
}
}
}
示例4: writeData
import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
public void writeData(UUID service, UUID Characteristics,byte[] data) {
if (!isConnectedToGatt || myGatBand == null) {
Log.d(TAG, "Cant read from BLE, not initialized.");
return;
}
Log.d(TAG, "* Getting gatt service, UUID:" + service.toString());
BluetoothGattService myGatService =
myGatBand.getService(service /*Consts.UUID_SERVICE_HEARTBEAT*/);
if (myGatService != null) {
Log.d(TAG, "* Getting gatt Characteristic. UUID: " + Characteristics.toString());
BluetoothGattCharacteristic myGatChar
= myGatService.getCharacteristic(Characteristics /*Consts.UUID_START_HEARTRATE_CONTROL_POINT*/);
if (myGatChar != null) {
Log.d(TAG, "* Writing trigger");
myGatChar.setValue(data /*Consts.BYTE_NEW_HEART_RATE_SCAN*/);
boolean status = myGatBand.writeCharacteristic(myGatChar);
Log.d(TAG, "* Writting trigger status :" + status);
}
}
}
示例5: writeCharacteristic
import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
private void writeCharacteristic(String serviceGuid, String characteristic, int value, int type) {
if(!isConnected()) {
logi("writeCharacteristic() :: Not connected. Returning");
return;
}
BluetoothGattService s = getService(UUID.fromString(serviceGuid));
if(s == null) {
logi("writeCharacteristic() :: Service not found");
return;
}
BluetoothGattCharacteristic c = s.getCharacteristic(UUID.fromString(characteristic));
if(c == null) {
logi("writeCharacteristic() :: characteristic not found");
return;
}
c.setValue(value, type, 0);
int ret = writeCharacteristic(c);
logi("writeCharacteristic() :: returns - " + ret);
}
示例6: onCharacteristicWriteRequest
import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
@Override
public void onCharacteristicWriteRequest(final BluetoothDevice device, final int requestId, final BluetoothGattCharacteristic characteristic, final boolean preparedWrite,
final boolean responseNeeded, final int offset, final byte[] value) {
Logger.d(mLogSession, "[Server callback] Write request to characteristic " + characteristic.getUuid()
+ " (requestId=" + requestId + ", prepareWrite=" + preparedWrite + ", responseNeeded=" + responseNeeded + ", offset=" + offset + ", value=" + ParserUtils.parse(value) + ")");
final String writeType = !responseNeeded ? "WRITE NO RESPONSE" : "WRITE COMMAND";
Logger.i(mLogSession, "[Server] " + writeType + " request for characteristic " + characteristic.getUuid() + " received, value: " + ParserUtils.parse(value));
if (offset == 0) {
characteristic.setValue(value);
} else {
final byte[] currentValue = characteristic.getValue();
final byte[] newValue = new byte[currentValue.length + value.length];
System.arraycopy(currentValue, 0, newValue, 0, currentValue.length);
System.arraycopy(value, 0, newValue, offset, value.length);
characteristic.setValue(newValue);
}
if (!preparedWrite && value != null && value.length == 1) { // small validation
if (value[0] != NO_ALERT[0]) {
Logger.a(mLogSession, "[Server] Immediate alarm request received: " + AlertLevelParser.parse(characteristic));
mCallbacks.onAlarmTriggered();
} else {
Logger.a(mLogSession, "[Server] Immediate alarm request received: OFF");
mCallbacks.onAlarmStopped();
}
}
Logger.d(mLogSession, "server.sendResponse(GATT_SUCCESS, offset=" + offset + ", value=" + ParserUtils.parse(value) + ")");
mBluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, null);
Logger.v(mLogSession, "[Server] Response sent");
}
示例7: writeCharacteristic
import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
/**
* Request a write on a given {@code BluetoothGattCharacteristic}. The write result is reported
* asynchronously through the {@code BluetoothGattCallback#onCharacteristicRead(android.bluetooth.BluetoothGatt, android.bluetooth.BluetoothGattCharacteristic, int)}
* callback.
*
* @param characteristic The characteristic to write on.
*/
public void writeCharacteristic(BluetoothGattCharacteristic characteristic, byte[] value) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
Log.d("WRITE",characteristic.getUuid().toString() + " : " + utils.bytesToHex(value));
characteristic.setValue(value);
mBluetoothGatt.writeCharacteristic(characteristic);
}
示例8: setCharacteristicValue
import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
public void setCharacteristicValue(BluetoothGattService gattService, BluetoothGatt gatt, String k, int v) {
DeviceCharacteristic dc = getDeviceCharacteristicByKey(k);
if (dc != null) {
BluetoothGattCharacteristic lc = null;
lc = gattService.getCharacteristic(UUID.fromString(dc.uuid.get()));
if (lc != null) {
ByteBuffer var2 = ByteBuffer.allocate(2);
var2.putShort((short) v);
lc.setValue(var2.array());
lc.setWriteType(2);
gatt.writeCharacteristic(lc);
EventBus.getDefault().post(new DeviceStatusEvent("SET " + k + " TO " + v));
}
}
}
示例9: writeOpCode
import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
/**
* Writes the operation code to the characteristic. This method is SYNCHRONOUS and wait until the
* {@link android.bluetooth.BluetoothGattCallback#onCharacteristicWrite(android.bluetooth.BluetoothGatt, android.bluetooth.BluetoothGattCharacteristic, int)} will be called or the connection state will change from {@link #STATE_CONNECTED_AND_READY}.
* If connection state will change, or an error will occur, an exception will be thrown.
*
* @param gatt the GATT device
* @param characteristic the characteristic to write to. Should be the DFU CONTROL POINT
* @param value the value to write to the characteristic
* @param reset whether the command trigger restarting the device
* @throws DeviceDisconnectedException
* @throws DfuException
* @throws UploadAbortedException
*/
private void writeOpCode(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic, final byte[] value, final boolean reset) throws DeviceDisconnectedException, DfuException,
UploadAbortedException {
mReceivedData = null;
mError = 0;
mRequestCompleted = false;
/*
* Sending a command that will make the DFU target to reboot may cause an error 133 (0x85 - Gatt Error). If so, with this flag set, the error will not be shown to the user
* as the peripheral is disconnected anyway. See: mGattCallback#onCharacteristicWrite(...) method
*/
mResetRequestSent = reset;
characteristic.setValue(value);
sendLogBroadcast(LOG_LEVEL_VERBOSE, "Writing to characteristic " + characteristic.getUuid());
sendLogBroadcast(LOG_LEVEL_DEBUG, "gatt.writeCharacteristic(" + characteristic.getUuid() + ")");
gatt.writeCharacteristic(characteristic);
// We have to wait for confirmation
try {
synchronized (mLock) {
while ((!mRequestCompleted && mConnectionState == STATE_CONNECTED_AND_READY && mError == 0 && !mAborted) || mPaused)
mLock.wait();
}
} catch (final InterruptedException e) {
loge("Sleeping interrupted", e);
}
if (mAborted)
throw new UploadAbortedException();
if (!mResetRequestSent && mError != 0)
throw new DfuException("Unable to write Op Code " + value[0], mError);
if (!mResetRequestSent && mConnectionState != STATE_CONNECTED_AND_READY)
throw new DeviceDisconnectedException("Unable to write Op Code " + value[0], mConnectionState);
}
示例10: writeImageSize
import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
/**
* Writes the image size to the characteristic. This method is SYNCHRONOUS and wait until the {@link android.bluetooth.BluetoothGattCallback#onCharacteristicWrite(android.bluetooth.BluetoothGatt, android.bluetooth.BluetoothGattCharacteristic, int)}
* will be called or the connection state will change from {@link #STATE_CONNECTED_AND_READY}. If connection state will change, or an error will occur, an exception will be thrown.
*
* @param gatt the GATT device
* @param characteristic the characteristic to write to. Should be the DFU PACKET
* @param imageSize the image size in bytes
* @throws DeviceDisconnectedException
* @throws DfuException
* @throws UploadAbortedException
*/
private void writeImageSize(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic, final int imageSize) throws DeviceDisconnectedException, DfuException,
UploadAbortedException {
mReceivedData = null;
mError = 0;
mImageSizeSent = false;
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
characteristic.setValue(new byte[4]);
characteristic.setValue(imageSize, BluetoothGattCharacteristic.FORMAT_UINT32, 0);
sendLogBroadcast(LOG_LEVEL_VERBOSE, "Writing to characteristic " + characteristic.getUuid());
sendLogBroadcast(LOG_LEVEL_DEBUG, "gatt.writeCharacteristic(" + characteristic.getUuid() + ")");
gatt.writeCharacteristic(characteristic);
// We have to wait for confirmation
try {
synchronized (mLock) {
while ((!mImageSizeSent && mConnectionState == STATE_CONNECTED_AND_READY && mError == 0 && !mAborted) || mPaused)
mLock.wait();
}
} catch (final InterruptedException e) {
loge("Sleeping interrupted", e);
}
if (mAborted)
throw new UploadAbortedException();
if (mError != 0)
throw new DfuException("Unable to write Image Size", mError);
if (mConnectionState != STATE_CONNECTED_AND_READY)
throw new DeviceDisconnectedException("Unable to write Image Size", mConnectionState);
}
示例11: setCharacteristicValue
import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
public boolean setCharacteristicValue(UUID serviceUuid, UUID characteristicUuid, byte[] value, boolean notify){
BluetoothGattService service = gattServer.getService(serviceUuid);
if(service == null)
return false;
BluetoothGattCharacteristic ch = service.getCharacteristic(characteristicUuid);
if(ch == null)
return false;
boolean rtn = ch.setValue(value);
if(rtn && notify){
notifyDevices(ch);
}
return rtn;
}
示例12: writeInitPacket
import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
/**
* Writes the Init packet to the characteristic. This method is SYNCHRONOUS and wait until the {@link android.bluetooth.BluetoothGattCallback#onCharacteristicWrite(android.bluetooth.BluetoothGatt, android.bluetooth.BluetoothGattCharacteristic, int)}
* will be called or the connection state will change from {@link #STATE_CONNECTED_AND_READY}. If connection state will change, or an error will occur, an exception will be thrown.
*
* @param gatt the GATT device
* @param characteristic the characteristic to write to. Should be the DFU PACKET
* @param buffer the init packet as a byte array. This must be shorter or equal to 20 bytes (TODO check this restriction).
* @param size the init packet size
* @throws DeviceDisconnectedException
* @throws DfuException
* @throws UploadAbortedException
*/
private void writeInitPacket(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic, final byte[] buffer, final int size) throws DeviceDisconnectedException, DfuException,
UploadAbortedException {
byte[] locBuffer = buffer;
if (buffer.length != size) {
locBuffer = new byte[size];
System.arraycopy(buffer, 0, locBuffer, 0, size);
}
mReceivedData = null;
mError = 0;
mInitPacketSent = false;
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
characteristic.setValue(locBuffer);
logi("Sending init packet (Value = " + parse(locBuffer) + ")");
sendLogBroadcast(LOG_LEVEL_VERBOSE, "Writing to characteristic " + characteristic.getUuid());
sendLogBroadcast(LOG_LEVEL_DEBUG, "gatt.writeCharacteristic(" + characteristic.getUuid() + ")");
gatt.writeCharacteristic(characteristic);
// We have to wait for confirmation
try {
synchronized (mLock) {
while ((!mInitPacketSent && mConnectionState == STATE_CONNECTED_AND_READY && mError == 0 && !mAborted) || mPaused)
mLock.wait();
}
} catch (final InterruptedException e) {
loge("Sleeping interrupted", e);
}
if (mAborted)
throw new UploadAbortedException();
if (mError != 0)
throw new DfuException("Unable to write Init DFU Parameters", mError);
if (mConnectionState != STATE_CONNECTED_AND_READY)
throw new DeviceDisconnectedException("Unable to write Init DFU Parameters", mConnectionState);
}
示例13: setCharacteristicValue
import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
public boolean setCharacteristicValue(UUID serviceUuid, UUID characteristicUuid, int value, int format, int ofst){
if(gattConnection == null)
return false;
BluetoothGattService service = gattConnection.getService(serviceUuid);
if(service == null)
return false;
BluetoothGattCharacteristic ch = service.getCharacteristic(characteristicUuid);
if(ch == null)
return false;
ch.setValue(value, format, ofst);
return gattConnection.writeCharacteristic(ch);
}
示例14: writeCharacteristicNonBlock
import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
public boolean writeCharacteristicNonBlock(final String address, final BluetoothGattCharacteristic characteristic, final byte[] b) {
boolean result = false;
BluetoothGatt bluetoothGatt = checkAndGetGattItem(address);
if (bluetoothGatt != null) {
characteristic.setValue(b);
result = bluetoothGatt.writeCharacteristic(characteristic);
}
return result;
}
示例15: onDescriptorWrite
import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
/**
* Callback indicating the result of a descriptor write operation.
*
* @param gatt GATT client invoked {@link BluetoothGatt#writeDescriptor}
* @param descriptor Descriptor that was writte to the associated
* remote device.
* @param status The result of the write operation
* {@link BluetoothGatt#GATT_SUCCESS} if the operation succeeds.
*/
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
// boolean indicating whether or not the next step is successful, default is false
boolean success = false;
// Check if writing descriptor was successful and force the action notification if it
// was
if (status == BluetoothGatt.GATT_SUCCESS) {
// Check if the SPIN Service is found
final BluetoothGattService spinService = gatt.getService(SPIN_SERVICE_UUID);
if (spinService != null) {
// Check if the Command Characteristic is found, write the new value and store
// the result
final BluetoothGattCharacteristic commandCharacteristic
= spinService.getCharacteristic(COMMAND_CHARACTERISTIC_UUID);
if (commandCharacteristic != null) {
// Set the value to 0x0801
commandCharacteristic.setValue(
new byte[]{
(byte) 0x08, // commandId = force action notification (8)
(byte) 0x01 // enable = false (0) or true (1)
}
);
success = gatt.writeCharacteristic(commandCharacteristic);
}
}
}
onStep(gatt, success);
}