如何通过蓝牙将ELM327命令发送到OBDII适配器?

时间:2016-03-23 08:33:30

标签: java android obd-ii

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

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

  

无法在父级或祖先上下文中找到onClick(View)方法   for android:在视图类上定义的onClick属性   android.support.v7.widget.AppCompatButton,id为'getValue'

我确信我的代码中遗漏了一些内容,但我不知道是什么。

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:致命异常:主要       过程:com.example.asabanov.powersupplytool,PID:26570       java.lang.IllegalStateException:无法在父级或祖先语句中找到onClick(View)方法for android:onClick   在视图类上定义的属性   android.support.v7.widget.AppCompatButton,id为'getValue'       在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)       at java.lang.reflect.Method.invoke(Native Method)       在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:at java.lang.Class.newInstance(Native Method)       W / System.err:at com.github.pires.obd.commands.ObdCommand.checkForErrors(ObdCommand.java:203)       W / System.err:at com.github.pires.obd.commands.ObdCommand.readResult(ObdCommand.java:123)       W / System.err:at com.github.pires.obd.commands.ObdCommand.run(ObdCommand.java:77)       W / System.err:at com.example.asabanov.powersupplytool.BTHandler $ ConnectedThread。(BTHandler.java:151)       W / System.err:at 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();
    }

1 个答案:

答案 0 :(得分:0)

您应该为按钮设置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);