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(String value) 

Source Link

Document

Set the locally stored value of this characteristic.

Usage

From source file:de.blinkt.openvpn.bluetooth.service.UartService.java

/***
 * ???//w w w . ja va 2  s  .com
 * @param value  
 * @return
 */
public boolean writeRXCharacteristic(byte[] value) {
    try {
        //mBluetoothGatt?????
        if (mBluetoothGatt == null) {
            Log.e("Blue_Chanl", "????");
            mConnectionState = STATE_DISCONNECTED;
            broadcastUpdate(ACTION_GATT_DISCONNECTED);
            return false;
        }
        BluetoothGattService RxService = null;

        //??
        if (RxService == null) {
            //"6E400001-B5A3-F393-E0A9-E50E24DCCA9E" 
            RxService = mBluetoothGatt.getService(RX_SERVICE_UUID);
        }

        showMessage("mBluetoothGatt null" + mBluetoothGatt);
        if (RxService == null) {
            showMessage("Rx service not found!");
            broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
            return false;
        }

        //???
        BluetoothGattCharacteristic RxChar = null;
        if (RxChar == null) {

            //"6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
            RxChar = RxService.getCharacteristic(RX_CHAR_UUID);
        }

        if (RxChar == null) {
            showMessage("Rx charateristic not found!");
            broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
            return false;
        }
        //???
        RxChar.setValue(value);
        Log.d(TAG, "writeRXCharacteristic: " + value);

        //?
        boolean status = mBluetoothGatt.writeCharacteristic(RxChar);

        Log.e("Blue_Chanl", "write TXchar - status=" + status);
        if (!status) {
            try {
                Thread.sleep(500);
                status = mBluetoothGatt.writeCharacteristic(RxChar);
                Log.e("Blue_Chanl", "??write TXchar - status=" + status);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    } catch (NullPointerException e) {
        e.printStackTrace();
    }
    return true;
}

From source file:com.orange.beaconme_sdk.ble.control.BeaconTagDeviceUpdater.java

private void onReadCharacteristic(BluetoothGattCharacteristic characteristic, BluetoothGatt gatt, int status) {
    Log.i("Updater", "onReadCharacteristic");
    UUID charUUID = characteristic.getUuid();
    for (WriteCharacteristicCommand command : commands) {
        if (command.getCharacteristicUUID().equals(charUUID)) {
            if (characteristic.getValue() == null) {
                Log.e(TAG, String.format("read characteristic %s value=%s, status=%d", characteristic.getUuid(),
                        Arrays.toString(characteristic.getValue()), status));
            } else {
                byte[] newValue;
                if (command.getSwitchState() != WriteCharacteristicCommand.SwitchState.NONE) {
                    newValue = getEnablingValueForChar(characteristic,
                            command.getSwitchState() == WriteCharacteristicCommand.SwitchState.ENABLE);

                } else {
                    newValue = command.getBytesToUpload();
                }//ww w  .j  a  v a2s  .  c  om
                if (!Arrays.equals(characteristic.getValue(), newValue)) {
                    characteristic.setValue(newValue);
                    queue(getWriteCharacteristicOperation(characteristic));
                } else {
                    doneUploadingUuid(characteristic.getUuid());
                }
            }
        }
    }
}

From source file:com.nbplus.iotapp.bluetooth.BluetoothLeService.java

public boolean writeRemoteCharacteristic(String address, String serviceUuid, String characteristicUuid,
        byte[] value) {
    Log.d(TAG, "writeRemoteCharacteristic add = " + address + ", svc = " + serviceUuid + ", char = "
            + characteristicUuid);//from w  ww  .ja  v a  2 s  .  c  o m
    if (StringUtils.isEmptyString(address) || StringUtils.isEmptyString(serviceUuid)
            || StringUtils.isEmptyString(characteristicUuid)) {
        Log.w(TAG, "Unknown parameter");
        return false;
    }
    if (value == null) {
        Log.w(TAG, "value is empty");
        return false;
    }
    BluetoothGatt bluetoothGatt = mConnectedBluetoothGattMap.get(address);

    if (mBluetoothAdapter == null || bluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return false;
    }
    BluetoothGattService service = bluetoothGatt.getService(UUID.fromString(serviceUuid));
    if (service == null) {
        Log.w(TAG, "Service not found.");
        return false;
    }
    BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(characteristicUuid));
    if (characteristic == null) {
        Log.w(TAG, "characteristic not found.");
        return false;
    }

    characteristic.setValue(value);
    boolean result = bluetoothGatt.writeCharacteristic(characteristic);
    Log.d(TAG, "Write charac uuid = " + characteristic.getUuid().toString() + ", result = " + result);

    return result;
}

From source file:com.umundus.service.NCallServiceOld.java

public 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);
    }// w  w  w . j  ava 2s .c  o  m
    characteristic.setValue(locBuffer);
    gatt.writeCharacteristic(characteristic);
    Log.i(TAG, "writePacket " + characteristic.getUuid());
}

