【问题标题】:How can I send ELM327 commands to my OBDII adapter through Bluetooth?如何通过蓝牙向我的 OBDII 适配器发送 ELM327 命令?
【发布时间】:2016-03-23 08:33:30
【问题描述】:

所以我正在制作一个 OBDII 蓝牙应用程序,用户可以发送诸如电压之类的命令(在这种情况下)并从 OBDII 获取数据。到目前为止,我已经设法通过我的移动设备与我的 OBDII 适配器建立蓝牙连接,现在我需要发送一些基本命令并在 Logcat 中显示它们(暂时)。

我遇到的问题是它无法识别我的onClick 方法?

在父上下文或祖先上下文中找不到方法 onClick(View) 对于视图类上定义的 android:onClick 属性 id 为“getValue”的 android.support.v7.widget.AppCompatButton

我确定我的代码中遗漏了一些东西,但我不知道是什么。

MainActivity.java

public class MainActivity extends AppCompatActivity {

    Button b1;
    BluetoothAdapter mAdapter;
    FragmentHostCallback mHost;
    BTHandler btHandler;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case Constants.MESSAGE_STATE_CHANGE:
                    switch (msg.arg1) {
                        case BTHandler.STATE_CONNECTED:
                            //setContentView(R.layout.activity_connected);
                            Intent intent = new Intent(MainActivity.this, Connected.class);
                            startActivity(intent);
                            Toast.makeText(getApplicationContext(), R.string.title_connected_to, Toast.LENGTH_SHORT).show();
                            Log.v("Log", "Connected");
                            break;
                        case BTHandler.STATE_NONE:
                            Toast.makeText(getApplicationContext(), R.string.title_not_connected, Toast.LENGTH_SHORT).show();
                            break;
                    }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btHandler = new BTHandler(MainActivity.this, mHandler);
        b1 = (Button) findViewById(R.id.connect);
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        //init();

        if (mAdapter == null) {
            Toast.makeText(getApplicationContext(), R.string.device_not_supported, Toast.LENGTH_LONG).show();
            finish();
        } else {
            if (!mAdapter.isEnabled()) {
                Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(intent, 1);
            }
        }
        //BTHandler btHandler = new BTHandler(MainActivity.this, mHandler);
        //btHandler.connect("");
    }

    public void onClick(View v) {
        int id = v.getId();
        String sendMessage = ("AT RV");

        switch (id) {
            case R.id.connect:
                onConnect(); //Operation
                Log.v("Log", "Pressed onClick");
                break;
            case R.id.getValue:
                btHandler.write(sendMessage);
                Log.v("Log", "getValue" + sendMessage);
                break;
        }
    }

BTHandler.java

public class BTHandler {

    public static final int STATE_NONE = 0;       // we're doing nothing
    public static final int STATE_CONNECTING = 2; // now initiating an outgoing connection
    public static final int STATE_CONNECTED = 3;  // now connected to a remote device

    final ArrayList<String> devices = new ArrayList();
    private final Handler mHandler;
    private BluetoothAdapter mAdapter;
    private BluetoothDevice device;
    private ConnectThread mConnectThread;
    private ConnectedThread mConnectedThread;
    private BluetoothSocket socket;
    private String status;
    private int mState;
    private boolean connectionStatus = false;

    public BTHandler(Context context, Handler handler) { // Konstruktor
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        mHandler = handler;
    }

    public void write(String s) {
        mConnectedThread.sendRawCommand(s);
    }
    /*
    public void write(byte[] bytes) {
        try {
            mmOutStream.write(bytes);
        } catch (IOException e) {
        }
    }
    */
    public void connect(String deviceAddress) {
        mConnectThread = new ConnectThread(deviceAddress);
        mConnectThread.start();
    }

    private void guiHandler(int what, int arg1, String obj) {
        Message msg = mHandler.obtainMessage();
        msg.what = what;
        msg.obj = obj;
        msg.arg1 = arg1;
        msg.sendToTarget();
    }

    private class ConnectThread extends Thread {
        BluetoothSocket tmp = null;
        private BluetoothSocket mmSocket;

