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:com.sdingba.su.alphabet_demotest.view.lanya.UartService.java

/**
 * ?   ?//  w w  w . ja  v a 2 s  .  c  o m
 * @param value
 */
public void writeRXCharacteristic(byte[] value) {

    if (mBluetoothGatt == null) {
        showMessage("SDingBaLanYan : mBluetoothGatt(is null) = null" + mBluetoothGatt);
        Log.d(TAG, "SDingBaLanYan : mBluetoothGatt(is null) = null" + mBluetoothGatt);

        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
        return;
    }

    BluetoothGattService RxService = mBluetoothGatt.getService(RX_SERVICE_UUID);
    if (RxService == null) {
        showMessage("SDingBaLanYan : Rx service not found!---RX_SERVICE_UUID");
        Log.d(TAG, "SDingBaLanYan : Rx service not found!---RX_SERVICE_UUID");
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
        return;
    }

    BluetoothGattCharacteristic RxChar = RxService.getCharacteristic(RX_CHAR_UUID);
    if (RxChar == null) {
        showMessage(" ==Rx== charateristic not found!");
        Log.d(TAG, " ==Rx== charateristic not found!");
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
        return;
    }

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

    Log.d(TAG, "SDingBaLanYan ???write TXchar - status=" + status);
}

From source file:com.guangyao.bluetoothtest.service.BluetoothLeService.java

/**
 * @brief writeRXCharacteristic//from w  w  w  .  j av  a2 s  .  c o m
 */
public boolean writeRXCharacteristic(byte[] value) {
    BluetoothGattService RxService = mBluetoothGatt.getService(RX_SERVICE_UUID);
    if (RxService == null) {
        return false;
    }

    BluetoothGattCharacteristic RxChar = RxService.getCharacteristic(RX_CHAR_UUID);
    if (RxChar == null) {
        return false;
    }
    RxChar.setValue(value);
    boolean status = mBluetoothGatt.writeCharacteristic(RxChar);
    Log.d(TAG, "Send commandstatus" + status + "-->" + DataHandlerUtils.bytesToHexStr(value));
    return status;
}

From source file:com.github.akinaru.bleanalyzer.bluetooth.BluetoothCustomManager.java

@SuppressLint("NewApi")
@Override/*from   w w  w .ja v  a 2  s  . c o m*/
public void writeCharacteristic(String characUid, byte[] value, BluetoothGatt gatt, IPushListener listener) {

    if (gatt != null && characUid != null && value != null) {

        gattThreadPool.execute(new GattTask(gatt, characUid, value, listener) {
            @Override
            public void run() {

                BluetoothGattCharacteristic charac = GattUtils.getCharacteristic(getGatt().getServices(),
                        getUid());
                //charac.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
                charac.setValue(getValue());

                getGatt().writeCharacteristic(charac);

                long startTime = System.currentTimeMillis();
                eventManager.reset();
                try {
                    eventManager.waitOne(BT_TIMEOUT);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                long endTime = System.currentTimeMillis();

                if ((endTime - startTime) >= BT_TIMEOUT) {
                    if (getListener() != null) {
                        getListener().onPushFailure();
                    }
                } else {
                    if (getListener() != null) {
                        getListener().onPushSuccess();
                    }
                }
            }
        });
        gattThreadPool.execute(new Runnable() {
            @Override
            public void run() {

            }
        });
    } else
        Log.e(TAG, "Error int writeCharacteristic() input argument NULL");
}

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;/*www  .  j a  va  2s  .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.google.android.apps.forscience.ble.MyBleService.java

void writeValue(String address, BluetoothGattCharacteristic theCharacteristic, byte[] value) {
    BluetoothGatt bluetoothGatt = addressToGattClient.get(address);
    if (bluetoothGatt == null) {
        Log.w(TAG, "No connection found for: " + address);
        sendGattBroadcast(address, BleEvents.WRITE_CHAR_FAIL, null);
        return;/*from  ww w  .jav  a 2 s .c  om*/
    }
    theCharacteristic.setValue(value);
    bluetoothGatt.writeCharacteristic(theCharacteristic);
}

From source file:com.example.bluetoothmodule.TreadmillService.java

public void writeRXCharacteristic(byte[] value) {
    BluetoothGattService RxService = mBluetoothGatt.getService(UUID_TREADMILL_SERVICE);
    showMessage("mBluetoothGatt null" + mBluetoothGatt);
    if (RxService == null) {
        showMessage("Rx service not found!");
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
        return;/*from   w ww. jav  a  2s . c  o  m*/
    }
    BluetoothGattCharacteristic RxChar = RxService.getCharacteristic(RX_CHAR_UUID);
    if (RxChar == null) {
        showMessage("Rx charateristic not found!");
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
        return;
    }
    RxChar.setValue(value);
    boolean status = mBluetoothGatt.writeCharacteristic(RxChar);

    Log.d(TAG, "write TXchar - status=" + status);
    Log.d("Chandler", "write TXchar - status=" + status);
}

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  ava2s.c o  m*/
    }

    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:org.bcsphere.bluetooth.BluetoothG43plus.java

