johnnyb
johnnyb

Reputation: 712

Proper Way to Disconnect on Wifi Direct (Android) - removeGroup, channel close, or both?

In API 27, Android added a close() method for WifiP2pManager.Channel. Is this a better alternative to removeGroup? Should I do both? If I call close(), after closing, should I call initialize() to get a new channel, or should I just keep using the existing channel? The documentation is unclear.

There is an existing question, but I believe that was before close() was added.

Upvotes: 0

Views: 243

Answers (1)

HasIEluS
HasIEluS

Reputation: 51

This is what I use and it works just fine.

public void disconnect() {
        if (mManager != null && mChannel != null) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            mManager.requestGroupInfo(mChannel, new WifiP2pManager.GroupInfoListener() {
                @Override
                public void onGroupInfoAvailable(WifiP2pGroup group) {
                    if (group != null && mManager != null && mChannel != null) {
                        mManager.removeGroup(mChannel, new WifiP2pManager.ActionListener() {

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

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

Upvotes: 1

Related Questions