Java AcceptThread未返回蓝牙文件传输应用程序上的活动
我正在制作一个基于蓝牙的文件传输应用程序,我不知道如何在这里使用线程 当我开始在应用程序的服务器部分监听输入连接时,该应用程序工作正常,但我用于执行此任务且不阻止用户界面的线程在被调用后不会返回到主活动。以下是代码的一部分:Java AcceptThread未返回蓝牙文件传输应用程序上的活动,java,android,multithreading,bluetooth,file-transfer,Java,Android,Multithreading,Bluetooth,File Transfer,我正在制作一个基于蓝牙的文件传输应用程序,我不知道如何在这里使用线程 当我开始在应用程序的服务器部分监听输入连接时,该应用程序工作正常,但我用于执行此任务且不阻止用户界面的线程在被调用后不会返回到主活动。以下是代码的一部分: public class AcceptThread extends Thread { private BluetoothServerSocket mmServerSocket; public BluetoothSocket mmBlueT
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
您能否告诉我,为了能够访问ConnectedThread
部分并使其正常工作,我缺少线程实现的哪个组件(可能是处理程序?)?。似乎没有错误,只是线程正在退出,而没有返回到调用方活动
谢谢因为这里没有您所有的代码,我们无法检查
但我看到了一些奇怪的东西:
//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");
这在您的活动中被称为?你的活动在哪里
您通常会将它放在onCreate()方法中的某个位置。这不是您显示的内容。否则,您的代码甚至不应该编译
事实上,我看不到任何其他原因,因为线程的主要目的是“并行”运行。因此,紧接着acceptThread.start()之后;调用,Log.d(“acceptThread:”,“acceptThread.start()被执行”);应该执行。同时,应该执行线程的run方法的内容
此外,您应该能够毫无问题地在AcceptThread中启动ConnectedThread
所以我想Nandeesh是对的。为了从AcceptThread
返回到活动,我在其run方法中使用了它
(this.mmActivity).runOnUiThread(new Runnable() {
public void run() {
((MainActivity)mmActivity).connectedThread();
}
});
其中,connectedThread
方法已在活动中声明并实现,如更新后的帖子所示。谢谢。在哪个函数中调用acceptThread.start()?您已将调用置于静态上下文中?@nandeesh,正在对活动进行调用,请查看代码段的最后一部分。我忘了将OnCreate部分添加到工作中,并相应地进行了更新。我还设法使用runOnUiThread方法从AcceptThread调用了代码的connectedThread部分,但我仍然不明白为什么不能在AcceptThread内部调用它而不出错。