使用DatagramSocket接收数据并同时连接到BluetoothSocket

时间:2013-10-05 21:26:57

标签: java android sockets bluetooth udp

我正在使用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();
}

0 个答案:

没有答案