        public ConnectThread(String deviceAddress) {
            mAdapter = BluetoothAdapter.getDefaultAdapter();
            device = mAdapter.getRemoteDevice(deviceAddress);

            BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter();
            BluetoothDevice device = mAdapter.getRemoteDevice(deviceAddress);
            UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
            try {
                tmp = device.createRfcommSocketToServiceRecord(uuid);
                //socket.connect();
                //Log.v("connect", "connect");
            } catch (IOException e) {
                //e.printStackTrace();
                //Log.v("exception", "e");
            }
            mmSocket = tmp;
        }

        public void run() {
            // Cancel discovery because it will slow down the connection
            mAdapter.cancelDiscovery();
            byte[] buffer = new byte[1024]; // buffer store for the stream
            int bytes;

            try {
                // Connect the device through the socket. This will block
                // until it succeeds or throws an exception
                mmSocket.connect();
                Log.v("connect", "connect");
            } catch (IOException connectException) {
                // Unable to connect; close the socket and get out
                try {
                    mmSocket.close();
                    Log.v("close", "close");
                } catch (IOException closeException) {
                }
                guiHandler(Constants.TOAST, Constants.SHORT, "Connection Failed");
                return;
            }
            guiHandler(Constants.CONNECTION_STATUS, Constants.STATE_CONNECTED, "");
        }
    }

    private class ConnectedThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;
        private ObdMultiCommand multiCommand;

        public ConnectedThread(BluetoothSocket socket) {
            connectionStatus = true;
            mmSocket = socket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;

            try {
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();
            } catch (IOException e) {
            }

            mmInStream = tmpIn;
            mmOutStream = tmpOut;

            try {
                RPMCommand engineRpmCommand = new RPMCommand();
                SpeedCommand speedCommand = new SpeedCommand();
                ModuleVoltageCommand voltageCommand = new ModuleVoltageCommand();

                while (!Thread.currentThread().isInterrupted()) {
                    engineRpmCommand.run(mmInStream, mmOutStream); //(socket.getInputStream(), socket.getOutputStream());
                    speedCommand.run(mmInStream, mmOutStream); //(socket.getInputStream(), socket.getOutputStream());
                    voltageCommand.run(mmInStream, mmOutStream); //(socket.getInputStream(), socket.getOutputStream());
                    // TODO handle commands result
                    Log.d("Log", "RPM: " + engineRpmCommand.getFormattedResult());
                    Log.d("Log", "Speed: " + speedCommand.getFormattedResult());
                    Log.v("Log", "Voltage: " + speedCommand.getFormattedResult());
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

        public void run() {
            byte[] buffer = new byte[1024];  // buffer store for the stream
            int bytes; // bytes returned from read()

            OBDcmds();
            // Keep listening to the InputStream until an exception occurs
            while (connectionStatus) {
                sendMultiCommand();
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        // CALL this to MainActivity
        public void sendRawCommand(String s) {
            try {
                //new ObdRawCommand();

            } catch (Exception e) {
            }
        }

        private void OBDcmds() { // execute commands

            try {
                new EchoOffCommand().run(socket.getInputStream(), socket.getOutputStream());
                new LineFeedOffCommand().run(socket.getInputStream(), socket.getOutputStream());
                new TimeoutCommand(125).run(socket.getInputStream(), socket.getOutputStream());
                new SelectProtocolCommand(ObdProtocols.AUTO).run(socket.getInputStream(), socket.getOutputStream()); //ISO_15765_4_CAN

            } catch (Exception e) {
                Log.v("Log", "e");
                // handle errors
            }
        }
        /*
        // Call this from the main activity to send data to the remote device
        public void write(byte[] bytes) {
            try {
                mmOutStream.write(bytes);
            } catch (IOException e) {
            }
        }
        */

        /* Call this from the main activity to shutdown the connection */
        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) {
            }
        }

        public void sendMultiCommand() {
            try {
                // RUN some code here
            } catch (Exception e) {
            }
        }
    }
}

编辑:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {...}

@Override
    protected void onCreate(Bundle savedInstanceState) {
        b1 = (Button) findViewById(R.id.connect);
        b1.setOnClickListener(this);
    }

Logcat:

E/AndroidRuntime: 致命异常: main 进程:com.example.asabanov.powersupplytool,PID:26570 java.lang.IllegalStateException:在 android:onClick 的父上下文或祖先上下文中找不到方法 onClick(View) 在视图类上定义的属性 id 为“getValue”的 android.support.v7.widget.AppCompatButton 在 android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:307) 在 android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:266) 在 android.view.View.performClick(View.java:5226) 在 android.view.View$PerformClick.run(View.java:21266) 在 android.os.Handler.handleCallback(Handler.java:739) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os.Looper.loop(Looper.java:168) 在 android.app.ActivityThread.main(ActivityThread.java:5845) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)

