【问题标题】:How to get Wifip2pInfo object in wifi direct如何在 wifi direct 中获取 Wifip2pInfo 对象
【发布时间】:2015-08-06 04:07:45
【问题描述】:

它给出了一个空指针异常。我想获取通过wifi直接连接的设备的IP地址。如何做到这一点谁能解释一下?附上Screen看看。 提前致谢。

【问题讨论】:

  • 我已经更新了我对您问题的回答。请检查一下,让我知道它是否适合您。

标签: android android-intent nullpointerexception action wifi-direct


【解决方案1】:

你可以使用下面的代码sn-p,当WiFiP2pInfo有连接时不为空,当没有连接或连接丢失时为空。

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.
        }
    }

以下代码有助于找到客户和所有者的地址,

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

并在 manifest.xml 中添加读取外部存储权限。

【讨论】:

  • 感谢您提供 Umang Kothari 的信息,但那里的“活动”是什么??那里显示一些错误,说符号无法解析
  • 是的,activity 是受尊重的 Activity 的对象,您已从该 Activity 注册了此接收器,并且您希望返回有关该 Activity 的连接信息。
  • 它在 java.net.InetAddress.getHostByAddrImpl(InetAddress.java: 438) 在 java.net.InetAddress.getHostName(InetAddress.java:307) 在 com.example.hp1.myproject3.MainActivity$Myreceiver$2.onConnectionInfoAvailable(MainActivity.java:231) 在 android.net.wifi.p2p.WifiP2pManager$ Channel$P2pHandler.handleMes
  • 因为获取IP地址是网络操作,你必须在工作线程而不是UI线程上进行这样的操作。使用 asynctask 机制获取 IP 地址
  • 它仍然在信息对象 kothari 上显示空指针异常。无论如何,非常感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-26
  • 2013-12-14
  • 1970-01-01
相关资源
最近更新 更多