为什么我会丢失蓝牙客户端/服务器连接?

And*_*fat 2 java sockets android bluetooth bluecove

我试图通过蓝牙从桌面应用程序(用Java编写)连接到Android应用程序.
对于桌面应用程序,我使用的是BlueCove API.
当我启动服务器(桌面应用程序)并启动Android应用程序时,连接正常.(即客户端发送"Hello World",服务器在控制台中打印).但是当我离开应用程序(通过按Back或Home按钮)并返回到它时,套接字连接似乎丢失了.

如何检查蓝牙插座是否已连接?
我想检查插座的连接是否再次连接.

我应该怎么写(如果是这样)的onPause,onResume方法是什么?
我想在onDestroy方法中我应该关闭套接字.

以下是客户端服务器的源代码:
Server
Client

我也试过IntentFilter用来检查连接的状态,但它没有用.

@Override
    public void onCreate(Bundle savedInstanceState) {
             // ..... 

        IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
        IntentFilter filter2 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
        IntentFilter filter3 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        this.registerReceiver(mReceiver, filter1);
        this.registerReceiver(mReceiver, filter2);
        this.registerReceiver(mReceiver, filter3);

}

//The BroadcastReceiver that listens for bluetooth broadcasts
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
           //Device found
            Toast.makeText(BluetoothClient.this, "Device not found", 2).show();
        }
        else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
          //Device is now connected
            Toast.makeText(BluetoothClient.this, "Device connected", 2).show();
        }
        else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
          //Done searching
            Toast.makeText(BluetoothClient.this, "Done searching", 2).show();
        }
        else if (BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
            //Device is about to disconnect
            Toast.makeText(BluetoothClient.this, "Device about to connect", 2).show();
        }
        else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
         //Device has disconnected
            Toast.makeText(BluetoothClient.this, "Device disconnected", 2).show();
        }           
    }
};
Run Code Online (Sandbox Code Playgroud)

And*_*fat 6

Server
Client

我修改了2个源代码文件.
现在它应该工作正常.如果BT在进入移动应用程序之前没有打开有一些小错误(它会在一段时间内被卡住太多),对于那些想要使用这个客户端/服务器的人,你应该看看onPause(), onResume(), onDestroy()功能.

问题是我没有正确使用套接字.

我希望它对那些想要用BT实现这样一个应用程序的人有用.