@Override
public void writeValue(JSONArray json, CallbackContext callbackContext) {
    Log.i(TAG, "writeValue");
    String deviceAddress = Tools.getData(json, Tools.DEVICE_ADDRESS);
    if (connectedDevice.get(deviceAddress) == null) {
        Tools.sendErrorMsg(callbackContext);
        return;//from ww  w  .  j ava  2 s .c o  m
    }
    int serviceIndex = Integer.parseInt(Tools.getData(json, Tools.SERVICE_INDEX));
    int characteristicIndex = Integer.parseInt(Tools.getData(json, Tools.CHARACTERISTIC_INDEX));
    String descriptorIndex = Tools.getData(json, Tools.DESCRIPTOR_INDEX);
    String writeValue = Tools.getData(json, Tools.WRITE_VALUE);
    writeValueCC.put(deviceAddress, callbackContext);
    if (descriptorIndex.equals("")) {
        BluetoothGattCharacteristic characteristic = deviceServices.get(deviceAddress).get(serviceIndex)
                .getCharacteristics().get(characteristicIndex);
        characteristic.setValue(Tools.decodeBase64(writeValue));
        mBluetoothGatts.get(deviceAddress).writeCharacteristic(characteristic);
    } else {
        BluetoothGattDescriptor descriptor = deviceServices.get(deviceAddress).get(serviceIndex)
                .getCharacteristics().get(characteristicIndex).getDescriptors()
                .get(Integer.parseInt(descriptorIndex));
        descriptor.setValue(Tools.decodeBase64(writeValue));
        mBluetoothGatts.get(deviceAddress).writeDescriptor(descriptor);
    }
}

From source file:org.bcsphere.bluetooth.BluetoothG43plus.java

@Override
public void addServices(JSONArray json, CallbackContext callbackContext) {
    Log.i(TAG, "addServices");
    if (!isOpenGattServer) {
        mBluetoothGattServer = mBluetoothManager.openGattServer(mContext, mGattServerCallback);
        isOpenGattServer = true;/*ww w. j a v  a  2 s.  c  o  m*/
    }
    addServiceCC = callbackContext;
    JSONArray services = Tools.getArray(json, Tools.SERVICES);
    gattServerSum = services.length();
    for (int i = 0; i < services.length(); i++) {
        String uniqueID = Tools.getData(services, i, Tools.UINQUE_ID);
        int serviceType = -1;
        if (Tools.getData(services, i, Tools.SERVICE_TYPE).equals("0")) {
            serviceType = BluetoothGattService.SERVICE_TYPE_PRIMARY;
        } else {
            serviceType = BluetoothGattService.SERVICE_TYPE_SECONDARY;
        }
        UUID serviceUUID = UUID.fromString(Tools.getData(services, i, Tools.SERVICE_UUID));
        BluetoothGattService service = new BluetoothGattService(serviceUUID, serviceType);
        JSONArray characteristics = Tools.getArray(services, i, Tools.CHARACTERISTICS);
        for (int j = 0; j < characteristics.length(); j++) {
            byte[] characteristicValue = Tools
                    .decodeBase64(Tools.getData(characteristics, Tools.CHARACTERISTIC_VALUE));
            UUID characteristicUUID = UUID
                    .fromString(Tools.getData(characteristics, Tools.CHARACTERISTIC_UUID));
            int characteristicProperty = Tools
                    .encodeProperty(Tools.getArray(characteristics, Tools.CHARACTERISTIC_PROPERTY));
            int characteristicPermission = Tools
                    .encodePermission(Tools.getArray(characteristics, Tools.CHARACTERISTIC_PERMISSION));
            BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(characteristicUUID,
                    characteristicProperty, characteristicPermission);
            characteristic.setValue(characteristicValue);
            JSONArray descriptors = Tools.getArray(characteristics, j, Tools.DESCRIPTORS);
            for (int k = 0; k < descriptors.length(); k++) {
                byte[] descriptorValue = Tools.decodeBase64(Tools.getData(descriptors, Tools.DESCRIPTOR_VALUE));
                UUID descriptorUUID = UUID.fromString(Tools.getData(descriptors, Tools.DESCRIPTOR_UUID));
                int descriptorPermission = Tools
                        .encodePermission(Tools.getArray(descriptors, Tools.DESCRIPTOR_PERMISSION));
                BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(descriptorUUID,
                        descriptorPermission);
                descriptor.setValue(descriptorValue);
                characteristic.addDescriptor(descriptor);
            }
            service.addCharacteristic(characteristic);
        }
        if (mBluetoothGattServer.addService(service)) {
            localServices.put(uniqueID, service);
        }
    }
}

From source file:com.evothings.BLE.java

private void writeCharacteristic(final CordovaArgs args, final CallbackContext callbackContext)
        throws JSONException {
    final GattHandler gh = mGatt.get(args.getInt(0));
    gh.mOperations.add(new Runnable() {
        @Override//  ww w  .  ja  v a2 s  .co  m
        public void run() {
            try {
                gh.mCurrentOpContext = callbackContext;
                BluetoothGattCharacteristic c = gh.mCharacteristics.get(args.getInt(1));
                System.out.println("writeCharacteristic(" + args.getInt(0) + ", " + args.getInt(1) + ", "
                        + args.getString(2) + ")");
                c.setValue(args.getArrayBuffer(2));
                if (!gh.mGatt.writeCharacteristic(c)) {
                    gh.mCurrentOpContext = null;
                    callbackContext.error("writeCharacteristic");
                    gh.process();
                }
            } catch (JSONException e) {
                e.printStackTrace();
                callbackContext.error(e.toString());
                gh.process();
            }
        }
    });
    gh.process();
}