Example usage for com.facebook.react.bridge ReadableMap getString

List of usage examples for com.facebook.react.bridge ReadableMap getString

Introduction

In this page you can find the example usage for com.facebook.react.bridge ReadableMap getString.

Prototype

@Nullable
    String getString(@NonNull String name);

Source Link

Usage

From source file:com.silklabs.react.blobs.WebSocketModule.java

License:Open Source License

@ReactMethod
public void sendBlob(ReadableMap blob, int id) {
    WebSocket client = mWebSocketConnections.get(id);
    if (client == null) {
        // This is a programmer error
        throw new RuntimeException("Cannot send a message. Unknown WebSocket id " + id);
    }//from ww  w .j  ava2  s  . com
    byte[] data = BlobModule.resolve(blob.getString("blobId"), blob.getInt("offset"), blob.getInt("size"));
    try {
        client.sendMessage(RequestBody.create(WebSocket.BINARY, data));
    } catch (IOException | IllegalStateException e) {
        notifyWebSocketFailed(id, e.getMessage());
    }
}

From source file:com.sogilis.ReactNativeBluetooth.ReactNativeBluetoothModule.java

License:Apache License

@ReactMethod
public void connect(final ReadableMap deviceMap) {
    final String deviceId = deviceMap.getString("id");

    BluetoothAction connectAction = new BluetoothAction(DEVICE_CONNECTED, deviceId, eventEmitter) {
        @Override//w w w .  ja v  a 2  s .c  o m
        public void run() throws BluetoothException {
            BluetoothDevice device = discoveredDevices.get(deviceId);
            Set<BluetoothDevice> bondedDevices = bluetoothAdapter.getBondedDevices();
            if (!bondedDevices.contains(device)) {
                if (!device.createBond()) {
                    throw new BluetoothException("Error when requesting device pairing");
                }
            } else {
                device.connectGatt(getReactApplicationContext(), false, gattCallback);
            }
        }
    };

    bluetoothActionsLoop.addAction(connectAction);
}

From source file:com.sogilis.ReactNativeBluetooth.ReactNativeBluetoothModule.java

License:Apache License

@ReactMethod
public void disconnect(final ReadableMap deviceMap) {
    final String deviceId = deviceMap.getString("id");

    BluetoothAction disconnectAction = new BluetoothAction(DEVICE_DISCONNECTED, deviceId, eventEmitter) {
        @Override/*from  w ww .ja v  a2s  .  c  o m*/
        public void run() throws BluetoothException {
            BluetoothGatt gatt = gattCollection.get(deviceId);
            gatt.disconnect();
            bluetoothActionsLoop.actionDone();
        }
    };

    bluetoothActionsLoop.addAction(disconnectAction);
}

From source file:com.sogilis.ReactNativeBluetooth.ReactNativeBluetoothModule.java

License:Apache License

@ReactMethod
public void discoverServices(final ReadableMap deviceMap, final ReadableArray serviceIds) {
    final String deviceId = deviceMap.getString("id");

    BluetoothAction discoverServicesAction = new BluetoothAction(SERVICE_DISCOVERY_STARTED, deviceId,
            eventEmitter) {/*from  w w  w.  j ava 2s  .  c o  m*/
        @Override
        public void run() throws BluetoothException {
            BluetoothGatt gatt = gattCollection.get(deviceId);

            if (gatt.discoverServices()) {
                emit(serviceDiscoveryStarted(gatt.getDevice()));
            } else {
                throw new BluetoothException("Error when requesting services discovery");
            }
        }
    };

    bluetoothActionsLoop.addAction(discoverServicesAction);
}

From source file:com.sogilis.ReactNativeBluetooth.ReactNativeBluetoothModule.java

License:Apache License

@ReactMethod
public void discoverCharacteristics(final ReadableMap serviceMap, final ReadableArray characteristicIds) {
    final String deviceId = serviceMap.getString("deviceId");
    final String serviceId = serviceMap.getString("id");

    BluetoothAction discoverAction = new BluetoothAction(CHARACTERISTIC_DISCOVERY_STARTED, deviceId,
            eventEmitter) {/*from   w ww .  java  2s. c  o m*/
        @Override
        public void run() throws BluetoothException {
            BluetoothDevice device = discoveredDevices.get(deviceId);
            BluetoothGatt gatt = gattCollection.get(device);
            BluetoothGattService service = findServiceById(gatt, serviceId);

            emit(characteristicDiscoveryStarted(device, service));
            emit(characteristicsDiscovered(device, service,
                    filterCharacteristics(device, service, characteristicIds)));
            bluetoothActionsLoop.actionDone();
        }
    };

    bluetoothActionsLoop.addAction(discoverAction);
}

From source file:com.sogilis.ReactNativeBluetooth.ReactNativeBluetoothModule.java

License:Apache License

@ReactMethod
public void readCharacteristicValue(final ReadableMap characteristicMap) {
    final String deviceId = characteristicMap.getString("deviceId");
    final String serviceId = characteristicMap.getString("serviceId");
    final String characteristicId = characteristicMap.getString("id");

    BluetoothAction readAction = new BluetoothAction(CHARACTERISTIC_READ, deviceId, characteristicId,
            eventEmitter) {/*from  w  w  w  . j  a va  2  s . com*/
        @Override
        public void run() throws BluetoothException {
            BluetoothGatt gatt = gattCollection.get(deviceId);
            BluetoothGattCharacteristic characteristic = findCharacteristic(gatt, serviceId, characteristicId,
                    BluetoothGattCharacteristic.PROPERTY_READ);

            if (!gatt.readCharacteristic(characteristic)) {
                throw new BluetoothException("Error when requesting characteristic read");
            }
        }
    };

    bluetoothActionsLoop.addAction(readAction);
}

