Android P and Wi-Fi RTT
Recently, Google launched the first developer preview build of Android P. With this, several new features were announced, including two that caught my attention. Those being Neural Networks 1.1 and Wi-Fi RTT. Wi-Fi RTT peaked my initial interest because it was something completely new in Android P.
What is Wi-Fi RTT?
Wi-Fi Round Trip Time is a part of the IEEE 802.11mc specification. This specification defines how to determine a device’s distance from wireless access points, henceforth referred to as APs. Given the location and distances from three different APs, we can use multilateration to determine a device’s location within a couple of meters.
How Does Wi-Fi RTT Work?
The formula that holds the key to allowing us to determine the distance from a device to an AP comes from high school physics, d = vt. In other words, distance is equal to your velocity, or in this case speed, times the time you’re traveling. So, if we measure the amount of time it takes for data to get from an AP to our device and then multiply that by the speed, which for wireless signals is the speed of light, we will get the distance to that AP, which can be used in the multilateration algorithm.
This is Creepy!
My first reaction, and the first of many people who I talked to about this feature, was that being able to know my location within a couple of meters is a new level of creepy. However, when I thought about this more, I realized it isn’t any more creepy compared to the location services we have right now. Currently, we can typically pinpoint a device’s location to roughly the building they are located at, which the extra fine tuning of the location information doesn’t make it any more creepy. Also, since this is in the location framework on Android P, all of the built in permission settings still apply, including the app by app location permissions that have been in place since Android Marshmallow. It will be up to each user if they want to gain the additional benefits of the fine grained location information. Additionally, all of the distance calculations are done on the device so the APs do not know your location.
What are the Benefits?
Some of the greatest areas that I can see immediate benefit from these high precision locations are in the realm of automation, indoor navigation, and smart home voice commands. An example in the automation realm would be if a device operates in different rooms indoors, it could use the location to know exactly what room it is in and perform the proper tasks for that room. Indoor navigation would be a great thing for events like conventions where you have a bunch of people coming to a new area and needing to find their way around the space in a hurry without having to find a map with a “You are here” indicator on it. Home voice commands is the canonical example Google provided in the announcement in letting the assistant know which room you mean when you say, “Turn off the lights.” I look forward to seeing the many other beneficial uses that we come up with.
Where’s the Code?
While I wanted to have a working sample available for everyone, that won’t be the case currently as there is a bug with the startRanging method (https://issuetracker.google.com/issues/74362130). However, I will give my understanding of the general flow for the process, which the elements of this can be referenced at their documentation at https://developer.android.com/reference/android/net/wifi/rtt/package-summary.html:
- Add the ACCESS_FINE_LOCATION and ACCESS_WIFI_STATE to your manifest
- Verify you have the ACCESS_FINE_LOCATION permission in your app, if not request the permission
- Get the WifiManager and WifiRttManager
- Using WifiManager, get a list of wifi networks you can see as a list containing ScanResult objects
- Using the scanResults create a RangingRequest via RangingRequest.Builder().addAccessPoints(scanResults).build()
- From here, you would then use the WifiRttManager you previously referenced to startRanging and within the callback’s onRangingResult(), you would then be able to access each of the AP’s RangingResult object.
- Within each RangingResult, you should be able to get the distance via getDistanceMm() and then the location of the AP via the LocationConfigurationInformation obtained from getReportedLocationConfigurationInformation()
- The latitude and longitude can be obtained via the methods getLatitude() and getLongitude() on the LocationConfigurationInformation object. Additionally, you can get your altitude if you want to distinguish between different floors in the building you are in.
- From here you would use a multilateration algorithm to determine the location.
Look forward to a follow up post in the future, when the bug is patched, where we take a dive into the guts of the code and the multilateration algorithm.