1

所以我试图让这段代码运行

public AcceptThread() {
    // Use a temporary object that is later assigned to mmServerSocket,
    // because mmServerSocket is final
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    BluetoothServerSocket tmp = null;
    try {
        // MY_UUID is the app's UUID string, also used by the client code
        tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord("Server", MY_UUID);
    } catch (IOException e) { }
    mmServerSocket = tmp;
}

但是当我从应该分配 tmp 的行继续时,它仍然为空。蓝牙已激活,一切都已激活,但当我使用它时,它根本不会将 tmp 设置为任何东西。有什么想法为什么不呢?顺便说一句。这种方法在 2.2 的 android 机器上不起作用,因为它在我的 2.2 机器上的那条线上崩溃,但在我的 4.1.2 设备上却没有。

如果需要更多信息才能回答,请提出要求,并尽我所能提供。

4

2 回答 2

1

I am completely new to Android development and ran into the same problem. Here is more help if other people run into this:

Because this is happening on the server side, be sure discoverability is enabled before starting the accept-connection thread. This can by starting the thread in the onActivityResult() method when the proper resultCode is received.

于 2014-01-14T21:46:40.570 回答
1

发现我没有授予应用程序使用蓝牙的权限,所以解决了。

于 2012-12-04T13:12:38.063 回答