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);
}
}
});
}
Aggregations