Example usage for android.bluetooth BluetoothGattCharacteristic setWriteType

List of usage examples for android.bluetooth BluetoothGattCharacteristic setWriteType

Introduction

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

Prototype

public void setWriteType(int writeType) 

Source Link

Document

Set the write type for this characteristic

Setting the write type of a characteristic determines how the BluetoothGatt#writeCharacteristic function write this characteristic.

Usage

From source file:com.megster.cordova.rfduino.RFduinoPlugin.java

private void write(CallbackContext callbackContext, byte[] data) {

    BluetoothGattCharacteristic characteristic = activePeripheral.getSendCharacteristic();
    characteristic.setValue(data);//from w ww .j a  v  a2  s.c  o  m
    characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
    boolean success = gatt.writeCharacteristic(characteristic);

    if (success) {
        callbackContext.success();
    } else {
        callbackContext.error("Write Failed");
    }

}

From source file:com.megster.cordova.rfduino.RFduinoPlugin.java

private void disconnect(CallbackContext callbackContext) {

    if (activePeripheral != null) {
        BluetoothGattCharacteristic characteristic = activePeripheral.getDisconnectCharacteristic();
        characteristic.setValue("");
        characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
        gatt.writeCharacteristic(characteristic);
        activePeripheral.disconnect();//from   ww w . j a v  a 2 s  .com
    }

    if (gatt != null) {
        gatt.close();
        gatt = null;
    }

    callbackContext.success();
}

From source file:com.example.bluetooth.RFduinoService.java

public boolean send(byte[] data) {
    if (mBluetoothGatt == null || mBluetoothGattService == null) {
        Log.w(TAG, "BluetoothGatt not initialized");
        return false;
    }//ww  w.  ja  v  a  2  s . c om

    BluetoothGattCharacteristic characteristic = mBluetoothGattService.getCharacteristic(UUID_SEND);

    if (characteristic == null) {
        Log.w(TAG, "Send characteristic not found");
        return false;
    }

    characteristic.setValue(data);
    characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
    return mBluetoothGatt.writeCharacteristic(characteristic);
}

From source file:com.example.bluetooth.RFduinoService.java

public boolean disconnectRFduino() {
    if (mBluetoothGatt == null || mBluetoothGattService == null) {
        Log.w(TAG, "BluetoothGatt not initialized");
        return false;
    }//from w w  w .ja  v a 2s  .c o m

    BluetoothGattCharacteristic characteristic = mBluetoothGattService.getCharacteristic(UUID_DISCONNECT);

    if (characteristic == null) {
        Log.w(TAG, "Disconnect characteristic not found");
        return false;
    }

    characteristic.setValue("");
    characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
    return mBluetoothGatt.writeCharacteristic(characteristic);
}

From source file:com.ble.BLService.java

public void writeCharacteristic(BluetoothGattCharacteristic characteristic, String value) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized Characteristic");
        return;/*from   w w  w.j a va2s  .  c  o  m*/
    }
    if (UUID_STM32_WRITE_TO_DEVICE.equals(characteristic.getUuid())) {
        add = value;
        byte[] dataByte = value.getBytes();
        characteristic.setValue(dataByte);
        characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
        mBluetoothGatt.writeCharacteristic(characteristic);
    }
}

From source file:com.megster.cordova.ble.central.Peripheral.java

private void writeCharacteristic(CallbackContext callbackContext, UUID serviceUUID, UUID characteristicUUID,
        byte[] data, int writeType) {

    boolean success = false;

    if (gatt == null) {
        callbackContext.error("BluetoothGatt is null");
        return;/*w  w  w  .  j  a va2  s .  com*/
    }

    BluetoothGattService service = gatt.getService(serviceUUID);
    BluetoothGattCharacteristic characteristic = findWritableCharacteristic(service, characteristicUUID,
            writeType);

    if (characteristic == null) {
        callbackContext.error("Characteristic " + characteristicUUID + " not found.");
    } else {
        characteristic.setValue(data);
        characteristic.setWriteType(writeType);
        writeCallback = callbackContext;

        if (gatt.writeCharacteristic(characteristic)) {
            success = true;
        } else {
            writeCallback = null;
            callbackContext.error("Write failed");
        }
    }

    if (!success) {
        commandCompleted();
    }

}

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 w ww. j a v a 2s. c  o m*/
 * @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);
}