I am working on an app that would allow indoor navigation using hardware devices that emit wifi signals in large public places such as hotels and hospitals. Each location would have multiple devices and each device would be unique so that we can identify the exact location of the device/person.
For this I don't want the user to connect to the wifi, if only a scan of the network was possible which could determine the current wifi beacon the mobile is closest to would do the job. I decided to use Android's WifiManager for the task:
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(true);
wifi.startScan() ;
List<ScanResult> mScanResults = wifi.getScanResults();
String s = "" ;
for(ScanResult result: mScanResults){
Log.d("Wifi", result.SSID) ;
}
None of the attributes for result seem to be appropriate for use as a UID for Wifi Beacon, I have two questions:
a) Can we use something like a BSSID for this purpose?
b) Can I write my own wifi manager class and then somehow negotiate a handshake with the wifi where it just tells me who it is without connecting? If so please share resources for doing the same.
Related
I want the ability to specify and constrain the interface over which UDP datagrams will be sent.
I want to do this without physically disabling the "non wanted" interface. Assume standard INET connectivity is available over the chosen interface (in fact assume that if both interfaces are active then INET connectivity is available over boti)
Most mobile devices will send data over Wifi (in preference to Data interface) if the Wifi interface is enabled; but I want to know how to grammatically "force" UDP over my chosen interface.
There are similar questions already posted, but no question hits the topic precisely. Also I would like the programmed solution to be available (generically or via support library) as FAR BACK as possible in terms of Android SDK versions.
Please note that question "Android how to select wifi network interface to send udp packet" is old and was NEVER answered.
There is an issue with cyanide's answer - how does one ascertain the valid WiFi interface value associated with getName() for ALL devices, current and future?
The solution I have come up with would entail finding the IP address associated with the WiFi interface, then obtaining a UDP socket which is BOUND to this address. Getting a handle onto the WiFi interface can be done by accessing the WiFiManager:
WifiManager wifiManager =
(WifiManager) context.getApplicationContext ()
.getSystemService (Context.WIFI_SERVICE);
.....
int localInetAddressInt = wifiManager.getDhcpInfo ().ipAddress;
ByteBuffer tmp = ByteBuffer.allocate (4);
tmp.putInt (localInetAddressInt);
InetAddress localInetAddress = InetAddress.getByAddress (tmp.array ());
DatagramSocket socket = new DatagramSocket (portNumber, localInetAddress);
Then, when sending over the DatagramSocket (socket), the device SHOULD route the datagram over the appropriate interface, i.e. the interface associated with bound IP address, i.e. the WiFi interface to which the IP address is bound. The IP address would normally be associated with the WiFi interface by the WiFi Access Point via DHCP.
Use NetworkInterface.getNetworkInterfaces" to get list of all active interfaces.
Then for each interface use NetworkInterface.getName.For a wifi it will be wlanXX (Samsung and other), tiwlanXX (Motorola), ethXX (HTC) and e0 (Blackberry). With mobile net it most certainly be rmnetXX.
Then you can use NetworkInterface.getInetAddrress for creating socket.
When WiFi is unavailable, only rmnet appears. Similarly, when WiFi is available, rmnet will not appear, even if Mobile Data is enabled.
Added 30May17 In fact, I doubt that is possible to do what you intend, as it looks like the mobile network is automatically switched off when WiFi is available.
WifiManager wifiManager =
(WifiManager) context.getApplicationContext ()
.getSystemService (Context.WIFI_SERVICE);
.....
int localInetAddressInt = wifiManager.getDhcpInfo ().ipAddress;
ByteBuffer tmp = ByteBuffer.allocate (4);
tmp.putInt (localInetAddressInt);
byte swap; for(int i = 0; i < 2; i++) { swap = tmp.get(i); tmp.put(i, tmp.get(3 - i)); tmp.put(3 - i, swap); }
InetAddress localInetAddress = InetAddress.getByAddress (tmp.array ());
DatagramSocket socket = new DatagramSocket (portNumber, localInetAddress);
This worked for me!
EDIT:
Nope it doesn't. When Mobile data is ON, it doesn't go via WIFI
Recently I found a seemingly cool way to communicate between devices using Google Nearby API. Skimming through the documentation didn't answer my question - is it possible to measure the connection signal strength in real time, or should I invent some kludges e.g. measuring time of sending and receiving data or something else?
Thank you.
As a developer of Nearby API stated in thread Google Nearby Messages - Cancel initial link between devices :
Today, Nearby doesn't expose distance directly
He suggests to use BLE RSII instead
You can get a rough approximation of distance by measuring BLE RSSI if one of the devices is capable of BLE advertising. This will be sensitive to how the device is held, antenna gain and environmental factors but better than random.
There's not a straight-forward "tape measure" API, though.
If your question really is about "Nearby Connection API" and not "Nearby Messages API", you could simply check the Wifi-Signal Strength:
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
int numberOfLevels = 5;
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int level = WifiManager.calculateSignalLevel(wifiInfo.getRssi(), numberOfLevels);
Is it possible to allow 2 Android devices to communicate by using SSIDs? One device would broadcast a message by creating a hotspot with the message as its SSID. Nearby devices can read and interpret the message. I couldn't find anything in the Android documentation that allow this. Is it possible on Android devices and how would I do this?
You can use this call to create a hotspot:
private boolean setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled);
...
WifiConfiguration netConfig = new WifiConfiguration();
netConfig.SSID = "\"SSID_NAME\"";
// more configs here.
And to scan wifis around something like:
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
List<ScanResult> results = wifi.getScanResults();
You can read the wifi info from there. Maybe you can identify the hotspot by MAC address.
I am trying to create a help wizard to recover from bad network connections in the app. One test case I hope to handle is the case where an end user has WiFi turned off, WiFi is available, and the mobile network is slower than the WiFi network. In this event, I want to be able to (1) discover the available WiFi network s, (2) find the WiFi network speed, (3) Compare its speed to the mobile network speed, (4) digest the user changes to the faster network.
For this to work, I need to know how to programmatically get information on available connections. Is that something we can do? If so, how can we tell what connections are available? Thanks in advance.
Task-1: Discover the available WiFi network
This can be done by getting WifiManager's instance from the System.
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiManager.getScanResults();
// The above is an async call and will results are available System will broadcast `SCAN_RESULTS_AVAILABLE` intent and you need to set a `BroadCastReceiver` for it.
// And get the results like this
List<ScanResult> results = wifiManager.getScanResults();
Task-2&3: find the network speed
This link gives an answer to your question about how to get network speeds of wifi and mobile network
Wifi:
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
int linkSpeed = wifiManager.getConnectionInfo().getRssi();
In case of mobile it should work:
TelephonyManager telephonyManager = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
CellInfoGsm cellinfogsm = (CellInfoGsm)telephonyManager.getAllCellInfo().get(0);
CellSignalStrengthGsm cellSignalStrengthGsm = cellinfogsm.getCellSignalStrength();
cellSignalStrengthGsm.getDbm();
Then You should compare this signal levels and if WIFI signal is better keep it turn on, but if mobile is better disconnect wifi
Task-4: Switching to the option with higher speed
In Android by default, if wifi is on and connected then your mobile network won't be used. Hence to use mobile data you must either disconnect from all available wifi-networks or switch off the wifi.
I will also suggest you to read link this, this and this for getting more information on how to get connection speed.
Company http://renewlondon.com/ have the terminal stations that collect all near by mac addresses
Can I via iOS SDK, and Android SDK,do the same thing?
You can access the wifi data using 'WifiManager' and after the scanning the scanresult contain all the data like
BSSID The address of the access point.
SSID The network name.
capabilities Describes the authentication, key management, and encryption schemes supported by the access point.
frequency The frequency in MHz of the channel over which the client is communicating with the access point.
level The detected signal level in dBm.
timestamp Time Synchronization Function (tsf) timestamp in microseconds when this result was last seen.
about the wifi devices.
if you need more related to coding, I think I can help you...
Sample code
WifiManager wManager;
List<ScanResult> wifiList;
wManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
// Inside BroadcastReceiver()
wifiList = wManager.getScanResults();
for (int i=0; i<wifiList.size(); i++){
ScanResult scanresult = wifiList.get(i);
System.out.println("SSID: "+ssid);
System.out.println("RSSI: "+scanresult.level);
System.out.println("Frequency: "+scanresult.frequency);
System.out.println("BSSID: "+scanresult.BSSID);
System.out.println("Capability: "+scanresult.capabilities);
}
Also checkout the BroadcastReceiver().
One way i can think of doing this is making your device as wifi hotspot and use some hidden api to discover devices.You are basically trying to mimic an access point.
Otherwise each device would need some p2p framework on them-either wifi direct on or some other framework like alljoyn or samsung chord which helps in peer to peer discovery