Search code examples
androidandroid-intentnullpointerexceptionactionwifi-direct

How to get Wifip2pInfo object in wifi direct


It's giving a null pointer exception.I would like to get the IP address of a device connected via wifi direct.How to do this can anyone explain?Attached a Screen to see. Thanks in advance.


Solution

  • You can use following code snippet, When WiFiP2pInfo has connection then it is not null but when there is no connection or connection lost then it will be null.

    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()) {
                manager.requestConnectionInfo(channel,
                        new ConnectionInfoListener() {
    
                            @Override
                            public void onConnectionInfoAvailable(
                                    WifiP2pInfo info) {
                                if (info != null) {
                                    activity.setConnectionInfo(info); // When connection is established with other device, We can find that info from wifiP2pInfo here.
                                }
                            }
                        }
    
                );
            } else {
                activity.resetData(); // When connection lost then we can reset data w.r.t that connection.
            }
        }
    

    Following code helps to find address of Client and Owner,

    public static String getDestinationDeviceIpAddress(WifiP2pInfo wifiP2pInfo) {
        String destinationAddress;
        if (wifiP2pInfo.isGroupOwner) {
            destinationAddress = WifiDirectUtil.getIPFromMac();
    
        } else {
            destinationAddress = wifiP2pInfo.groupOwnerAddress.getHostAddress();
        }
        return destinationAddress;
    }
    
        public static String getIPFromMac() {
        BufferedReader br = null;
        boolean isFirstLine = true;
        String ipAddress = null;
        try {
    
            br = new BufferedReader(new FileReader("/proc/net/arp"));
            String line;
    
            while ((line = br.readLine()) != null) {
                if (isFirstLine) {
                    isFirstLine = false;
                    continue;
                }
    
                String[] splitted = line.split(" +");
                Log.d(TAG, "** length **" + splitted.length);
                if (splitted.length >= 4) {
    
                    String device = splitted[5];
                    Log.d(TAG, device);
                    if (device.contains("p2p")) {
                        ipAddress = splitted[0];
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return ipAddress;
    }
    

    And add permission Read External storage in manifest.xml.