Search in sources :

Example 1 with WifiP2pInfo

use of android.net.wifi.p2p.WifiP2pInfo in project physical-web by google.

the class WifiDirectConnect method connect.

public void connect(UrlDevice urlDevice, String title) {
    String progressTitle = mContext.getString(R.string.page_loading_title) + " " + title;
    mProgress = new ProgressDialog(mContext);
    mProgress.setCancelable(true);
    mProgress.setOnCancelListener(new OnCancelListener() {

        @Override
        public void onCancel(DialogInterface dialogInterface) {
            Log.i(TAG, "Dialog box canceled");
            mDevice = null;
            mManager.cancelConnect(mChannel, new ActionListener() {

                @Override
                public void onSuccess() {
                    Log.d(TAG, "cancel connect call success");
                }

                @Override
                public void onFailure(int reason) {
                    Log.d(TAG, "cancel connect call fail " + reason);
                }
            });
        }
    });
    mProgress.setTitle(progressTitle);
    mProgress.setMessage(mContext.getString(R.string.page_loading_message));
    mProgress.show();
    mDevice = urlDevice;
    WifiP2pConfig config = new WifiP2pConfig();
    config.deviceAddress = Utils.getWifiAddress(mDevice);
    config.groupOwnerIntent = 0;
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
    mContext.registerReceiver(mReceiver, intentFilter);
    mManager.requestGroupInfo(mChannel, new WifiP2pManager.GroupInfoListener() {

        public void onGroupInfoAvailable(final WifiP2pGroup group) {
            if (group != null) {
                Log.d(TAG, "group not null");
                if (group.getOwner().deviceAddress.equals(Utils.getWifiAddress(mDevice))) {
                    Log.i(TAG, "Already connected");
                    mManager.requestConnectionInfo(mChannel, new WifiP2pManager.ConnectionInfoListener() {

                        public void onConnectionInfoAvailable(final WifiP2pInfo info) {
                            if (mDevice != null && info.groupOwnerAddress != null) {
                                Intent intent = new Intent(Intent.ACTION_VIEW);
                                intent.setData(Uri.parse("http:/" + info.groupOwnerAddress + ":" + Integer.toString(Utils.getWifiPort(mDevice))));
                                mContext.startActivity(intent);
                            }
                        }
                    });
                } else {
                    mManager.removeGroup(mChannel, new ActionListener() {

                        @Override
                        public void onSuccess() {
                            Log.d(TAG, "remove call success");
                            connectHelper(true, config);
                        }

                        @Override
                        public void onFailure(int reason) {
                            Log.d(TAG, "remove call fail " + reason);
                        }
                    });
                }
            } else {
                Log.d(TAG, "group null");
                connectHelper(true, config);
            }
        }
    });
}
Also used : IntentFilter(android.content.IntentFilter) DialogInterface(android.content.DialogInterface) WifiP2pConfig(android.net.wifi.p2p.WifiP2pConfig) Intent(android.content.Intent) ProgressDialog(android.app.ProgressDialog) WifiP2pInfo(android.net.wifi.p2p.WifiP2pInfo) ActionListener(android.net.wifi.p2p.WifiP2pManager.ActionListener) WifiP2pGroup(android.net.wifi.p2p.WifiP2pGroup) WifiP2pManager(android.net.wifi.p2p.WifiP2pManager) OnCancelListener(android.content.DialogInterface.OnCancelListener)

Aggregations

ProgressDialog (android.app.ProgressDialog)1 DialogInterface (android.content.DialogInterface)1 OnCancelListener (android.content.DialogInterface.OnCancelListener)1 Intent (android.content.Intent)1 IntentFilter (android.content.IntentFilter)1 WifiP2pConfig (android.net.wifi.p2p.WifiP2pConfig)1 WifiP2pGroup (android.net.wifi.p2p.WifiP2pGroup)1 WifiP2pInfo (android.net.wifi.p2p.WifiP2pInfo)1 WifiP2pManager (android.net.wifi.p2p.WifiP2pManager)1 ActionListener (android.net.wifi.p2p.WifiP2pManager.ActionListener)1