Example usage for android.bluetooth BluetoothGattCharacteristic WRITE_TYPE_DEFAULT

List of usage examples for android.bluetooth BluetoothGattCharacteristic WRITE_TYPE_DEFAULT

Introduction

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

Prototype

int WRITE_TYPE_DEFAULT

To view the source code for android.bluetooth BluetoothGattCharacteristic WRITE_TYPE_DEFAULT.

Click Source Link

Document

Write characteristic, requesting acknoledgement by the remote device

Usage

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

@Override
public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) throws JSONException {

    LOG.d(TAG, "action = " + action);

    if (bluetoothAdapter == null) {
        Activity activity = cordova.getActivity();
        BluetoothManager bluetoothManager = (BluetoothManager) activity
                .getSystemService(Context.BLUETOOTH_SERVICE);
        bluetoothAdapter = bluetoothManager.getAdapter();
    }/*from   ww w  .  j  a  v  a  2  s.c o  m*/

    boolean validAction = true;

    if (action.equals(SCAN)) {

        UUID[] serviceUUIDs = parseServiceUUIDList(args.getJSONArray(0));
        int scanSeconds = args.getInt(1);
        findLowEnergyDevices(callbackContext, serviceUUIDs, scanSeconds);

    } else if (action.equals(START_SCAN)) {

        UUID[] serviceUUIDs = parseServiceUUIDList(args.getJSONArray(0));
        findLowEnergyDevices(callbackContext, serviceUUIDs, -1);

    } else if (action.equals(STOP_SCAN)) {

        bluetoothAdapter.stopLeScan(this);
        callbackContext.success();

    } else if (action.equals(LIST)) {

        listKnownDevices(callbackContext);

    } else if (action.equals(CONNECT)) {

        String macAddress = args.getString(0);
        connect(callbackContext, macAddress);

    } else if (action.equals(DISCONNECT)) {

        String macAddress = args.getString(0);
        disconnect(callbackContext, macAddress);

    } else if (action.equals(READ)) {

        String macAddress = args.getString(0);
        UUID serviceUUID = uuidFromString(args.getString(1));
        UUID characteristicUUID = uuidFromString(args.getString(2));
        read(callbackContext, macAddress, serviceUUID, characteristicUUID);

    } else if (action.equals(WRITE)) {

        String macAddress = args.getString(0);
        UUID serviceUUID = uuidFromString(args.getString(1));
        UUID characteristicUUID = uuidFromString(args.getString(2));
        byte[] data = args.getArrayBuffer(3);
        int type = BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT;
        write(callbackContext, macAddress, serviceUUID, characteristicUUID, data, type);

    } else if (action.equals(WRITE_WITHOUT_RESPONSE)) {

        String macAddress = args.getString(0);
        UUID serviceUUID = uuidFromString(args.getString(1));
        UUID characteristicUUID = uuidFromString(args.getString(2));
        byte[] data = args.getArrayBuffer(3);
        int type = BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE;
        write(callbackContext, macAddress, serviceUUID, characteristicUUID, data, type);

    } else if (action.equals(START_NOTIFICATION)) {

        String macAddress = args.getString(0);
        UUID serviceUUID = uuidFromString(args.getString(1));
        UUID characteristicUUID = uuidFromString(args.getString(2));
        registerNotifyCallback(callbackContext, macAddress, serviceUUID, characteristicUUID);

    } else if (action.equals(STOP_NOTIFICATION)) {

        String macAddress = args.getString(0);
        UUID serviceUUID = uuidFromString(args.getString(1));
        UUID characteristicUUID = uuidFromString(args.getString(2));
        removeNotifyCallback(callbackContext, macAddress, serviceUUID, characteristicUUID);

    } else if (action.equals(IS_ENABLED)) {

        if (bluetoothAdapter.isEnabled()) {
            callbackContext.success();
        } else {
            callbackContext.error("Bluetooth is disabled.");
        }

    } else if (action.equals(IS_CONNECTED)) {

        String macAddress = args.getString(0);

        if (peripherals.containsKey(macAddress) && peripherals.get(macAddress).isConnected()) {
            callbackContext.success();
        } else {
            callbackContext.error("Not connected.");
        }

    } else if (action.equals(SETTINGS)) {

        Intent intent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS);
        cordova.getActivity().startActivity(intent);
        callbackContext.success();

    } else if (action.equals(ENABLE)) {

        enableBluetoothCallback = callbackContext;
        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        cordova.startActivityForResult(this, intent, REQUEST_ENABLE_BLUETOOTH);

    } else {

        validAction = false;

    }

    return validAction;
}

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;/*w  ww .  j ava  2  s.  co  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.wolkabout.hexiwear.service.BluetoothService.java

public void setTime() {
    Log.d(TAG, "Setting time...");
    if (!isConnected || alertIn == null) {
        Log.w(TAG, "Time not set.");
        return;/*from  w ww  .  java 2 s  . c  o m*/
    }

    final byte[] time = new byte[20];
    final long currentTime = System.currentTimeMillis();
    final long currentTimeWithTimeZoneOffset = (currentTime + TimeZone.getDefault().getOffset(currentTime))
            / 1000;

    final ByteBuffer buffer = ByteBuffer.allocate(8);
    buffer.order(ByteOrder.LITTLE_ENDIAN).asLongBuffer().put(currentTimeWithTimeZoneOffset);
    final byte[] utcBytes = buffer.array();

    final byte length = 0x04;

    time[0] = WRITE_TIME;
    time[1] = length;
    time[2] = utcBytes[0];
    time[3] = utcBytes[1];
    time[4] = utcBytes[2];
    time[5] = utcBytes[3];

    alertIn.setValue(time);
    alertIn.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
    bluetoothGatt.writeCharacteristic(alertIn);
}

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

