App: Wifi Hot-Spot on Oreo


Hey. This page is more than 6 years old! The content here is probably outdated, so bear that in mind. If this post is part of a series, there may be a more recent post that supersedes this one.

I rooted my Pixel XL to try and fix the Trigger app’s refusal to turn on wifi hot-spotting automagically when I turn my car on. The reason it’s not working, I think, is that Trigger is borderline abandonware and has not been updated for Oreo’s new (lack of) exposure of hotspot function in WifiManager.

I wrote a little app (which turned out to be quite a lot of screwing around) to fix this. It runs in the background and provides a way to turn WiFi HotSpot off and on with [cci]Intents[/cci].

If you are simple instructions for setting up on Trigger – they’re here.

The source is still here on GitHub.

Once you’ve installed the app, you can turn on the Oreo device’s WiFi HotSpot (from another/ your application) with the [cci]action[/cci] element route or [cci]data[/cci] element route. To turn the hotspot off you replace the ‘on’ in all the code/links below with ‘off’.

Action Element

[cc lang=”java”]
Intent intent = new Intent();
intent.setAction(“com.fitc.wifihotspot.TURN_ON”);
sendBroadcast(intent);
[/cc]

***EDIT 02/04/2018***
Time-old code above for broadcasting an intent does not work in Oreo. Oreo introduced an Implicit Broadcast Ban – more about that here
A (temporary?) workaround is listed on that same CommonsWare article:
[cc lang=”java”]
Intent intent = new Intent(“com.fitc.wifihotspot.TURN_ON”);
sendImplicitBroadcast(this,intent);
[/cc]
[cc lang=”java”]
private static void sendImplicitBroadcast(Context ctxt, Intent i) {
PackageManager pm=ctxt.getPackageManager();
List matches=pm.queryBroadcastReceivers(i, 0);

for (ResolveInfo resolveInfo : matches) {
Intent explicit=new Intent(i);
ComponentName cn=
new ComponentName(resolveInfo.activityInfo.applicationInfo.packageName,
resolveInfo.activityInfo.name);

explicit.setComponent(cn);
ctxt.sendBroadcast(explicit);
}
}
[/cc]
Unfortunately,waht this means is that if you are using some third-party app where these is an option to broadcast implicit intents, then unless they have implemented this workaround (a something else which works), my app willnot work.

Data Element

[cc lang=”java”]
Uri uri = new Uri.Builder().scheme(“wifihotspot”).authority(“turnon”).build();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(uri);
startActivity(i);
[/cc]

The code above is the same as you would use for getting your app to open your default Android browser on whatever-website.

FYI, the [cci]data[/cci] route uses that same stuff under-the-bonnet as what Android does with a hyperlink on a website. In fact, if you have installed the app on your phone and you are reading this in your browser on your phone, then clicking on [cci lang=”html”]wifihotspot://turnon[/cci] SHOULD turn on your hotspot?!? Maaagic! Click  [cci lang=”html”]wifihotspot://turnoff[/cci] and off it goes. Whoaaaa!!

EndNote

There were two hurdles I ran into. Too drunk to explain them in detail, but you can see how I overcame them in GitHub code::

  1. [cci]data[/cci] element in an [cci]intent[/cci] only picked up by ‘Browsers’ (which must be [cci]Activity[/cci]), not my [cci]BroadcastReceiver[/cci] (as I’d hoped)
  2. It’s a whole lot of pain getting at Oreo’s hotspot tethering – more here.