BluetoothServerSocket不接受来自()返回 [英] BluetoothServerSocket doesn't return from accept()

查看:148
本文介绍了BluetoothServerSocket不接受来自()返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个小的蓝牙Android的应用程序在学校的一个项目。

I'm trying to build a little Bluetooth-Android-App for a project in school.

我很新的到Android(有我的电话,因为2天)。自2周我的笔记本电脑的Andr​​oid编程,我尝试。安装了Android的x86(EeePC的)一个VirtualBox的,所以我可以使用 BluetoothAdapter 的笔记本电脑。仿真器不支持蓝牙和速度很慢。这是关于该项目...

I'm quite new to Android (got my phone since 2 days). I'm experimenting since 2 weeks with android programming on my laptop. Installed a VirtualBox with Android x86 (eeepc) so I can use the BluetoothAdapter of the laptop. Emulator doesn't support Bluetooth and is quite slow. That's about the project...

问题/问题:
一个Bluetoothconnection有2个装置 - 连接和监听之一。听音设备具有 BluetoothServerSocket ,即循环接受()方法,直到接受()返回的BluetoothSocket
在我的情况下接受()方法不返回,所以我被卡住和应用程序与blackscreen要求MIT停止应用程序,或只是等待冻结。当我通过超时接受() - > 接受(10000)我得到一个 IOException异常后超时。

The problem/question: A Bluetoothconnection has 2 devices - a connecting and a listening one. The listening device has a BluetoothServerSocket, that loops accept() method until accept() returns a BluetoothSocket. In my case the accept() method doesn't return so I get stuck and the app freezes with blackscreen asking mit to stop the app or just to wait. When I pass a timeout to accept() --> accept(10000) I get an IOException after the timeout.

监听设备:

private class AcceptThread extends Thread {

    private BluetoothSocket tSocket;
    private BluetoothServerSocket bss = null;

    public void run() {
        try {
            Log.d(TAG, "erzeuge ServerSocket");
            bss = BluetoothAdapter.getDefaultAdapter().listenUsingInsecureRfcommWithServiceRecord("BluetoothChatInsecure", MainActivity.BT_UUID);
            Log.d(TAG, "ServerSocket OK");
        } catch (IOException e) {
            e.printStackTrace();
            Log.e(TAG, "Fehler Serversocket");
        }

        while (true)  {

            Log.d(TAG, "Versuche zu akzeptieren");
            try {
                Log.d(TAG, "Akzeptieren Anfang");
                tSocket = bss.accept(10000);
                //this line is never reached
                Log.d(TAG, "Akzeptieren Ende");

                if (tSocket != null){
                    //Hier wollen wir hin!
                    Log.d(TAG, "Verbindung akzeptiert");
                    ConnectedThread conThread = new ConnectedThread(tSocket);
                    conThread.run();
                    bss.close();
                    break;
                } else {
                    Log.e(TAG, "Fehler, keine Verbindung");
                }
            } catch (IOException e) {
                Log.e(TAG, "IOException währent accept-loop");
                //this exception is triggered every 10 sec, when the accept(10000) times out
                e.printStackTrace();
            }   
        }
        Log.i(TAG, "Acceptthread hat fertig");          
    }

}

连接设备:

try {
    socket = device.createInsecureRfcommSocketToServiceRecord(MainActivity.BT_UUID);

    outstr = socket.getOutputStream();
    instr = socket.getInputStream();

    ois = new ObjectInputStream(instr);
    oos = new ObjectOutputStream(outstr);

} catch (IOException e) {
    e.printStackTrace();
}

我已经读了很多关于计算器线程和有关此主题的其他一些论坛,但我没有得到该问题的解决方案。
对不起我的英语,但我不是一个母语。

I've read a lot of threads on stackoverflow and some other forums about this topic, but I didn't got a solution for the problem. Sorry about my English, but I am not a native speaker.

感谢您的帮助!

编辑:

我忘了写,我有2个设备上测试应用程序。我的笔记本电脑不接受环,而我用我的手机,并尝试连接。

I forgot to write, that I test the app with 2 devices. My laptop does accept-loop, while I use my phone and try to connect.

推荐答案

我已经面临这个问题了几天。结果最后,我明白了为什么:
我创建接受服务器的传入连接两次线程。因此,将ServerSocket正在创建的时间,虽然只是第二次accept()方法被调用。结果
这导致服务器不接受任何连接!

I've been facing this problem for a couple of days.
Finally, I realized why: I was creating the Thread that accepts incoming connections in the server twice. Thus, the ServerSocket was being created to times, although only the second time the accept() method was called.
This leads to server not accepting any connection!!

这篇关于BluetoothServerSocket不接受来自()返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