void connect()

in tools/android-app/app/src/main/java/com/aws/iotfleetwise/Elm327.java [33:79]


    void connect(String deviceParam)
    {
        if (!deviceParam.contains("\t")) {
            mStatus = "No device selected";
            return;
        }
        String[] deviceInfo = deviceParam.split("\t");
        String deviceAddress = deviceInfo[1];
        try {
            if (mSocket != null && mSocket.isConnected()) {
                if (mLastAddress.equals(deviceAddress)) {
                    return;
                }
                Log.i("ELM327.connect", "Closing connection to " + mLastAddress);
                mSocket.close();
            }
            Log.i("ELM327.connect", "Connecting to " + deviceAddress);
            BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            BluetoothDevice device = bluetoothAdapter.getRemoteDevice(deviceAddress);
            mSocket = device.createRfcommSocketToServiceRecord(SERIAL_PORT_UUID);
            mSocket.connect();
            mLastAddress = deviceAddress;

            Log.i("ELM327.connect", "Resetting device");
            List<String> res = sendCommand(CMD_RESET, TIMEOUT_RESET_MS);
            if (res == null) {
                throw new RuntimeException("Error resetting ELM327");
            }
            String version = res.get(0);

            Log.i("ELM327.connect", "Set automatic protocol");
            res = sendCommand(CMD_SET_PROTOCOL_AUTO, TIMEOUT_SETUP_MS);
            if (res == null) {
                throw new RuntimeException("Error setting ELM327 protocol");
            }

            mStatus = "Connected to "+version;
        }
        catch (SecurityException e) {
            mSocket = null;
            mStatus = "Access denied";
        }
        catch (Exception e) {
            mSocket = null;
            mStatus = "Connection error: "+e.getMessage();
        }
    }