Example usage for android.bluetooth BluetoothDevice getAddress

List of usage examples for android.bluetooth BluetoothDevice getAddress

Introduction

In this page you can find the example usage for android.bluetooth BluetoothDevice getAddress.

Prototype

public String getAddress() 

Source Link

Document

Returns the hardware address of this BluetoothDevice.

Usage

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

@Override
public boolean execute(final String action, final JSONArray json, final CallbackContext callbackContext)
        throws JSONException {
    try {/*w  w  w.  ja  v a  2  s.co m*/
        if (bluetoothAPI != null) {
            if (isSetContext) {
                bluetoothAPI.setContext(myContext);
                isSetContext = false;
            }
            if (action.equals("getCharacteristics")) {
                bluetoothAPI.getCharacteristics(json, callbackContext);
            } else if (action.equals("getDescriptors")) {
                bluetoothAPI.getDescriptors(json, callbackContext);
            } else if (action.equals("removeServices")) {
                bluetoothAPI.removeServices(json, callbackContext);
            }
            if (action.equals("stopScan")) {
                bluetoothAPI.stopScan(json, callbackContext);
            } else if (action.equals("getConnectedDevices")) {
                bluetoothAPI.getConnectedDevices(json, callbackContext);
            }

            cordova.getThreadPool().execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        if (action.equals("startScan")) {
                            bluetoothAPI.startScan(json, callbackContext);
                        } else if (action.equals("connect")) {
                            bluetoothAPI.connect(json, callbackContext);
                        } else if (action.equals("disconnect")) {
                            bluetoothAPI.disconnect(json, callbackContext);
                        } else if (action.equals("getServices")) {
                            bluetoothAPI.getServices(json, callbackContext);
                        } else if (action.equals("writeValue")) {
                            bluetoothAPI.writeValue(json, callbackContext);
                        } else if (action.equals("readValue")) {
                            bluetoothAPI.readValue(json, callbackContext);
                        } else if (action.equals("setNotification")) {
                            bluetoothAPI.setNotification(json, callbackContext);
                        } else if (action.equals("getDeviceAllData")) {
                            bluetoothAPI.getDeviceAllData(json, callbackContext);
                        } else if (action.equals("addServices")) {
                            bluetoothAPI.addServices(json, callbackContext);
                        } else if (action.equals("getRSSI")) {
                            bluetoothAPI.getRSSI(json, callbackContext);
                        }
                    } catch (Exception e) {
                        Tools.sendErrorMsg(callbackContext);
                    } catch (Error e) {
                        Tools.sendErrorMsg(callbackContext);
                    }
                }
            });
        }

        if (action.equals("addEventListener")) {
            String eventName = Tools.getData(json, Tools.EVENT_NAME);
            if (eventName.equals("newadvpacket")) {
                newadvpacketContext = callbackContext;
            } else if (eventName.equals("disconnect")) {
                disconnectContext = callbackContext;
            }
            if (bluetoothAPI != null) {
                bluetoothAPI.addEventListener(json, callbackContext);
            }
            return true;
        }

        if (action.equals("getEnvironment")) {
            JSONObject jo = new JSONObject();
            if (this.webView.page != null) {
                jo.put("deviceAddress", this.webView.page.deviceAddress);
                jo.put("deviceType", this.webView.page.deviceType);
                jo.put("api", versionOfAPI);
            } else {
                jo.put("deviceAddress", "N/A");
                jo.put("deviceType", "N/A");
                jo.put("api", versionOfAPI);
            }
            callbackContext.success(jo);
            return true;
        }
        if (action.equals("openBluetooth")) {
            if (!bluetoothAdapter.isEnabled()) {
                bluetoothAdapter.enable();
            }
            Tools.sendSuccessMsg(callbackContext);
            return true;
        }
        if (action.equals("getBluetoothState")) {
            Log.i(TAG, "getBluetoothState");
            JSONObject obj = new JSONObject();
            if (bluetoothAdapter.isEnabled()) {
                Tools.addProperty(obj, Tools.BLUETOOTH_STATE, Tools.IS_TRUE);
                callbackContext.success(obj);
            } else {
                Tools.addProperty(obj, Tools.BLUETOOTH_STATE, Tools.IS_FALSE);
                callbackContext.success(obj);
            }
            return true;
        }
        if (action.equals("startClassicalScan")) {
            Log.i(TAG, "startClassicalScan");
            if (bluetoothAdapter.isEnabled()) {
                if (bluetoothAdapter.startDiscovery()) {
                    callbackContext.success();
                } else {
                    callbackContext.error("start classical scan error!");
                }
            } else {
                callbackContext.error("your bluetooth is not open!");
            }
        }
        if (action.equals("stopClassicalScan")) {
            Log.i(TAG, "stopClassicalScan");
            if (bluetoothAdapter.isEnabled()) {
                if (bluetoothAdapter.cancelDiscovery()) {
                    callbackContext.success();
                } else {
                    callbackContext.error("stop classical scan error!");
                }
            } else {
                callbackContext.error("your bluetooth is not open!");
            }
        }
        if (action.equals("rfcommConnect")) {
            String deviceAddress = Tools.getData(json, Tools.DEVICE_ADDRESS);
            String securestr = Tools.getData(json, Tools.SECURE);
            String uuidstr = Tools.getData(json, Tools.UUID);
            boolean secure = false;
            if (securestr != null && securestr.equals("true")) {
                secure = true;
            }
            Log.i(TAG, "connect to " + deviceAddress);
            BluetoothDevice device = bluetoothAdapter.getRemoteDevice(deviceAddress);
            BluetoothSerialService classicalService = classicalServices.get(deviceAddress);

            if (device != null && classicalService == null) {
                classicalService = new BluetoothSerialService();
                classicalService.disconnectCallback = disconnectContext;
                classicalServices.put(deviceAddress, classicalService);
            }

            if (device != null) {
                classicalService.connectCallback = callbackContext;
                classicalService.connect(device, uuidstr, secure);
            } else {
                callbackContext.error("Could not connect to " + deviceAddress);
            }
        }
        if (action.equals("rfcommDisconnect")) {
            String deviceAddress = Tools.getData(json, Tools.DEVICE_ADDRESS);
            BluetoothSerialService service = classicalServices.get(deviceAddress);
            if (service != null) {
                service.connectCallback = null;
                service.stop();
                classicalServices.remove(deviceAddress);
                callbackContext.success();
            } else {
                callbackContext.error("Could not disconnect to " + deviceAddress);
            }
        }
        if (action.equals("rfcommListen")) {
            String name = Tools.getData(json, Tools.NAME);
            String uuidstr = Tools.getData(json, Tools.UUID);
            String securestr = Tools.getData(json, Tools.SECURE);
            boolean secure = false;
            if (securestr.equals("true")) {
                secure = true;
            }
            BluetoothSerialService service = new BluetoothSerialService();
            service.listen(name, uuidstr, secure, this);
            acceptServices.put(name + uuidstr, service);
        }
        if (action.equals("rfcommUnListen")) {
            String name = Tools.getData(json, Tools.NAME);
            String uuidstr = Tools.getData(json, Tools.UUID);
            BluetoothSerialService service = acceptServices.get(name + uuidstr);
            if (service != null) {
                service.stop();
            }
        }
        if (action.equals("rfcommWrite")) {
            String deviceAddress = Tools.getData(json, Tools.DEVICE_ADDRESS);
            BluetoothSerialService service = classicalServices.get(deviceAddress);
            if (service != null) {
                String data = Tools.getData(json, Tools.WRITE_VALUE);
                service.write(Tools.decodeBase64(data));
                callbackContext.success();
            } else {
                callbackContext.error("there is no connection on device:" + deviceAddress);
            }
        }
        if (action.equals("rfcommRead")) {
            String deviceAddress = Tools.getData(json, Tools.DEVICE_ADDRESS);
            BluetoothSerialService service = classicalServices.get(deviceAddress);
            if (service != null) {
                byte[] data = new byte[2048];
                byte[] predata = service.buffer.array();
                for (int i = 0; i < service.bufferSize; i++) {
                    data[i] = predata[i];
                }

                JSONObject obj = new JSONObject();
                //Tools.addProperty(obj, Tools.DEVICE_ADDRESS, deviceAddress);
                Tools.addProperty(obj, Tools.VALUE, Tools.encodeBase64(data));
                Tools.addProperty(obj, Tools.DATE, Tools.getDateString());
                callbackContext.success(obj);
                service.bufferSize = 0;
                service.buffer.clear();
            } else {
                callbackContext.error("there is no connection on device:" + deviceAddress);
            }
        }
        if (action.equals("rfcommSubscribe")) {
            String deviceAddress = Tools.getData(json, Tools.DEVICE_ADDRESS);
            BluetoothSerialService service = classicalServices.get(deviceAddress);
            if (service != null) {
                service.dataAvailableCallback = callbackContext;
            } else {
                callbackContext.error("there is no connection on device:" + deviceAddress);
            }
        }
        if (action.equals("rfcommUnsubscribe")) {
            String deviceAddress = Tools.getData(json, Tools.DEVICE_ADDRESS);
            BluetoothSerialService service = classicalServices.get(deviceAddress);
            if (service != null) {
                service.dataAvailableCallback = null;
            } else {
                callbackContext.error("there is no connection on device:" + deviceAddress);
            }
        }
        if (action.equals("getPairedDevices")) {
            try {
                Log.i(TAG, "getPairedDevices");
                JSONArray ary = new JSONArray();
                Set<BluetoothDevice> devices = bluetoothAdapter.getBondedDevices();
                Iterator<BluetoothDevice> it = devices.iterator();
                while (it.hasNext()) {
                    BluetoothDevice device = (BluetoothDevice) it.next();
                    JSONObject obj = new JSONObject();
                    Tools.addProperty(obj, Tools.DEVICE_ADDRESS, device.getAddress());
                    Tools.addProperty(obj, Tools.DEVICE_NAME, device.getName());
                    ary.put(obj);
                }
                callbackContext.success(ary);
            } catch (Exception e) {
                Tools.sendErrorMsg(callbackContext);
            } catch (java.lang.Error e) {
                Tools.sendErrorMsg(callbackContext);
            }
        } else if (action.equals("createPair")) {
            Log.i(TAG, "createPair");
            String deviceAddress = Tools.getData(json, Tools.DEVICE_ADDRESS);
            JSONObject obj = new JSONObject();
            BluetoothDevice device = bluetoothAdapter.getRemoteDevice(deviceAddress);
            if (Tools.creatBond(device.getClass(), device)) {
                Tools.addProperty(obj, Tools.DEVICE_ADDRESS, device.getAddress());
                callbackContext.success(obj);
            } else {
                Tools.addProperty(obj, Tools.DEVICE_ADDRESS, device.getAddress());
                callbackContext.error(obj);
            }
        } else if (action.equals("removePair")) {
            Log.i(TAG, "removePair");
            String deviceAddress = Tools.getData(json, Tools.DEVICE_ADDRESS);
            JSONObject obj = new JSONObject();
            BluetoothDevice device = bluetoothAdapter.getRemoteDevice(deviceAddress);
            if (Tools.removeBond(device.getClass(), device)) {
                Tools.addProperty(obj, Tools.DEVICE_ADDRESS, device.getAddress());
                callbackContext.success(obj);
            } else {
                Tools.addProperty(obj, Tools.DEVICE_ADDRESS, device.getAddress());
                callbackContext.error(obj);
            }
        }
    } catch (Exception e) {
        Tools.sendErrorMsg(callbackContext);
    } catch (Error e) {
        Tools.sendErrorMsg(callbackContext);
    }
    return true;
}