java - 通过蓝牙将数据发送到 OBD-ii 设备时应用程序崩溃 (Android-Studio)

标签 java android-studio bluetooth android-bluetooth obd-ii

我正在开发一个应用程序,通过蓝牙向汽车诊断扫描仪(如 ELM327)请求信息。因此,我使用 obd-java-api ( https://github.com/eltonvs/java-obd-api ) 作为请求命令,但出现问题并且应用程序崩溃。 我对java和蓝牙服务还很陌生,所以我根本不明白问题出在哪里。

一切都很顺利,直到我使用 OBD 命令。我的意思是,当我连接到套接字时它可以工作,但当我使用 de 命令时它不起作用。

有人有想法吗?

我已经验证了我的 OBD 设备是否可以使用蓝牙 AT 命令与 ELM327 终端正常工作。我以为这是 Echo 功能的问题,但是……我错了。

我也尝试了原始的 obd java api( https://github.com/pires/obd-java-api ) 并得到了相同的结果。

package com.example.obd2_bluetooth_communication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.ParcelUuid;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Set;
import java.util.UUID;

import br.ufrn.imd.obd.commands.ObdCommandGroup;
import br.ufrn.imd.obd.commands.engine.RPMCommand;
import br.ufrn.imd.obd.commands.protocol.EchoOffCommand;
import br.ufrn.imd.obd.commands.protocol.LineFeedOffCommand;
import br.ufrn.imd.obd.commands.protocol.SelectProtocolCommand;
import br.ufrn.imd.obd.commands.protocol.TimeoutCommand;
import br.ufrn.imd.obd.enums.ObdProtocols;

import static android.graphics.Color.RED;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{

    TextView tvEstadoBT, tvRPM;
    ImageView ivEstadoBT;
    Button btnOnOff, btnPaired, btnEnviar;

    BluetoothAdapter mBlueAdapter;
    Set<BluetoothDevice> paired_devices;
    String plist[];
    ListView lvPairedDevices;
    ArrayAdapter<String> mAdapter;
    ArrayList<BluetoothDevice> mBTDevices = new ArrayList<>();

    private static UUID myuuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    BluetoothSocket socket;
    //UUID theuuid;


    RPMCommand rpmCommand = new RPMCommand();

    static final int STATE_CONFIG = 1;
    static final int STATE_CONNECTED = 2;
    static final int STATE_CONNECTION_FAILED = 3;
    static final int STATE_ERROR = 4;
    static final int STATE_INTERRUPT = 5;
    static final int STATE_DATO_ENVIADO = 6;

    private final BroadcastReceiver mBroadcastReceiver1 = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            // When discovery finds a device
            if (action.equals(mBlueAdapter.ACTION_STATE_CHANGED)) {
                final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, mBlueAdapter.ERROR);

                switch (state){
                    case BluetoothAdapter.STATE_OFF:
                        showToast("Bluetooth apagado");
                        btnOnOff.setText("Encender Bluetooth");
                        tvEstadoBT.setText("Bluetooth apagado");
                        tvEstadoBT.setTextColor(RED);
                        ivEstadoBT.setImageResource(R.drawable.ic_action_off);
                        btnEnviar.setEnabled(false);

                        break;

                    case BluetoothAdapter.STATE_TURNING_OFF:
                        showToast("Apagando...");
                        break;

                    case BluetoothAdapter.STATE_ON:
                        showToast("Bluetooth encendido");
                        btnOnOff.setText("Apagar Bluetooth");
                        tvEstadoBT.setText("Bluetooth encendido");
                        tvEstadoBT.setTextColor(Color.GREEN);
                        ivEstadoBT.setImageResource(R.drawable.ic_action_on);


                        break;

                    case BluetoothAdapter.STATE_TURNING_ON:
                        showToast("Encendiendo...");
                        break;

                }
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mBroadcastReceiver1);
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvEstadoBT       = findViewById(R.id.tvEstadoBT);
        ivEstadoBT       = findViewById(R.id.ivEstadoBT);
        btnOnOff         = findViewById(R.id.btnOnOff);
        btnPaired        = findViewById(R.id.btnPaired);
        lvPairedDevices  = findViewById(R.id.lvPairedDevices);
        btnEnviar        = findViewById(R.id.btnSend);
        tvRPM            = findViewById(R.id.tvRPM);


        mBlueAdapter = BluetoothAdapter.getDefaultAdapter();

        //Verificar si el dispositivo posee Bluetooth
        if (mBlueAdapter == null) {

            showToast("Tu dispositivo no soporta la opción de Bluetooth");
        }

        //Condiciones iniciales para el botón de encendido/apagado
        if (!mBlueAdapter.isEnabled()) {
            btnOnOff.setText("Encender Bluetooth");
            tvEstadoBT.setText("Bluetooth apagado");
            tvEstadoBT.setTextColor(RED);
            ivEstadoBT.setImageResource(R.drawable.ic_action_off);
        }
        if (mBlueAdapter.isEnabled()) {
            btnOnOff.setText("Apagar Bluetooth");
            tvEstadoBT.setText("Bluetooth encendido");
            tvEstadoBT.setTextColor(Color.GREEN);
            ivEstadoBT.setImageResource(R.drawable.ic_action_on);

            }


        }


    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {

            switch(msg.what)
            {
                case STATE_CONFIG:
                    showToast("Configurado");
                    btnEnviar.setEnabled(true);
                    finish();
                    break;

                case STATE_CONNECTED:
                    showToast("Conectado");
                    btnEnviar.setEnabled(true);
                    finish();
                    break;

                case STATE_CONNECTION_FAILED:
                    showToast("Conexión falló");
                    finish();
                    break;

                case STATE_ERROR:
                    showToast("Ocurrió un error");
                    ;
                    break;

                case STATE_INTERRUPT:
                    showToast("Función interrumpida");

                    break;

                case STATE_DATO_ENVIADO:

                    tvRPM.setText(rpmCommand.getFormattedResult() + " rpm");


                    break;

            }
            return false;
        }
    });

    private void showToast(String msg){

        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }


    public void OnOffCLICK(View view) {

        if(!mBlueAdapter.isEnabled()){
            //showToast("Activando Bluetooth...");
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivity(enableIntent);
            IntentFilter BTIntent = new IntentFilter((BluetoothAdapter.ACTION_STATE_CHANGED));
            registerReceiver(mBroadcastReceiver1, BTIntent);
        }
        if(mBlueAdapter.isEnabled()){
            mBlueAdapter.disable();

            IntentFilter BTIntent = new IntentFilter((BluetoothAdapter.ACTION_STATE_CHANGED));
            registerReceiver(mBroadcastReceiver1, BTIntent);
        }
    }


    public void PairedDevicesCLICK(View view) {

        if(!mBlueAdapter.isEnabled()){

            showToast("La función no está disponible porque el Bluettoth está apagado");
        }
        else{

            paired_devices = mBlueAdapter.getBondedDevices();
            mBTDevices = new ArrayList<>();
            int count = paired_devices.size();
            plist = new String[count];
            int j = 0;
            for(BluetoothDevice device : paired_devices)
            {
                mBTDevices.add(device);
                plist[j] = device.getName();
                j++;
            }

            mAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,plist);
            lvPairedDevices.setAdapter(mAdapter);
            lvPairedDevices.setOnItemClickListener(MainActivity.this);

        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

        /*String deviceName = mBTDevices.get(i).getName();
        String deviceAddress = mBTDevices.get(i).getAddress();

        showToast("Connecting to: " + deviceName);*/
        /*ParcelUuid[] uuids = mBTDevices.get(i).getUuids();
        theuuid = UUID.fromString(uuids[0].toString());
        showToast(theuuid.toString());*/
        ClientClass cliente = new ClientClass(mBTDevices.get(i));
        cliente.start();

    }

    public void EnviarCLICK(View view) {

        ObdCommandGroup obdCommands = new ObdCommandGroup();

        obdCommands.add(new EchoOffCommand());
        obdCommands.add(new LineFeedOffCommand());
        obdCommands.add(new TimeoutCommand(255));
        obdCommands.add(new SelectProtocolCommand(ObdProtocols.AUTO));
        //obdCommands.add(new RPMCommand());
        obdCommands.add(rpmCommand);

        try {
            obdCommands.run(socket.getInputStream(), socket.getOutputStream());
            Message message = Message.obtain();
            message.what = STATE_DATO_ENVIADO;
            handler.sendMessage(message);

        } catch (IOException e) {
            e.printStackTrace();
            Message message = Message.obtain();
            message.what = STATE_ERROR;
            handler.sendMessage(message);
        } catch (InterruptedException e) {
            e.printStackTrace();
            Message message = Message.obtain();
            message.what = STATE_INTERRUPT;
            handler.sendMessage(message);
        }
    }

    private class ClientClass extends Thread{

        private BluetoothDevice OBD2;
        private BluetoothSocket socket;

        public ClientClass (BluetoothDevice device1)
        {
            OBD2 = device1;
            /*ParcelUuid[] uuids = OBD2.getUuids();

            if (uuids != null)
            {
                theuuid = UUID.fromString(uuids[0].toString());
                //showToast(theuuid.toString());
            }*/

            try {
                //socket = OBD2.createInsecureRfcommSocketToServiceRecord(theuuid);
                socket = OBD2.createInsecureRfcommSocketToServiceRecord(myuuid);
            } catch (IOException e) {
                e.printStackTrace();
                Message message = Message.obtain();
                message.what = STATE_ERROR;
                handler.sendMessage(message);
            }
        }

        public void run()
        {
            mBlueAdapter.cancelDiscovery();
            try {
                socket.connect();
                Message message = Message.obtain();
                message.what = STATE_CONNECTED;
                handler.sendMessage(message);

            } catch (IOException e) {
                e.printStackTrace();
                try {
                    socket.close();
                    Message message = Message.obtain();
                    message.what = STATE_CONNECTION_FAILED;
                    handler.sendMessage(message);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                return;


            }
        }
    }
}

我期望 RPM 命令结果,但当我请求该结果时应用程序崩溃。

最佳答案

我是您正在使用的库的创建者。首先,感谢您使用我的项目!

阅读您的错误日志后,我发现您的广播接收器有问题,而不是 OBD api 有问题。我认为您忘记在 onCreate 方法中注册接收器(因为您尝试在 onDestroy 中取消注册)。

关于java - 通过蓝牙将数据发送到 OBD-ii 设备时应用程序崩溃 (Android-Studio),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56877534/

相关文章:

安卓工作室 : How to delete crashlytics plugin

java - 使用 WebDriver 在基于 CSS 的菜单中选择一个选项

java - 将idl转换为java

java - 是否可以使用 try-catch 语句修补内存泄漏?

iphone - 越狱 iPhone 上的蓝牙编程

ios - 重新加载蓝牙信号强度

linux - 根据数据包类型拆分 TTY 设备

java - 使用正则表达式屏蔽字符串的一部分

android - IDE Android Studio错误开始创建新项目时

java - Android Studio - 应用程序不包括模拟器