From source file:br.liveo.ndrawer.ui.activity.MainActivity.java

License:asdf

public int writeCharacteristic(BluetoothGattCharacteristic characteristic, byte[] b) {
    characteristic.setValue(b);
    bleRequest req = new bleRequest();
    req.status = bleRequestStatus.not_queued;
    req.characteristic = characteristic;
    req.operation = bleRequestOperation.wrBlocking;
    addRequestToQueue(req);/*from  w  ww  .  j  a v a2s.c o  m*/
    boolean finished = false;
    while (!finished) {
        bleRequestStatus stat = pollForStatusofRequest(req);
        if (stat == bleRequestStatus.done) {
            finished = true;
            return 0;
        } else if (stat == bleRequestStatus.timeout) {
            finished = true;
            return -3;
        }
    }
    return -2;
}

From source file:com.umundus.service.NCallServiceOld.java

public void writeRemoconCharacteristic(byte[] value) {

    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "writeRemoconCharacteristic BluetoothAdapter not initialized");
        return;//ww  w. j a  va  2 s  .  c o m
    }

    BluetoothGattService RemoconService = mBluetoothGatt.getService(NCALL_REMOCON_SERVICE);
    if (RemoconService == null) {
        showMessage("key input service not found!");
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_SRC);
        return;
    }

    BluetoothGattCharacteristic RemoconChar = RemoconService
            .getCharacteristic(NCALL_REMOCON_SEND_DATA_CHARACTERISTIC);
    if (RemoconChar == null) {
        showMessage("remocon control charateristic not found!");
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_SRC);
        return;
    }

    mRequestCompleted = false;

    RemoconChar.setValue(value);
    boolean status = mBluetoothGatt.writeCharacteristic(RemoconChar);

    synchronized (mLock) {
        if (!mRequestCompleted && mConnectionState == STATE_DISCOVERED) {
            try {
                mLock.wait(); // onCharacteristicWrite ? notify .
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

From source file:com.umundus.service.NCallServiceOld.java

public void writeRemoconPatternCharacteristic(byte[] value) {

    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "writeRemoconCharacteristic BluetoothAdapter not initialized");
        return;//from  w  w  w .  j a  v  a 2 s  .co m
    }

    BluetoothGattService RemoconService = mBluetoothGatt.getService(NCALL_REMOCON_SERVICE);
    if (RemoconService == null) {
        showMessage("key input service not found!");
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_SRC);
        return;
    }

    BluetoothGattCharacteristic RemoconChar = RemoconService
            .getCharacteristic(NCALL_REMOCON_SEND_DATA_CHARACTERISTIC);
    if (RemoconChar == null) {
        showMessage("remocon control charateristic not found!");
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_SRC);
        return;
    }

    Log.i(TAG, "writeRemoconPatternCharacteristic ++");
    RemoconChar.setValue(value);
    boolean status = mBluetoothGatt.writeCharacteristic(RemoconChar);
    Log.i(TAG, "writeRemoconPatternCharacteristic --");
}

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

/**
 * Writes the operation code 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 .ja v  a  2 s  . c o  m
 * @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
 * @throws DeviceDisconnectedException
 * @throws DfuException
 * @throws UploadAbortedException
 */
private void writeOpCode(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic,
        final byte[] value) throws DeviceDisconnectedException, DfuException, UploadAbortedException {
    mReceivedData = null;
    mErrorState = 0;
    mRequestCompleted = false;

    characteristic.setValue(value);
    gatt.writeCharacteristic(characteristic);

    // We have to wait for confirmation
    try {
        synchronized (mLock) {
            while ((mRequestCompleted == 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 Op Code " + value[0], mErrorState);
    if (mConnectionState != STATE_CONNECTED_AND_READY)
        throw new DeviceDisconnectedException("Unable to write Op Code " + value[0], mConnectionState);
}

From source file:br.liveo.ndrawer.ui.activity.MainActivity.java

License:asdf

public int writeCharacteristic(BluetoothGattCharacteristic characteristic, byte b) {

    byte[] val = new byte[1];
    val[0] = b;//from w ww. j ava2 s. c o m
    characteristic.setValue(val);

    bleRequest req = new bleRequest();
    req.status = bleRequestStatus.not_queued;
    req.characteristic = characteristic;
    req.operation = bleRequestOperation.wrBlocking;
    addRequestToQueue(req);
    boolean finished = false;
    while (!finished) {
        bleRequestStatus stat = pollForStatusofRequest(req);
        if (stat == bleRequestStatus.done) {
            finished = true;
            return 0;
        } else if (stat == bleRequestStatus.timeout) {
            finished = true;
            return -3;
        }
    }
    return -2;
}

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

/**
 * 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.
 * /*from ww w  .  ja va2s . co m*/
 * @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);
    }
    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 (Andorid 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.
}