在Android wifi热点中获取wifi广播地址 [英] Getting wifi broadcast address in Android wifi hotspot

查看:398
本文介绍了在Android wifi热点中获取wifi广播地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用wifi在拥有我的应用的同一网络中的所有手机之间广播UDP消息的应用。

I'm developing an app that uses wifi to broadcast UDP messages between all the mobiles that are in the same network that have my app.

我设法发送了/从许多具有外部AP的手机中接收数据包,这就是我的路由器。

I managed to send/receive packets from many cellphones having an external AP, that's my router.

但是考虑到没有AP,我希望用户能够使用手机的Wifi热点功能,因此他们仍然可以使用我的应用程序。因此,其中一部手机将是wifi热点,而其他所有手机都将连接到该热点。

But given the case that there's no AP, I want users to be able to use their phone's Wifi Hotspot feature so they can still use my aplication. So one of the cellphones would be the wifi hotspot, and all the others would connect to that.

我要求用户自己拥有一个wifi。到外部AP或到所述热点。然后,当我的应用程序启动时,它会检查电话是否连接到wifi网络,并调用WifiManager.isWifiEnabled()和NetworkInfo.isConnected()。

I require users to connect to a wifi by they own. Either to an external AP or to the said hotspot. Then when my application starts, it checks whether the phone is connected to a wifi network, calling the WifiManager.isWifiEnabled() and NetworkInfo.isConnected().

问题是如果我在使用热点的手机中调用这些函数,则函数isConnected()将返回false。而且我无法使用WifiManager.getDhcpInfo()获取广播地址。
连接到热点的其他电话运行正常。但是,由于WifiManager已禁用,因此热点手机无法发送任何广播。

The problem is that if I call these functions in the cellphone that's using hotspot, the function isConnected() will return false. And I can't get the broadcast address using WifiManager.getDhcpInfo(). The other phones that are connected to the hotspot they do work perfectly. But the hotspot cellphone can't send any broadcast, since WifiManager is disabled.

因此,我的问题是有没有办法检查当前是否正在使用手机一个wifi热点?如果可以,有什么办法可以获取它的广播地址?

So, my question would be "Is there any way to check if a cellphone is currently being a wifi hotspot? And if so, is there any way I can get its broadcast address?"

推荐答案

首先,您可以检查一下是您的IP地址:

First you can check what is your IP address :

public InetAddress getIpAddress() {
  InetAddress inetAddress = null;
  InetAddress myAddr = null;

  try {
    for (Enumeration < NetworkInterface > networkInterface = NetworkInterface
      .getNetworkInterfaces(); networkInterface.hasMoreElements();) {

      NetworkInterface singleInterface = networkInterface.nextElement();

      for (Enumeration < InetAddress > IpAddresses = singleInterface.getInetAddresses(); IpAddresses
        .hasMoreElements();) {
        inetAddress = IpAddresses.nextElement();

        if (!inetAddress.isLoopbackAddress() && (singleInterface.getDisplayName()
            .contains("wlan0") ||
            singleInterface.getDisplayName().contains("eth0") ||
            singleInterface.getDisplayName().contains("ap0"))) {

          myAddr = inetAddress;
        }
      }
    }

  } catch (SocketException ex) {
    Log.e(TAG, ex.toString());
  }
  return myAddr;
}

,我使用此IP通过以下方式进行广播:

and I used this IP to get Broadcast this way:

public InetAddress getBroadcast(InetAddress inetAddr) {

    NetworkInterface temp;
    InetAddress iAddr = null;
    try {
        temp = NetworkInterface.getByInetAddress(inetAddr);
        List < InterfaceAddress > addresses = temp.getInterfaceAddresses();

        for (InterfaceAddress inetAddress: addresses)

            iAddr = inetAddress.getBroadcast();
        Log.d(TAG, "iAddr=" + iAddr);
        return iAddr;

    } catch (SocketException e) {

        e.printStackTrace();
        Log.d(TAG, "getBroadcast" + e.getMessage());
    }
    return null;
}

当然可以用一种方法来完成,但是在我的实现中将其分开分为两种方法很有用。

It can be done of course in one method, but in my implementation separating it into two method was useful.

要确定是否启用Wifi系绳,可以使用以下代码:

To identify if Wifi Tether is on you can use this code:

WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
Method[] wmMethods = wifi.getClass().getDeclaredMethods();
for (Method method: wmMethods) {
    if (method.getName().equals("isWifiApEnabled")) {

        try {
            if ((Boolean) method.invoke(wifi)) {
                isInetConnOn = true;
                iNetMode = 2;

            } else {
                Log.d(TAG, "WifiTether off");
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }
}

如果客户设备需要知道服务器设备是否为移动热点,可以使用特定的IP地址。据我所知,所有网络共享设备都具有相同的地址192.168.43.1,在Android 2.3和4. +上是相同的,已在许多手机和平板电脑上选中。当然,这不是最佳解决方案,但它很快。在我的应用程序中,客户端设备进行检查(将数据包发送到该地址),而我的服务器设备以预定义的方式进行响应,例如 yesIamInTheterModeIamYourServer。

If client devices needs to know if server device is mobile Hotspot, specific IP address can be used. As far as I know, all Tethering devices have the same address 192.168.43.1 It is the same on Android 2.3 and on 4.+, Checked on many phones and tablets. Of course it is not best solution but it is fast. In my application client devices checks (sends packet to this address) and my server devices response in predefined way like "yesIamInTheterModeIamYourServer".

这篇关于在Android wifi热点中获取wifi广播地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