2014-08-28 82 views
1

在我的Android应用程序中,该应用程序从答复复制到this question(然后由于Samsung Galaxy标签与UUID相关的问题而稍有更改),我成功连接到OBD设备蓝牙。Java中的IOException异常(Android):传输端点未连接

然后,当我尝试发送任何命令(由sendData()完成)时,标题(传输端点未连接)的异常被抛出,并且不发送任何内容。当我连接到我的电脑(代码中的唯一区别是硬件地址)时,我可以毫无问题地发送命令(当然,由于电脑不是OBD设备,所以我没有收到任何回应)。因此,我相信我获得了我需要的所有权限,并且UUID地址也很好。

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

  • mmSocket.connect()在openBT()失败(没有最后的土司“5”发生后的很长一段时间)
  • 如果蓝牙关闭,应用程序显示请求打开bluetoot,但然后“不幸停止”。

EDIT2

我坐车去了一次测试的三星手机,并再次ONF的平板电脑也应用。结果:

  • 平板: 运行的第一次应用程序时
    • ,连接,EDIT1中提到,并没有取得成功。
    • 我在我所有的未来尝试的成功,但IOException异常被抛出:
      • ,当我按下按钮发送的第一次,exception.getMessage()被对方
      • 每下一返回连接复位一次,我得到消息交通运输端点没有连接
  • 电话:我成功地在第一次尝试已经连接,一切是相同

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

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

EDIT4:我没有找到这个重要的早些时候,但是从我EML327唯一的反应是AT + BRSF = 24。在Google搜索后,我找到了答案。

我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

我不只是等待和aswers对问题herehere帮了我的烦恼。问题是EML327需要通道16,所以方法调用的第二个参数应该是16.