Java(Android)中的IOException:未连接传输端点

时间:2014-08-28 09:08:31

标签: java android bluetooth ioexception obd-ii

在我的Android应用程序中,该应用程序已从答案复制到this question(然后由于与Samsung Galaxy选项卡的UUID相关问题而略有改变)我通过蓝牙成功连接到OBD设备。

然后,当我尝试发送任何命令(由sendData()完成)时,抛出标题中的异常(传输端点未连接),并且不发送任何内容。

当我连接到我的电脑(代码的唯一区别是硬件地址)时,我可以毫无问题地发送命令(当然,我没有得到任何响应,因为计算机不是OBD设备)。因此,我相信我获得了所需的所有权限,UUID地址也很好。

EDIT1 : 我再次在平板电脑上安装了Komunikacija.apk。我只添加了一些评论,遇到了两个新问题:

    openLT()中的
  • mmSocket.connect()失败(在最后一次祝酒之后很长一段时间没有发生任何事情" 5")
  • 如果关闭蓝牙,应用程序会显示开启蓝牙的请求,但不久就会停止"。

EDIT2

我再次上车,在三星手机上测试应用程序,并再次在平板电脑上测试。结果:

  • 片剂
    • 首次运行应用程序时,EDIT1中提到的连接未成功完成。
    • 我在接下来的所有尝试中都取得了成功,但抛出了IOException:
      • 当我第一次按下按钮SEND时,exception.getMessage()返回由同行重置连接
      • 每次下载时,我收到消息传输端点未连接
  • 手机:我在第一次尝试时已成功建立连接,其他一切都是相同的

EDIT3 : 我发现OBD设备EML327至少是问题的一部分,因为今天,我测试了另一个OBD设备(OBDLink LX),一切正常,如果我使用它。现在,问题是

  

如果我使用OBD327,为什么这两个OBD设备的行为完全不同以及如何修复发生的错误?

EDIT4 :我之前没有找到这个重要的,但我的EML327的唯一回应是AT + BRSF = 24。谷歌搜索后,我找到了答案。

我的MainActivity.java:

package com.example.komunikacija;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Set;
import java.util.UUID;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    TextView myLabel;
    EditText myTextbox;
    BluetoothAdapter mBluetoothAdapter;
    BluetoothSocket mmSocket;
    BluetoothDevice mmDevice;
    OutputStream mmOutputStream;
    InputStream mmInputStream;
    Thread workerThread;
    byte[] readBuffer;
    int readBufferPosition;
    int counter, stevec;
    volatile boolean stopWorker;

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

    Button openButton = (Button)findViewById(R.id.open);
    Button sendButton = (Button)findViewById(R.id.send);
    Button closeButton = (Button)findViewById(R.id.close);
    myLabel = (TextView)findViewById(R.id.label);
    myTextbox = (EditText)findViewById(R.id.entry);

    //Open Button
    openButton.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            try {
                findBT();
                openBT();
            }
            catch (IOException ex) { }
        }
    });

    //Send Button
    sendButton.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            try{
                sendData();
            }
            catch (IOException ex) { 
                Toast.makeText(getApplicationContext(), "error when sending:"+ ex.getMessage(), Toast.LENGTH_SHORT).show();;

            }
        }
    });

    //Close button
    closeButton.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            try 
            {
                closeBT();
            }
            catch (IOException ex) { }
        }
    });
}

void findBT() {
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if(mBluetoothAdapter == null){
        myLabel.setText("No bluetooth adapter available");
    }

    else{
        if (!mBluetoothAdapter.isEnabled()){
            Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBluetooth, 0);
        }

        Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
        Toast.makeText(this, "we found "+Integer.toString(pairedDevices.size()), Toast.LENGTH_SHORT).show();//number of devices found
        if(pairedDevices.size() > 0){
            for(BluetoothDevice device : pairedDevices){
                //computer's addres: "00:22:68:E6:7D:D7"
                //obd device's("00:0D:18:00:00:01")
                //device.getName().equals("MattsBlueTooth")
                if(device.getAddress().equals("00:22:68:E6:7D:D7")) {
                    Toast.makeText(this, "Found.", Toast.LENGTH_SHORT).show();
                    mmDevice = device;
                    break;
                }
            }
        }
        myLabel.setText("Bluetooth Device Found");

    }
}

