How to get started

First thing you will need is a device with Android P preview installed. I grabbed a Pixel 2 and followed the instructions in the documentation to get the latest build installed. Don’t worry, nothing will break! Do it under your own responsibility though ;) One important thing to note here is that the device you choose should support the 802.11mc RTT WiFi protocol (Pixel 2 supports it, but I am not certain of which other devices out there do).

After that, you have to make sure you also have the proper environment to start developing, only the newest versions of Android Studio support building for Android P. You can download the 3.2 Canary 7 build here, which at this moment is the latest one. Then, you can go to Android SDK manager and download the Android P dev Preview revision 2 (some of the functionality for RTT wasn’t working for me in version 1 and I had to update).

Now everything is set up and you are ready to finally…

Start coding

Here are the steps to use the new API classes in your advantage:

Request permissions

You need to add the corresponding lines to the manifest file and request permissions in runtime like it is explained here.

Register a receiver for the result of the WiFi scan

You will need to scan the area around the mobile device. Therefore, you need to register a broadcast receiver to be notified with the result of this operation. Something like this:

registerReceiver(object : BroadcastReceiver() {

override fun onReceive(context: Context, intent: Intent) { ... } }, IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION))

Proceed with the WiFi scan

In order to discover the surrounding access points, you can use the Android class wifiManager . For this purpose I used wifiManager.startScan . This method appears to be deprecated in Android P, but I couldn’t find any alternatives so far. If someone has found any other way of doing this targeting Android P, a comment with a hint is more than welcome!

Build a RangingRequest

You can use the results of the WiFi scan from your broadcast receiver. This happens inside the block of your BroadcastReceiver callback onReceive and looks something similar to:

val rangingRequest = RangingRequest.Builder()

.addAccessPoints(scanResults.takeMax(RangingRequest.getMaxPeers()))

.build()

takeMax function is an extension that will take either the size of the list or the max specified in the parameters, kotlin sometimes is just wonderful:

fun <T> MutableList<T>.takeMax(max: Int) = this.subList(0, minOf(size, max))

Start the ranging

Now that you have the request ready, you can start ranging by using the new RttManager class from Android P:

rttMngr.startRanging(rangingRequest,object:RangingResultCallback() {

override fun onRangingResults(

results: MutableList<RangingResult>) { ... }



override fun onRangingFailure(p0: Int) { ... }

}, null)

And inside the callback onRangingResults you can do whatever you plan with the received results. There are a couple of useful methods in the class RangingResult such as getDistanceMm or getMacAddress . One important note is that you need to check if the result is actually a success or a failure through the method getStatus in order to handle only valid data.

TL;DR

The new support to RTT protocol by Android P opens a whole new world of possibilities. I am really thrilled to find out what the community will build with this. In my opinion indoor navigation will drag a lot of attention with these advances.

This is just a simple scratch of the basic procedure to get information on the surrounding area and about the access points.

Here is the code of the sample app I built:

https://github.com/RafaO/android-P-RTT

Disclaimer 1: Don’t expect to see any beautiful code in the repository. It was just a try out of new functionalities.

Disclaimer 2: Be aware that both the device where you run the app and the access points that will be delivered to you in the RangingResult array, need to support the IEEE 802.11mc WiFi protocol.

Thanks for reading!