gpt4 book ai didi

java - AcceptThread 未返回蓝牙文件传输应用程序上的 Activity

转载 作者:行者123 更新时间:2023-12-01 12:28:49 25 4
gpt4 key购买 nike

我正在制作一个基于蓝牙的文件传输应用程序,但我不确定如何在此处使用线程。

当我开始监听应用程序服务器部分的传入连接时,它工作正常,但我用来执行此任务且不阻止 UI 的线程在被调用后不会返回到主 Activity 。这是部分代码:

 public class AcceptThread extends Thread {

private BluetoothServerSocket mmServerSocket;
public BluetoothSocket mmBlueToothSocket;
private final BluetoothAdapter mmBluetoothAdapter;

/*Accept incomming connections on the server*/
public AcceptThread(BluetoothAdapter mBluetoothAdapter){

BluetoothServerSocket tmp = null;
this.mmBluetoothAdapter = mBluetoothAdapter;

//works well
try {
this.mmServerSocket = mmBluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord(BluetoothConfig.mServiceName,BluetoothConfig.mUUID);

} catch (IOException e) {
mmServerSocket = tmp;
Log.d("AcceptThreadConstructorErr:", e.getMessage());
}

}

public void run(){

//while (true){
try {
mmBlueToothSocket = this.mmServerSocket.accept();
Log.d("AcceptThreadRun:", "mmServerSocket reached");
} catch (IOException e) {
Log.d("AcceptThreadRunErr:", e.getMessage());
//break;
}
//}

//If I call the ConnectedThread part of the code here it raises an error so I am trying to call it from my Activity but is not returning there after executing the run part of this class

}//run

}//AcceptThread class


public class MainActivity extends Activity {

private BluetoothServerSocket mmServerSocket;
public BluetoothSocket mmBlueToothSocket;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//accept to listen for incomming connections
AcceptThread acceptThread = new AcceptThread(bConfig.getBluetoothAdapter());
acceptThread.start();

//After the start execution this part is never reached!!
connectedThread();

}

//Called from acceptThread once innitiated the connections listening in AcceptThread
public void connectedThread(){

socket = acceptThread.mmBlueToothSocket;

if (socket == null){
Log.d("acceptThread:", "socket Null in acceptThread");
}

if (socket != null){

try {

ConnectedThread connected = new ConnectedThread(socket);
connected.start();
Log.d("serverConnect", "connect.start() ok");

}catch (Exception e) {
Log.d("serverConnect:", e.getMessage());
}

}

}//connectedThread

}//Activity

您能否告诉我,我缺少 Thread 实现的哪个组件(可能是 Handler?),以便能够到达 ConnectedThread 部分并使其成功上类?。似乎没有错误,只是线程退出并且没有返回到调用者 Activity 。

谢谢

最佳答案

e由于此处并非您的所有代码,我们无法检查。

但我看到一些奇怪的东西:

//accept to listen for incoming connections
AcceptThread acceptThread = new AcceptThread(bConfig.getBluetoothAdapter());
acceptThread.start();

//This part is never reached!!!!
Log.d("acceptThread:", "acceptThread.start() is executed");

这在您的 Activity 中被调用?您的 Activity 在哪里?

您通常会将其放在 onCreate() 方法中之类的位置。这不是您显示的内容。否则你的代码甚至不应该编译。

确实,我没有看到任何其他原因,因为线程的主要目的是“并行”运行。因此,紧接在acceptThread.start();之后被调用,Log.d("acceptThread:", "acceptThread.start()被执行");应该被执行。同时你的线程的run方法的内容应该被执行。

此外,您应该能够毫无问题地在 AcceptThread 中启动 ConnectedThread。

所以我认为 Nandeesh 是对的。

关于java - AcceptThread 未返回蓝牙文件传输应用程序上的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26114008/

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