Wifi Direct:群组问题

1.在 4.2 以上版本,如果直连配对成功过,设置里会显示已保存的群组,以后这两台手机连接就无需确认,可以直接连上,不会弹出对话框让用户选择同意或是拒绝。4.0 里还没有群组这个东西。

2.4.2版本的 WifiP2pSettings 源码中,连接成功后,会默认自动记下这个群组。

在 WifiPpManager 中,当有两设备连成功构成组后:

1
2
3
4
5
6
case WifiP2pManager.RESPONSE_PERSISTENT_GROUP_INFO:
    WifiP2pGroupList groups = (WifiP2pGroupList) message.obj;
    if (listener != null) {
        ((PersistentGroupInfoListener) listener).onPersistentGroupInfoAvailable(groups);
    }
break;

而 onPersistentGroupInfoAvailable(groups) 是这个类中的内部接口中定义的方法,在 WifiP2pSettings 中实现了这个接口,重写了方法

1
2
3
4
5
6
7
public void onPersistentGroupInfoAvailable(WifiP2pGroupList groups) {
    mPersistentGroup.removeAll();
    for (WifiP2pGroup group: groups.getGroupList()) {
        if (DBG)     Log.d(TAG, " group " + group);
        mPersistentGroup.addPreference(new WifiP2pPersistentGroup(getActivity(), group));
    }
}

得到组的列表,然后将组添加到界面上

3.群组封装的对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class WifiP2pPersistentGroup extends Preference {
    public WifiP2pGroup mGroup;
    public WifiP2pPersistentGroup(Context context, WifiP2pGroup group) {
        super(context);
        mGroup = group;
    }
    @Override
    protected void onBindView(View view) {
        setTitle(mGroup.getNetworkName());
        super.onBindView(view);
    }
    int getNetworkId() {
        return mGroup.getNetworkId();
    }
    String getGroupName() {
        return mGroup.getNetworkName();
    }
}
// 如果点击了群组,弹出对话框问是否要删除群组
else if (preference instanceof WifiP2pPersistentGroup) {
    mSelectedGroup = (WifiP2pPersistentGroup) preference;
    showDialog(DIALOG_DELETE_GROUP);
}
else if (id == DIALOG_DELETE_GROUP) {
    int stringId = R.string.wifi_p2p_delete_group_message;
    AlertDialog dialog = new AlertDialog.Builder(getActivity())
        .setMessage(getActivity().getString(stringId))
        .setPositiveButton(getActivity().getString(R.string.dlg_ok), mDeleteGroupListener)
        .setNegativeButton(getActivity().getString(R.string.dlg_cancel), null)
        .create();
    return dialog;
}

4.删除群组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//delete persistent group dialog listener
mDeleteGroupListener = new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
    if (which == DialogInterface.BUTTON_POSITIVE) {
        if (mWifiP2pManager != null) {
            mWifiP2pManager.deletePersistentGroup(mChannel,
                mSelectedGroup.getNetworkId(),
                new WifiP2pManager.ActionListener() {
                    public void onSuccess() {
                        if (DBG) Log.d(TAG, " delete group success");
                    }
                    public void onFailure(int reason) {
                        if (DBG) Log.d(TAG, " delete group fail " + reason);
                    }
                });
            }
        }
    }
};

5.manager.requestGroupInfo(channel, activity) 会调用 GroupInfoListener 接口里的 onGroupInfoAvailable 方法,得到组的信息。GroupInfoListener 这个接口要实现 onGroupInfoAvailable(WifiP2pGroup group) 方法,group 里包含一个 Group Owner 和多个 Group Client 的信息。

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s