Flag Wifi as mobile hotspot based on its name from code

喜夏-厌秋 提交于 2020-01-03 02:53:10

问题


Situation

  • I have an android tablet connecting to an onboard unit (OBU) an a car via WiFi.
  • The OBU has SIM card providing access to internet.
  • The apps on the tablet communicate with server via the internet provided by the OBU.
  • So, basically the OBU serves as mobile hotspot for the tablet.

Problem

  • The tablet performs updates that consume lot of data because it thinks it on Wifi
  • This leads to depleting of the data limit available to the SIM card
  • This leads to failure of the whole solution
  • ... or extra costs for each additional MB (over the limit) of transferred data

Facts

  • I can prevent automatic updates of normal apps on the tablet via settings in google play
  • I can't prevent system updates via the method mentioned in previous point
  • I can mark WiFi as mobile hotspot manually which also prevents the system updates
  • I read: http://www.lorier.net/docs/android-metered
  • I read: https://stackoverflow.com/questions/...

Goal

  • To flag a Wifi network as mobile hotspot from android code based on its SSID

Any ideas?


回答1:


WiFis don't reliably identify themselves as mobile hotspots. Basically, a hotspot tells you nothing about how it's connected to the Internet, so there isn't a clean way of telling. The hotspot's upstream connection could be anything out of the following:

  • a residential (DSL, cable, fiber) Internet connection
  • a mobile data connection
  • a captive portal, which requires authentication on a dedicated web page before you are allowed on the Internet
  • a corporate network, requiring you to use a proxy server for Web access and blocking most non-HTTP[s] services
  • an isolated network with no Internet connectivity at all.

(How to detect if you're connected to one of the latter three is described here).

Radiobeacon (licensed under AGPLv3) has an approach to detect mobile hotspots. (The idea here is somewhat different, as the app georeferences hotspots to use for position tracking, and thus needs to filter out hotspots whose position is likely to change – but those tend to be on mobile data connections, so you should get a good hit rate from them.)

By SSID

This is one of the two methods employed by Radiobeacon. Examine the SSID for patterns that are commonly used in mobile hotspots (strings such as Android, mobile, iPhone and the like). Of course, anyone could use that in their non-mobile hotspot (people do get creative about their SSID names), so there's both a false acceptance and false rejection rate to consider.

By BSSID

The other method employed by Radiobeacon: Look at the BSSID (the hardware address of the hotspot). The BSSID is a hexadecimal string of the form 00:60:0d:c0:ff:ee, of which the first half identifies the manufacturer of the chip. Some chips are used mainly in mobile equipment while others are used in fixed equipment, thus some prefixes indicate a mobile hotspot. A few prefixes will already help you filter out all iPhones. Still, there may be prefixes that are found in both mobile and fixed equipment, and we're not even talking about home routers which have a mobile data connection as their upstream link.

For both these examples, visit the Radiobeacon link for the code containing the blacklists. They have been obtained by trial and error, may be far from complete or even contain errors – but they may serve as a starting point.

Using WiFi Location Services

As a third method, you can also try looking up the geographic coordinates for the BSSID through a service such as Mozilla Location Service. There are a handful of other, smaller providers out there, plus the services offered by Google and Apple, but the last two are probably not legal to use for that purpose.

Though I don't really know the details of how these service handle mobile WiFis, they will need to do take some precaution to prevent using these to establish the user's location. The may eliminate hotspots from the database if they move around too frequently, or give them a low confidence interval, or just keep them in the database and leave it up to the consumer to decide which BSSID-location tuples to use for establishing their location.

If they go for one of the first two approaches, then getting a position with a high confidence (up to 2000m, which is the typical range of a WiFi) is a sign that you're on a fixed WiFi. If they go for the last approach, you will additionally need to establish your position by means of GPS or cell lookup. If that is far away from the WiFi's location (something beyond 4000 meters), that indicates a mobile hotspot.

By External IP Address

A fourth approach you could look into is to determine your external IP address (not the one assigned to your device but the one that your communication peers would see as the originating Internet address), and matching that against a list of Internet providers. You'll need to research that a bit:

  • how to determine your external IP address – it's easiest if you can just set up your own web service that echoes the IP address back to the requester
  • how to find out from the IP address whether it belongs to a mobile carrier – reverse DNS lookup may shed some more light on that.

Again, there is a chance of carriers using the same IP address pool for both mobile and fixed data customers.

In conclusion

All approaches mentioned here may give false negatives or false positives. You will probably want to combine he result of all four methods into a likelihood that you're on a mobile data connection, and avoid moving large amounts of data if the likelihood exceeds a certain threshold.




回答2:


Looking at the settings on my phone, I realized there is a setting for that which I had never noticed (this goes for Cyanogenmod 10.2.1 and stock Android – YMMV with vendor ROMs). In Settings > Data usage, select "Mobile hotspots" from the menu. You'll get a list of all WiFi networks you configured. Check the ones which are on an Internet connection with restricted volume and/or bandwidth.

However, you'll have to flag networks manually once you connect to them. As this is a system setting, Android doesn't allow apps to tamper with that programmatically. In order to do so, you would likely need a rooted device and pry around under the hood.

You would need to:

  • Find where the setting is stored in the file system
  • Modify the data
  • Find out when applications pull the settings: if that happens just prior to the actual update, then just modifying the data might be sufficient for the app to react accordingly. However, if the app gets the setting on startup, you'd have to notify it somehow that the preference has changed.

All in all, this is likely a non-trivial task. You might need to search through the Android source code to familiarize youself with the inner workings of system preferences – how to update them and how to notify applications of changes.




回答3:


Sorry for bringing up an old thread, but this feature is built into the Android OS, but it's a little bit hidden.

  1. Select Settings
  2. Press 'Data Usage'
  3. Open the options menu (three dots in top right)
  4. Select 'Mobile Hotspots'

This will let you select which of your SSIDs are hotspots to minimize data transfer.



来源:https://stackoverflow.com/questions/23912020/flag-wifi-as-mobile-hotspot-based-on-its-name-from-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!