void openBT() throws IOException{
    Toast.makeText(this, "openBT.", Toast.LENGTH_SHORT).show();
//      UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");   //Standard SerialPortService ID
    UUID uuid = mmDevice.getUuids()[0].getUuid();
    BluetoothSocket tmp = null;
    try {
        tmp = mmDevice.createRfcommSocketToServiceRecord(uuid);

        // for others devices its works with:
        // Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});

        // for galaxy tab 2 with:
        Method m = mmDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class});

        tmp = (BluetoothSocket) m.invoke(mmDevice, 1);
    } catch (IOException e) {

    } catch (NoSuchMethodException e) {
        // TODO Auto-generated catch block
        Toast.makeText(this, "1", Toast.LENGTH_SHORT).show();;
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        Toast.makeText(this, "2", Toast.LENGTH_SHORT).show();;
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        Toast.makeText(this, "3", Toast.LENGTH_SHORT).show();;
    } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        Toast.makeText(this, "4", Toast.LENGTH_SHORT).show();;
    }
    mmSocket = tmp;
    Toast.makeText(this, "5", Toast.LENGTH_SHORT).show();;

    mmSocket.connect();
//        Log.i(TAG, "Client Connected!");


//      mmSocket = mmDevice.createInsecureRfcommSocketToServiceRecord(uuid);        
    Toast.makeText(this, "before connect", Toast.LENGTH_SHORT).show();
//      mmSocket.connect();
    Toast.makeText(this, "before stream", Toast.LENGTH_SHORT).show();
    mmOutputStream = mmSocket.getOutputStream();
    mmInputStream = mmSocket.getInputStream();
    Toast.makeText(this, "before listening", Toast.LENGTH_SHORT).show();

    beginListenForData();

    myLabel.setText("Bluetooth Opened");
}

void beginListenForData(){
    final Handler handler = new Handler(); 
    final byte delimiter = 10; //This is the ASCII code for a newline character
    final byte konec = 90; //ASCII for char Z

    stopWorker = false;
    readBufferPosition = 0;
    readBuffer = new byte[1024];
    workerThread = new Thread(new Runnable() {
        public void run(){                
           while(!Thread.currentThread().isInterrupted() && !stopWorker){
             try
                {
                    int bytesAvailable = mmInputStream.available();                        
                    if(bytesAvailable > 0)
                    {
                        byte[] packetBytes = new byte[bytesAvailable];
                        mmInputStream.read(packetBytes);
                        packetBytes[bytesAvailable - 1] = konec;

                        for(int i=0;i<bytesAvailable;i++)
                        {
                            byte b = packetBytes[i];

                            if(b == konec)//originally if(b == delimiter)
                            {
                                 byte[] encodedBytes = new byte[readBufferPosition];
                                 System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
                                 final String data = new String(encodedBytes, "US-ASCII");
                                 readBufferPosition = 0; 

                                handler.post(new Runnable()
                                {
                                    public void run()
                                    {
                                        Toast.makeText(getApplicationContext(), "setting " + Integer.toString(stevec++)+data, Toast.LENGTH_SHORT).show();

                                        myLabel.setText(data);
                                    }
                                });
                            }
                            else
                            {
                                readBuffer[readBufferPosition++] = b;
                            }
                        }
                    }
                } 
                catch (IOException ex) 
                {
                    stopWorker = true;
                }
           }
        }
    });

    workerThread.start();
}

void sendData() throws IOException {
    String msg = myTextbox.getText().toString();
    Toast.makeText(this, "sending:"+msg, Toast.LENGTH_SHORT).show();;

    //msg += "\n";    <-- dont want to have that.
    mmOutputStream.write(msg.getBytes());
    myLabel.setText("Data Sent");
}

void closeBT() throws IOException {
    stopWorker = true;
    mmOutputStream.close();
    mmInputStream.close();
    mmSocket.close();
    myLabel.setText("Bluetooth Closed");
}
}

1 个答案:

答案 0 :(得分:1)

我不只是等待,问题herehere的问题让我摆脱了麻烦。问题是EML327需要通道16,所以方法调用的第二个参数应为16。