Example usage for android.bluetooth BluetoothGattCharacteristic setValue

List of usage examples for android.bluetooth BluetoothGattCharacteristic setValue

Introduction

In this page you can find the example usage for android.bluetooth BluetoothGattCharacteristic setValue.

Prototype

public boolean setValue(int value, int formatType, int offset) 

Source Link

Document

Set the locally stored value of this characteristic.

Usage

From source file:no.nordicsemi.android.nrftoolbox.dfu.DfuService.java

/**
 * Writes the image size to the characteristic. This method is SYNCHRONOUS and wait until the {@link BluetoothGattCallback#onCharacteristicWrite(BluetoothGatt, 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.
 * /*from ww  w  .  ja va 2s .com*/
 * @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;
    mErrorState = 0;
    mImageSizeSent = false;

    characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
    characteristic.setValue(imageSize, BluetoothGattCharacteristic.FORMAT_UINT32, 0);
    gatt.writeCharacteristic(characteristic);

    // We have to wait for confirmation
    try {
        synchronized (mLock) {
            while ((mImageSizeSent == false && mConnectionState == STATE_CONNECTED_AND_READY && mErrorState == 0
                    && !mAborted) || mPaused)
                mLock.wait();
        }
    } catch (final InterruptedException e) {
        loge("Sleeping interrupted", e);
    }
    if (mAborted)
        throw new UploadAbortedException();
    if (mErrorState != 0)
        throw new DfuException("Unable to write Image Size", mErrorState);
    if (mConnectionState != STATE_CONNECTED_AND_READY)
        throw new DeviceDisconnectedException("Unable to write Image Size", mConnectionState);
}