【问题标题】:AcceptThread is not returning to Activity on bluetooth file transfer appAcceptThread 未返回蓝牙文件传输应用程序上的 Activity
【发布时间】:2014-10-02 23:20:18
【问题描述】:

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

当我开始在应用程序的服务器部分侦听传入连接时工作正常,但我用来执行此任务且不阻塞 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 部分并使其工作?似乎没有错误,只是线程退出并没有返回调用者活动。

谢谢

【问题讨论】:

  • acceptThread.start() 在什么函数中被调用?您已将调用置于静态上下文中?
  • @nandeesh,正在调用activity,看最后一段代码sn-p。

标签: java android multithreading bluetooth file-transfer


【解决方案1】:

因为这里没有你的所有代码,我们无法检查它。

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

//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 是对的。

【讨论】:

  • 我忘记添加 OnCreate 部分来工作,已相应更新。我还设法使用 runOnUiThread 方法从 AcceptThread 调用了 connectedThread 部分代码,但我仍然不明白为什么我不能在没有错误的情况下从 AcceptThread 内部调用它。
【解决方案2】:

为了从AcceptThread返回Activity,我在它的run方法中使用了这个

(this.mmActivity).runOnUiThread(new Runnable() {
            public void run() {             
              ((MainActivity)mmActivity).connectedThread();
            }
});

connectedThread 方法已在活动中声明并实现,如更新后的帖子所示。谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-13
    • 2012-04-02
    • 1970-01-01
    • 1970-01-01
    • 2016-09-09
    相关资源
    最近更新 更多