在我的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");
}
}