本文整理汇总了Java中android.bluetooth.BluetoothGatt.writeCharacteristic方法的典型用法代码示例。如果您正苦于以下问题:Java BluetoothGatt.writeCharacteristic方法的具体用法?Java BluetoothGatt.writeCharacteristic怎么用?Java BluetoothGatt.writeCharacteristic使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.bluetooth.BluetoothGatt
的用法示例。
在下文中一共展示了BluetoothGatt.writeCharacteristic方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeCharacteristic
import android.bluetooth.BluetoothGatt; //导入方法依赖的package包/类
private void writeCharacteristic(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) throws DeviceDisconnectedException, DfuException,
UploadAbortedException {
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();
}
}
示例2: writePacket
import android.bluetooth.BluetoothGatt; //导入方法依赖的package包/类
/**
* Writes the buffer to the characteristic. The maximum size of the buffer is 20 bytes. This method is ASYNCHRONOUS and returns immediately after adding the data to TX queue.
*
* @param gatt the GATT device
* @param characteristic the characteristic to write to. Should be the DFU PACKET
* @param buffer the buffer with 1-20 bytes
* @param size the number of bytes from the buffer to send
*/
private void writePacket(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic, final byte[] buffer, final int size) {
byte[] locBuffer = buffer;
if (buffer.length != size) {
locBuffer = new byte[size];
System.arraycopy(buffer, 0, locBuffer, 0, size);
}
//logi("Sending Packet - " + bytesToHex(locBuffer));
characteristic.setValue(locBuffer);
gatt.writeCharacteristic(characteristic);
// FIXME BLE buffer overflow
// after writing to the device with WRITE_NO_RESPONSE property the onCharacteristicWrite callback is received immediately after writing data to a buffer.
// The real sending is much slower than adding to the buffer. This method does not return false if writing didn't succeed.. just the callback is not invoked.
//
// More info: this works fine on Nexus 5 (Android 4.4) (4.3 seconds) and on Samsung S4 (Android 4.3) (20 seconds) so this is a driver issue.
// Nexus 4 and 7 uses Qualcomm chip, Nexus 5 and Samsung uses Broadcom chips.
}
示例3: setCharacteristicValue
import android.bluetooth.BluetoothGatt; //导入方法依赖的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));
}
}
}
示例4: onServicesDiscovered
import android.bluetooth.BluetoothGatt; //导入方法依赖的package包/类
/**
* Callback invoked when the list of remote services, characteristics and descriptors
* for the remote device have been updated, ie new services have been discovered.
*
* @param gatt GATT client invoked {@link BluetoothGatt#discoverServices}
* @param status {@link BluetoothGatt#GATT_SUCCESS} if the remote device
*/
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
// boolean indicating whether or not the next step is successful, default is false
boolean success = false;
// Check if Service discovery was successful and set the LED color to red 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 0x09FF0000
commandCharacteristic.setValue(
new byte[]{
(byte) 0x09, // commandId = set LED color (9)
(byte) 0xFF, // red = 0x00 - 0xFF (0 - 255)
(byte) 0x00, // blue = 0x00 - 0xFF (0 - 255)
(byte) 0x00 // green = 0x00 - 0xFF (0 - 255)
}
);
success = gatt.writeCharacteristic(commandCharacteristic);
}
}
}
onStep(gatt, success);
}
示例5: onDescriptorWrite
import android.bluetooth.BluetoothGatt; //导入方法依赖的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);
}
示例6: writeCharacteristic
import android.bluetooth.BluetoothGatt; //导入方法依赖的package包/类
@Override
public 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;
return gatt.writeCharacteristic(characteristic);
}
示例7: writeCharacteristic
import android.bluetooth.BluetoothGatt; //导入方法依赖的package包/类
/**
* 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() + " (" + getWriteType(characteristic.getWriteType()) + ")");
Logger.d(mLogSession, "gatt.writeCharacteristic(" + characteristic.getUuid() + ")");
return gatt.writeCharacteristic(characteristic);
}
示例8: writeOpCode
import android.bluetooth.BluetoothGatt; //导入方法依赖的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);
}
示例9: writeImageSize
import android.bluetooth.BluetoothGatt; //导入方法依赖的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);
}
示例10: writeInitPacket
import android.bluetooth.BluetoothGatt; //导入方法依赖的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);
}
示例11: onCharacteristicRead
import android.bluetooth.BluetoothGatt; //导入方法依赖的package包/类
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
if(status == BluetoothGatt.GATT_SUCCESS){
if(characteristic.getUuid().equals(UUID_TITAG_GET_BATTERY)){
byte[] mValue = characteristic.getValue();
int mBattery = new Byte(mValue[0]).intValue();
setBattery(mBattery);
byte[] mHumidityValue = {mValue[0]};
mGattnTitagTempHumidityService.getCharacteristic(UUID_TITAG_ENABLE_AIR_TEMP_HUMIDITY).setValue(mHumidityValue);
gatt.writeCharacteristic(mGattnTitagTempHumidityService.getCharacteristic(UUID_TITAG_ENABLE_AIR_TEMP_HUMIDITY));
}
}
}
示例12: sendBufferedData
import android.bluetooth.BluetoothGatt; //导入方法依赖的package包/类
private void sendBufferedData(byte [] bufferedData, BluetoothGatt gatt){
/**
* Called by sendPendingBufferData(). Do not call separately!
*/
for(int i = 0; i < bufferedData.length; i++) {
Log.i("BLE", "buffer value at index " + i + " is "+bufferedData[i]);
}
BluetoothGattCharacteristic characteristic = gatt.getService(Globals.SNACH_SYSTEM_SERVICE_UUID).getCharacteristic(Globals.SNACH_SYSTEM_UART_RX_UUID);
if (characteristic != null) {
hasFinishedWriting = false;
startDataLostChecker();
characteristic.setValue(bufferedData);
gatt.writeCharacteristic(characteristic);
}
}
示例13: writeCharacteristicNonBlock
import android.bluetooth.BluetoothGatt; //导入方法依赖的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;
}