W/System.err:com.github.pires.obd.exceptions.UnableToConnectException:错误 运行 01 42,响应:0142...UNABLETOCONNECT W/System.err:在 java.lang.Class.newInstance(Native Method) W/System.err:在 com.github.pires.obd.commands.ObdCommand.checkForErrors(ObdCommand.java:203) W/System.err:在 com.github.pires.obd.commands.ObdCommand.readResult(ObdCommand.java:123) W/System.err:在 com.github.pires.obd.commands.ObdCommand.run(ObdCommand.java:77) W/System.err:在 com.example.asabanov.powersupplytool.BTHandler$ConnectedThread.(BTHandler.java:151) W/System.err:在 com.example.asabanov.powersupplytool.BTHandler$ConnectThread.run(BTHandler.java:117) V/Log: e

编辑:

private void onConnect() {
        ArrayList deviceStrs = new ArrayList();
        final ArrayList<String> devices = new ArrayList();

        BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter();
        Set pairedDevices = mAdapter.getBondedDevices();
        if (pairedDevices.size() > 0) {
            for (Object device : pairedDevices) {
                BluetoothDevice bdevice = (BluetoothDevice) device;
                deviceStrs.add(bdevice.getName() + "\n" + bdevice.getAddress());
                devices.add(bdevice.getAddress());
            }
        }

        // show list
        final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.select_dialog_singlechoice,
                deviceStrs.toArray(new String[deviceStrs.size()]));

        alertDialog.setSingleChoiceItems(adapter, -1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                int position = ((AlertDialog) dialog).getListView().getCheckedItemPosition();
                String deviceAddress = devices.get(position);

                btHandler.connect(deviceAddress);
                //btHandler.write();

            }
        });
        alertDialog.setTitle("Paired devices");
        alertDialog.show();
    }

【问题讨论】:

  • 你在哪里onConnect();方法?

标签: java android obd-ii


【解决方案1】:

你应该为你的按钮设置一个 onClickListener。

为此,您需要将 OnClickListener 接口实现为:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

创建按钮时,将其 OnClickListener 设置如下:

b1 = (Button) findViewById(R.id.connect);
b1.setOnClickListener(this);

当按钮被点击时,这应该会让你进入你的 onClick 方法。

编辑:我看到你有两个按钮。只需对另一个按钮执行相同操作即可。不要忘记为 b2 声明一个成员,就像 b1 一样。

Button b2;



b2 = (Button)findViewById(R.id.getValue);

b2.setOnClickListener(this);

【讨论】:

  • @swess,抱歉我无法查看链接。只需通过添加implements View.OnClickListener 来更改MainActivity 的类声明,并通过调用b1.setOnClickListener(this);b1 的点击侦听器设置为MainActivity 的实例。
  • 但我应该在我的 onClick 方法中添加b1 = (Button) findViewById(R.id.connect); b1.setOnClickListener(this); 吗?
  • 不,只需在创建按钮后立即添加b1.setOnClickListener(this);。基本上,您将在您设置b1MainActivityonCreate 中的代码中添加一行。
  • 我编辑了这篇文章。看看 Logcat,看看你能不能用它做点什么。它显示相同的错误消息。
猜你喜欢
  • 2016-12-04
  • 2016-05-08
  • 2016-05-07
  • 1970-01-01
  • 2013-03-31
  • 2014-07-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多