[wifi-to-direct end connection to Android on peer?

问题描述 投票:6回答:1

是否可以通过Wifi-Direct终止与对等方的连接?我尝试了cancelConnect和removeGroup。他们俩都回到忙碌状态?谢谢。

android wifi-direct service-discovery
1个回答
10
投票

这是我用来与同级断开连接的方法。我从日志中注意到,Android内置应用也使用相同的方法断开对等端。

public static void disconnect() {
    if (mManager != null && mChannel != null) {
        mManager.requestGroupInfo(mChannel, new GroupInfoListener() {
            @Override
            public void onGroupInfoAvailable(WifiP2pGroup group) {
                if (group != null && mManager != null && mChannel != null) {
                    mManager.removeGroup(mChannel, new ActionListener() {

                        @Override
                        public void onSuccess() {
                            Log.d(TAG, "removeGroup onSuccess -");
                        }

                        @Override
                        public void onFailure(int reason) {
                            Log.d(TAG, "removeGroup onFailure -" + reason);
                        }
                    });
                }
            }
        });
    }
}

编辑1/12/2020:从if语句中删除了isGroupOwner()。这允许非所有者设备使用相同的方法断开连接。

当非所有者断开连接时,新的WifiP2pInfo将显示:

groupFormed: false isGroupOwner: false groupOwnerAddress: null

您可以从以下位置找到您的WifiP2pInfo:

((1)注册收听WIFI_P2P_CONNECTION_CHANGED_ACTION并从额外的EXTRA_WIFI_P2P_INFO中获取WifiP2pInfo的广播接收器>

((2)通过调用WifiP2pManager#requestConnectionInfo

© www.soinside.com 2019 - 2024. All rights reserved.