0

我正在使用 minSDK 版本 7 创建一个 Android 应用程序,针对版本 16 并且正在物理 Galaxy Nexus 设备上进行测试。我正在尝试使用下面“蓝牙设备连接”下显示的代码连接到蓝牙设备,该代码定期在自己的线程上运行,直到与设备成功连接。在一个单独的线程上,我正在接收 UDP 数据包,其代码显示在“UDP 数据包接收”下。

当蓝牙设备尝试连接时,没有收到 UDP 数据包,但是在成功连接到蓝牙设备后,UDP 数据包开始进入。问题是我希望 UDP 数据包在蓝牙设备尝试时继续流式传输连接,但我找不到为什么在设备尝试连接时没有收到数据包。

这些是我设置的权限:

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

蓝牙设备连接:

    public void run() {
        synchronized (BluetoothSocketService.this) {
            if (mState != HeadsetConnectionState.CONNECTED) {
                Log.i(TAG, "BEGIN mConnectThread");

                BluetoothSocket tmp = null;
                try {
                    tmp = mmDevice
                            .createRfcommSocketToServiceRecord(MY_UUID);
                } catch (IOException e) {
                    Log.e(TAG, "create() failed", e);
                }
                mmSocket = tmp;
                mAdapter.cancelDiscovery();

                try {
                    mmSocket.connect();
                } catch (IOException e) {
                    try {
                        mmSocket.close();
                    } catch (IOException e2) {
                        Log.e(TAG,
                                "unable to close() socket during connection failure",
                                e2);
                    }
                    Log.d(TAG, "bluetooth failed to connect");
                    if (connectTimer != null)
                        connectTimer.schedule(
                                new ConnectTimerTask(mmDevice),
                                DELAY_BEFORE_RECONNECT_ATTEMPT_MS,
                                TimeUnit.MILLISECONDS);
                    return;
                }
                connected(mmSocket, mmDevice);
            }
        }
    }

UDP数据包接收:

public void run() {
    if (_myDatagramSocket != null) {
        if (isRunning) {
            Logger.printProcess("UdpServer.run()",
                    "UdpServer is running @ " + _myPort);
        }
    } else {
        Logger.printError("UdpServer.run()",
                "Socket is null. closing UdpServer.");
        return;
    }

    while (isRunning) {
        try {
            byte[] myBuffer = new byte[_myDatagramSize];
            DatagramPacket myPacket = new DatagramPacket(myBuffer,
                    _myDatagramSize);
            System.out.println("Receiving datagram packet!");
            _myDatagramSocket.receive(myPacket);
            _myListener.process(myPacket, _myPort);
        } catch (IOException ioex) {
            Logger.printProcess("UdpServer.run()", " socket closed.");
            break;
        } catch (ArrayIndexOutOfBoundsException ex) {
            Logger.printError("UdpServer.run()",
                    "ArrayIndexOutOfBoundsException:  " + ex);
        }
    }
    dispose();
}
4

0 回答 0