Example usage for org.apache.cordova CordovaArgs getArrayBuffer

List of usage examples for org.apache.cordova CordovaArgs getArrayBuffer

Introduction

In this page you can find the example usage for org.apache.cordova CordovaArgs getArrayBuffer.

Prototype

public byte[] getArrayBuffer(int index) throws JSONException 

Source Link

Usage

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

License:Apache License

@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  www  .  j a  va2 s.c om

    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(NOTIFY)) {

        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(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.google.cordova.ChromeSocket.java

License:Open Source License

private void write(CordovaArgs args, final CallbackContext callbackContext) throws JSONException {
    int socketId = args.getInt(0);
    byte[] data = args.getArrayBuffer(1);

    SocketData sd = sockets.get(Integer.valueOf(socketId));
    if (sd == null) {
        Log.e(LOG_TAG, "No socket with socketId " + socketId);
        return;/*from  w w w. j av a  2s.  c  o  m*/
    }

    int result = sd.write(data);
    if (result <= 0) {
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, result));
    } else {
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));
    }
}

From source file:com.google.cordova.ChromeSocket.java

License:Open Source License

private void sendTo(CordovaArgs args, final CallbackContext context) throws JSONException {
    JSONObject opts = args.getJSONObject(0);
    int socketId = opts.getInt("socketId");
    String address = opts.getString("address");
    int port = opts.getInt("port");
    byte[] data = args.getArrayBuffer(1);

    SocketData sd = sockets.get(Integer.valueOf(socketId));
    if (sd == null) {
        Log.e(LOG_TAG, "No socket with socketId " + socketId);
        return;/*ww w . j a  va  2 s  .  c  o m*/
    }

    int result = sd.sendTo(data, address, port);
    if (result <= 0) {
        context.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, result));
    } else {
        context.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));
    }
}

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

License:Apache License

@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();
    }/*  w  w  w . j av  a  2 s .  c  om*/

    boolean validAction = true;

    if (action.equals(DISCOVER)) {

        int scanSeconds = args.getInt(0);
        findLowEnergyDevices(callbackContext, scanSeconds);

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

        listKnownDevices(callbackContext);

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

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

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

        registerOnDataCallback(callbackContext);

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

        disconnect(callbackContext);

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

        byte[] data = args.getArrayBuffer(0);
        write(callbackContext, data);

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

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

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

        if (activePeripheral != null && activePeripheral.isConnected()) {
            callbackContext.success();
        } else {
            callbackContext.error("Not connected.");
        }

    } else {

        validAction = false;

    }

    return validAction;
}

From source file:io.winch.phonegap.plugin.WinchPlugin.java

License:Open Source License

private void put(CallbackContext callbackContext, CordovaArgs args) {
    try {/*from   w  w w.  java  2  s.  c o  m*/
        String namespace = args.getString(0);
        String key = args.getString(1);
        byte[] data = args.getArrayBuffer(2);

        mWinch.getNamespace(namespace).put(key, data);
        callbackContext.success();
    } catch (JSONException e) {
        e.printStackTrace();
    } catch (WinchError e) {
        e.printStackTrace();
        PluginResult r = buildErrorResult(e.getErrorCode(), e.getMessage());
        callbackContext.sendPluginResult(r);
    }
}

From source file:org.chromium.ChromeBluetoothSocket.java

License:Open Source License

private void send(CordovaArgs args, final CallbackContext callbackContext) throws JSONException {

    int socketId = args.getInt(0);
    byte[] data = args.getArrayBuffer(1);

    ChromeBluetoothSocketSocket socket = sockets.get(socketId);

    if (socket == null) {
        callbackContext.error("Invalid Argument");
        return;//from  w w w. j  ava 2  s  .c  o  m
    }
    socket.send(data, callbackContext);
}

From source file:org.chromium.ChromeUsb.java

License:Open Source License

private static byte[] getByteBufferForTransfer(CordovaArgs args, JSONObject params, int direction)
        throws JSONException {
    if (direction == UsbConstants.USB_DIR_OUT) {
        // OUT transfer requires data positional argument.
        return args.getArrayBuffer(ARG_INDEX_DATA_ARRAYBUFFER);
    } else {//  ww  w . j  ava 2s .  c  o  m
        // IN transfer requires client to pass the length to receive.
        return new byte[params.optInt("length")];
    }
}