From source file:com.sogilis.ReactNativeBluetooth.ReactNativeBluetoothModule.java

License:Apache License

@ReactMethod
public void writeCharacteristicValue(final ReadableMap characteristicMap, final String base64Value,
        final boolean withResponse) {
    final String deviceId = characteristicMap.getString("deviceId");
    final String serviceId = characteristicMap.getString("serviceId");
    final String characteristicId = characteristicMap.getString("id");

    BluetoothAction writeAction = new BluetoothAction(CHARACTERISTIC_WRITTEN, deviceId, characteristicId,
            eventEmitter) {// w  ww .  ja v  a2s.  c  om
        @Override
        public void run() throws BluetoothException {
            BluetoothGatt gatt = gattCollection.get(deviceId);
            BluetoothGattCharacteristic characteristic = findCharacteristic(gatt, serviceId, characteristicId,
                    BluetoothGattCharacteristic.PROPERTY_WRITE);

            characteristic.setValue(Base64.decode(base64Value, Base64.DEFAULT));

            if (withResponse) {
                characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
            } else {
                characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
            }

            if (!gatt.writeCharacteristic(characteristic)) {
                throw new BluetoothException("Error when requesting characteristic write");
            }
        }
    };

    bluetoothActionsLoop.addAction(writeAction);
}

From source file:com.sogilis.ReactNativeBluetooth.ReactNativeBluetoothModule.java

License:Apache License

@ReactMethod
public void subscribeToNotification(final ReadableMap characteristicMap) {
    final String deviceId = characteristicMap.getString("deviceId");
    final String serviceId = characteristicMap.getString("serviceId");
    final String characteristicId = characteristicMap.getString("id");

    BluetoothAction subscribeAction = new BluetoothAction(CHARACTERISTIC_NOTIFIED, deviceId, characteristicId,
            eventEmitter) {//from  w ww . ja v  a 2  s.c om
        @Override
        public void run() throws BluetoothException {
            BluetoothGatt gatt = gattCollection.get(deviceId);
            BluetoothGattCharacteristic characteristic = findCharacteristic(gatt, serviceId, characteristicId,
                    BluetoothGattCharacteristic.PROPERTY_NOTIFY);

            if (enableNotification(gatt, characteristic)) {
                Log.d(MODULE_NAME, "Notification enabled");
            } else {
                throw new BluetoothException("Error when enabling characteristic notification");
            }
        }
    };

    bluetoothActionsLoop.addAction(subscribeAction);
}

From source file:com.sogilis.ReactNativeBluetooth.ReactNativeBluetoothModule.java

License:Apache License

@ReactMethod
public void unsubscribeFromNotification(final ReadableMap characteristicMap) {
    final String deviceId = characteristicMap.getString("deviceId");
    final String serviceId = characteristicMap.getString("serviceId");
    final String characteristicId = characteristicMap.getString("id");

    BluetoothAction unsubscribeAction = new BluetoothAction(CHARACTERISTIC_NOTIFIED, deviceId, characteristicId,
            eventEmitter) {//from   w w w .ja va 2 s  . co m
        @Override
        public void run() throws BluetoothException {
            BluetoothGatt gatt = gattCollection.get(deviceId);
            BluetoothGattCharacteristic characteristic = findCharacteristic(gatt, serviceId, characteristicId,
                    BluetoothGattCharacteristic.PROPERTY_NOTIFY);

            if (disableNotification(gatt, characteristic)) {
                Log.d(MODULE_NAME, "Notification disabled");
            } else {
                throw new BluetoothException("Error when disabling characteristic notification");
            }
        }
    };

    bluetoothActionsLoop.addAction(unsubscribeAction);
}

From source file:com.testproject.webview.XWalkViewManager.java

License:Open Source License

@ReactProp(name = "source")
public void setSource(XWalkView view, @Nullable ReadableMap source) {
    if (source != null) {
        if (source.hasKey("html")) {
            String html = source.getString("html");
            if (source.hasKey("baseUrl")) {
                view.load(source.getString("baseUrl"), html);
            } else {
                view.load(null, html);//  ww  w  . j  av a 2 s .c o m
            }
            return;
        }
        if (source.hasKey("uri")) {
            String url = source.getString("uri");
            if (source.hasKey("method")) {
                String method = source.getString("method");
                if (method.equals(HTTP_METHOD_POST)) {
                    byte[] postData = null;
                    if (source.hasKey("body")) {
                        String body = source.getString("body");
                        try {
                            postData = body.getBytes("UTF-8");
                        } catch (UnsupportedEncodingException e) {
                            postData = body.getBytes();
                        }
                    }
                    if (postData == null) {
                        postData = new byte[0];
                    }
                    //                        view.postUrl(url, postData);
                    return;
                }
            }
            HashMap<String, String> headerMap = new HashMap<>();
            if (source.hasKey("headers")) {
                ReadableMap headers = source.getMap("headers");
                ReadableMapKeySetIterator iter = headers.keySetIterator();
                while (iter.hasNextKey()) {
                    String key = iter.nextKey();
                    headerMap.put(key, headers.getString(key));
                }
            }
            //                view.loadUrl(url, headerMap);
            view.load(url, null);
            return;
        }
    }
    view.load(null, BLANK_URL);
}