gpt4 book ai didi

java - Java (Android) 中的 IOException : Transport endpoint is not connected

转载 作者:行者123 更新时间:2023-11-30 02:42:42 25 4
gpt4 key购买 nike

在我的 Android 应用程序中,它是从对 this question 的答案复制而来的(然后由于三星 Galaxy 标签的 UUID 相关问题而略有更改)我通过蓝牙成功连接到 OBD 设备。

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

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

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

  • openBT() 中的 mmSocket.connect() 失败(在最后一次 toast “5”之后很长一段时间没有任何反应)
  • 如果蓝牙关闭,应用程序会显示打开蓝牙的请求,但随后“不幸停止”。

EDIT2:

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

  • 平板电脑:
    • 第一次运行应用程序时,EDIT1 中提到的连接未成功建立。
    • 我接下来的所有尝试都成功了,但抛出了 IOException:
      • 当我第一次按下按钮 SEND 时,exception.getMessage() 返回了Connection reset by peer
      • 每次,我都会收到消息Transport endpoint is not connected
  • 电话:我已经在第一次尝试中成功建立连接,其他一切都一样

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

Why are those two OBD-devices behaving completely different and how to fix errors which occur, if I use OBD327?

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

最佳答案

我不只是等待问题的答案herehere帮助我摆脱困境。问题是 EML327 需要 channel 16,所以 invoke 方法的第二个参数应该是 16。

关于java - Java (Android) 中的 IOException : Transport endpoint is not connected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25544946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com