public String p2pGroupToString(WifiP2pGroup wifiP2pGroup) { if (wifiP2pGroup != null) { String strWifiP2pGroup = "Network name: " + wifiP2pGroup.getNetworkName(); strWifiP2pGroup += "\nIs group owner: " + wifiP2pGroup.isGroupOwner(); if (wifiP2pGroup.getOwner() != null) { strWifiP2pGroup += "\nGroup owner: "; strWifiP2pGroup += "\n" + p2pDeviceToString(wifiP2pGroup.getOwner()); } if (wifiP2pGroup.getClientList() != null && !wifiP2pGroup.getClientList().isEmpty()) { for (WifiP2pDevice client : wifiP2pGroup.getClientList()) { strWifiP2pGroup += "\nClient: "; strWifiP2pGroup += "\n" + p2pDeviceToString(client); } } return strWifiP2pGroup; } else { Log.e(TAG, "WifiP2pGroup is null"); return ""; } }
/** * This closes the P2P Connection. After some research, this seems to be the proper way to do it. * Before closing a group the group information should be refreshed by calling requestGroupInfo(). * The next step is to call removeGroup(), but only if we are the group owner. This properly * tears down the connection on both sides and will fire onConnectionChanged on both devices. */ private void closeP2PConnection() { Log.d(TAG, "Closing P2P connection..."); if (mManager != null && mChannel != null) { mManager.requestGroupInfo(mChannel, new WifiP2pManager.GroupInfoListener() { @Override public void onGroupInfoAvailable(WifiP2pGroup group) { if (group != null && mManager != null && mChannel != null && group.isGroupOwner()) { mManager.removeGroup(mChannel, new WifiP2pManager.ActionListener() { @Override public void onSuccess() { Log.d(TAG, "P2P Group removed"); } @Override public void onFailure(int i) { Log.d(TAG, "P2P Group NOT removed"); } }); } } }); } }
@Override public void onGroupInfoAvailable(WifiP2pGroup group) { try { callback.GroupInfoAvailable(group); if(mNetworkName.equals(group.getNetworkName()) && mPassphrase.equals(group.getPassphrase())){ debug_print("Already have local service for " + mNetworkName + " ," + mPassphrase); }else { mNetworkName = group.getNetworkName(); mPassphrase = group.getPassphrase(); startLocalService("NI:" + group.getNetworkName() + ":" + group.getPassphrase() + ":" + mInetAddress); } } catch(Exception e) { debug_print("onGroupInfoAvailable, error: " + e.toString()); } }
protected void disconnectFromDevice() { manager.requestGroupInfo(channel, new WifiP2pManager.GroupInfoListener() { @Override public void onGroupInfoAvailable(final WifiP2pGroup group) { if (group != null) { manager.removeGroup(channel, new WifiP2pManager.ActionListener() { @Override public void onSuccess() { isConnectedToAnotherDevice = false; deleteGroup(manager, channel, group); Log.d(TAG, "Removed WiFi Direct Group."); } @Override public void onFailure(int reason) { Log.e(TAG, "Failed to remove a WiFi Direct Group. Reason: " + reason); } }); } } }); }
@Override public void onGroupInfoAvailable(WifiP2pGroup group) { mOwner = group.getOwner(); String owner; if (mOwner.deviceAddress.equals(mDevice.deviceAddress)) { owner = "YOU ARE THE GROUP OWNER"; } else { owner = mOwner.deviceName + " [" + mOwner.deviceAddress + "]"; } TextView view = (TextView) mContentView.findViewById(R.id.my_g_name); view.setText(owner); view = (TextView) mContentView.findViewById(R.id.my_g_status); view.setText("Status: " + AperiMainActivity.getDeviceStatus(mOwner.status)); view = (TextView) mContentView.findViewById(R.id.my_g_is_group_owner); view.setText(mOwner.isGroupOwner() ? "Is: Group Owner" : "Is: Client"); }
@Override @SuppressLint("InlinedApi") public void onReceive(Context context, Intent intent) { if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(intent.getAction())) { WifiP2pGroup wifiP2pGroup = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_GROUP); onWifiP2pGroupChange(wifiP2pGroup); } else if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(intent.getAction())) { int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, 0 /* default to unknown */); onWifiP2pStateChange(state); } }
/** Handle a change notification about the wifi p2p group. */ private void onWifiP2pGroupChange(WifiP2pGroup wifiP2pGroup) { if (wifiP2pGroup == null || wifiP2pGroup.getInterface() == null) { return; } NetworkInterface wifiP2pInterface; try { wifiP2pInterface = NetworkInterface.getByName(wifiP2pGroup.getInterface()); } catch (SocketException e) { Logging.e(TAG, "Unable to get WifiP2p network interface", e); return; } List<InetAddress> interfaceAddresses = Collections.list(wifiP2pInterface.getInetAddresses()); IPAddress[] ipAddresses = new IPAddress[interfaceAddresses.size()]; for (int i = 0; i < interfaceAddresses.size(); ++i) { ipAddresses[i] = new IPAddress(interfaceAddresses.get(i).getAddress()); } wifiP2pNetworkInfo = new NetworkInformation( wifiP2pGroup.getInterface(), ConnectionType.CONNECTION_WIFI, WIFI_P2P_NETWORK_HANDLE, ipAddresses); observer.onNetworkConnect(wifiP2pNetworkInfo); }
/** * The state of Wi-Fi P2P connectivity has changed * Here is where you can request group info * Available extras: EXTRA_WIFI_P2P_INFO, EXTRA_NETWORK_INFO, EXTRA_WIFI_P2P_GROUP * @param intent */ private void handleConnectionChanged(Intent intent) { Log.i(TAG, "Wi-Fi P2P Connection Changed"); if(wifiP2pManager == null) { return; } // Extra information from EXTRA_NETWORK_INFO NetworkInfo networkInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO); if(networkInfo.isConnected()) { Log.i(TAG, "Connected to P2P network. Requesting connection info"); wifiP2pManager.requestConnectionInfo(channel, WifiDirectHandler.this); } else { Intent disconnected = new Intent(Action.COMMUNICATION_DISCONNECTED); localBroadcastManager.sendBroadcast(disconnected); } // Requests peer-to-peer group information wifiP2pManager.requestGroupInfo(channel, new WifiP2pManager.GroupInfoListener() { @Override public void onGroupInfoAvailable(WifiP2pGroup wifiP2pGroup) { if (wifiP2pGroup != null) { Log.i(TAG, "Group info available"); Log.i(TAG, "WifiP2pGroup:"); Log.i(TAG, p2pGroupToString(wifiP2pGroup)); WifiDirectHandler.this.wifiP2pGroup = wifiP2pGroup; } } }); }
@Override public void onConnectionInfoAvailable(final WifiP2pInfo info) { /* This method is automatically called when we connect to a device. * The group owner accepts connections using a server socket and then spawns a * client socket for every client. This is handled by the registration jobs. * This will automatically handle first time connections.*/ manager.requestGroupInfo(channel, new WifiP2pManager.GroupInfoListener() { @Override public void onGroupInfoAvailable(WifiP2pGroup group) { if (isRunningAsHost && !registrationIsRunning) { if (info.groupFormed && !group.getClientList().isEmpty()) { startHostRegistrationServer(); } } else if (!thisDevice.isRegistered && !info.isGroupOwner) { if (serviceRequest == null) { //This means that discoverNetworkServices was never called and we're still connected to an old host for some reason. Log.e(Salut.TAG, "This device is still connected to an old host for some reason. A forced disconnect will be attempted."); forceDisconnect(); } Log.v(Salut.TAG, "Successfully connected to another device."); startRegistrationForClient(new InetSocketAddress(info.groupOwnerAddress.getHostAddress(), SALUT_SERVER_PORT)); } } }); }
private void deleteGroup(WifiP2pManager manager, WifiP2pManager.Channel channel, WifiP2pGroup wifiP2pGroup) { try { Method getNetworkId = WifiP2pGroup.class.getMethod("getNetworkId"); Integer networkId = (Integer) getNetworkId.invoke(wifiP2pGroup); Method deletePersistentGroup = WifiP2pManager.class.getMethod("deletePersistentGroup", WifiP2pManager.Channel.class, Integer.class, WifiP2pManager.ActionListener.class); deletePersistentGroup.invoke(manager, channel, networkId, null); } catch (Exception ex) { Log.v(Salut.TAG, "Failed to delete persistent group."); } }
private void notify_connectivity_changed(final WifiP2pInfo p2pInfo, final NetworkInfo networkInfo, final WifiP2pGroup group) { executorService.execute(new Runnable() { @Override public void run() { for (IBlaubotWifiDirectEventListener listener : eventListeners) { listener.onConnectivityChanged(p2pInfo, networkInfo, group); } } }); }
@Override public void onGroupInfoAvailable(WifiP2pGroup group) { if (group != null) { Log.d(WiFiDirectActivity.TAG, "onGroupInfoAvailable - " + group.getNetworkName()); Log.d(WiFiDirectActivity.TAG, "onGroupInfoAvailable - " + group.getPassphrase()); TextView view = (TextView) mContentView.findViewById(R.id.group_ip); view.setText(group.getNetworkName() + " - " + group.getPassphrase()); for (WifiP2pDevice device : group.getClientList()) { Log.d(WiFiDirectActivity.TAG, "group.getClientList() - " + device); } } }
@Override public void onGroupInfoAvailable(WifiP2pGroup group) { wifiP2pGroup = group; try { Method getNetworkId = group.getClass().getMethod("getNetworkId"); getNetworkId.setAccessible(true); mNetId = (Integer) getNetworkId.invoke(group); } catch (Exception e) { e.printStackTrace(); } onNFGameNotify(); Log.d(TAG, "wifiP2pGroup = " + wifiP2pGroup); }
@Override public void onConnectionInfoAvailable(final WifiP2pInfo info) { if (mGroupActor != null) { return; } WfdLog.d(TAG, "connection info available"); if (!info.groupFormed) { createGroup(); return; } WfdLog.d(TAG, "group formed"); if (info.isGroupOwner) { instantiateGroupOwner(); } else { mManager.requestGroupInfo(mChannel, new GroupInfoListener() { @Override public void onGroupInfoAvailable(WifiP2pGroup arg0) { if (arg0 == null) { // happens when the go goes away and the // framework does not have time to update the // connection loss return; } WifiP2pDevice groupOwnerDevice = arg0.getOwner(); Integer destPort = mPorts.get(groupOwnerDevice.deviceAddress); if (destPort == null) { Log.e(TAG, "null destPort for group owner"); mManager.removeGroup(mChannel, null); } instantiateGroupClient(info.groupOwnerAddress, destPort); } }); } }
@Override public void onConnectivityChanged(WifiP2pInfo p2pInfo, NetworkInfo networkInfo, WifiP2pGroup group) { // TODO Auto-generated method stub }
@Override public void onGroupInfoAvailable(WifiP2pGroup group) { groupInfo.set(group); latch.countDown(); }
public WifiP2pGroup getWifiP2pGroup() { return wifiP2pGroup; }
@Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) { // UI update to indicate wifi p2p status. int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1); if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) { // Wifi Direct mode is enabled synCarnet.syncService.setIsWifiP2pEnabled(true); } else { synCarnet.syncService.setIsWifiP2pEnabled(false); synCarnet.syncService.setConnected(false); progressDialog = synCarnet.syncService.getProgressDialog(); if (progressDialog != null && progressDialog.isShowing()) { progressDialog.dismiss(); } Toast.makeText(this.synCarnet, synCarnet.getString(R.string.noWifi), Toast.LENGTH_SHORT).show(); } } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) { // request available peers from the wifi p2p manager. This is an // asynchronous call and the calling activity is notified with a // callback on PeerListListener.onPeersAvailable() if (manager != null) { if (!synCarnet.syncService.isConnected() && synCarnet.syncService.isWifiP2pEnabled()) manager.requestPeers(channel, peerList); } } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) { if (manager == null) { return; } NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO); if (networkInfo.isConnected()) { WifiP2pGroup group = (WifiP2pGroup) intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_GROUP); if (group != null) { ServiceStatic.setDevice(group.getOwner().deviceName, group.getOwner().deviceAddress); } // we are connected with the other device, request connection // info to find group owner IP synCarnet.syncService.setConnected(true); progressDialog = synCarnet.syncService.getProgressDialog(); Toast.makeText(synCarnet, synCarnet.getString(R.string.connexionSuccessful), Toast.LENGTH_SHORT).show(); peerList.setIntent(intent); manager.requestConnectionInfo(channel, peerList); if (!displayPeers) { Toast.makeText(synCarnet, synCarnet.getString(R.string.syncing), Toast.LENGTH_SHORT).show(); } } else { synCarnet.syncService.setConnected(false); } } }
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.start_server: if (manager != null && channel != null) { manager.createGroup(channel, new WifiP2pManager.ActionListener() { @Override public void onSuccess() { Toast.makeText(WiFiDirectActivity.this, "Group Initiated", Toast.LENGTH_SHORT).show(); } @Override public void onFailure(int reasonCode) { Toast.makeText(WiFiDirectActivity.this, "Group Failed : " + reasonCode, Toast.LENGTH_SHORT).show(); } }); } else { Log.e(TAG, "channel or manager is null"); } return true; case R.id.display_group_info: Log.d("DEVicedetailfragment", "!!! START SERVICE !!!"); Toast.makeText(getApplicationContext(), "services start !", Toast.LENGTH_SHORT).show(); // start the network service Intent netIntent = new Intent(this, NetworkService.class); // 8988 is the driver port netIntent.putExtra("port", 8988); startService(netIntent); netIntent.putExtra("port", 8989); startService(netIntent); manager.requestGroupInfo(channel, new WifiP2pManager.GroupInfoListener() { @Override public void onGroupInfoAvailable(WifiP2pGroup group) { DeviceDetailFragment fragmentDetails = (DeviceDetailFragment) getFragmentManager() .findFragmentById(R.id.frag_detail); fragmentDetails.setGroupInfo(group.getNetworkName(), group.getPassphrase()); } }); return true; case R.id.start_camera: Intent intent = new Intent(getApplicationContext(), ObjTrackActivity.class); startActivity(intent); return true; default: return super.onOptionsItemSelected(item); } }
@Override public void GroupInfoAvailable(WifiP2pGroup group) { }
/** * Called on changes to the P2p-WiFi connectivity like connects, disconnects, ... * @param p2pInfo the p2p2 info containing group owner state, group owner ip, ... * @param networkInfo the network information * @param group the group of which the connectivity changed */ public void onConnectivityChanged(WifiP2pInfo p2pInfo, NetworkInfo networkInfo, WifiP2pGroup group);
@Override public void onConnectivityChanged(WifiP2pInfo p2pInfo, NetworkInfo networkInfo, WifiP2pGroup group) { }
public void GroupInfoAvailable(WifiP2pGroup group);