private void processCommands() {
    LOG.d(TAG, "Processing Commands");

    if (bleProcessing) {
        return;//from   ww w  . j  a v  a 2 s. c om
    }

    BLECommand command = commandQueue.poll();
    if (command != null) {
        if (command.getType() == BLECommand.READ) {
            LOG.d(TAG, "Read " + command.getCharacteristicUUID());
            bleProcessing = true;
            readCharacteristic(command.getCallbackContext(), command.getServiceUUID(),
                    command.getCharacteristicUUID());
        } else if (command.getType() == BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT) {
            LOG.d(TAG, "Write " + command.getCharacteristicUUID());
            bleProcessing = true;
            writeCharacteristic(command.getCallbackContext(), command.getServiceUUID(),
                    command.getCharacteristicUUID(), command.getData(), command.getType());
        } else if (command.getType() == BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE) {
            LOG.d(TAG, "Write No Response " + command.getCharacteristicUUID());
            bleProcessing = true;
            writeCharacteristic(command.getCallbackContext(), command.getServiceUUID(),
                    command.getCharacteristicUUID(), command.getData(), command.getType());
        } else if (command.getType() == BLECommand.REGISTER_NOTIFY) {
            LOG.d(TAG, "Register Notify " + command.getCharacteristicUUID());
            bleProcessing = true;
            registerNotifyCallback(command.getCallbackContext(), command.getServiceUUID(),
                    command.getCharacteristicUUID());
        } else if (command.getType() == BLECommand.REMOVE_NOTIFY) {
            LOG.d(TAG, "Remove Notify " + command.getCharacteristicUUID());
            bleProcessing = true;
            removeNotifyCallback(command.getCallbackContext(), command.getServiceUUID(),
                    command.getCharacteristicUUID());
        } else {
            // this shouldn't happen
            throw new RuntimeException("Unexpected BLE Command type " + command.getType());
        }
    } else {
        LOG.d(TAG, "Command Queue is empty.");
    }

}