Boilerplater Libraries: Device Utils

Image for post
Image for post

A lot of my apps make use of things like the users contact list, evaluating bluetooth & wifi connections to trigger some particular action, displaying a list of the users currentlyinstalled apps.. Many basic things that have a few common pitfalls (OOM app icons anyone?) & that are pretty tedious to write over and over again across multiple apps. So I put them into a library 🤓

Nothing overly fancy, just a few util classes that I use in numerous production apps that might just save you a bit of time (& potential headache). They’re also (for the most part) wrapped in RxJava2 observables so if you’re using Rx in your app (& if you’re not I’d highly recommend you should be) things like getting the currently connected Bluetooth device is now a ton easier to implement cleanly into your app.

They’re all very straightforward &sensibly named (at least to me 😛) so I’ll just give a quick overview over some of the most useful methods so you can determine if they might be of any use to you..

AppUtils

getInstalledApps(Context context) — Observable that returns a list of the currently installed apps on the device with the intent “android.intent.action.MAIN” & the category of android.intent.category.LAUNCHER.

This class also provides method to get a bitmap of the app icon for the provided package name (getAppIcon) as well as another straightforward method to get the app label for a particular package (getAppLabel).

ContactUtils

This class provides numerous methods to query contacts based on their name, their contact id, their phone number, their uri (supplied via the extra People field in some chat apps), as well as methods to get a list of all the contacts on a users device (getContactsObservable).

As well as that it also provides methods to retrieve a contacts avatar & another to retrieve all phone numbers for the specific contact.

WifiUtils

getSavedWifiNetworks — Returns a list of all the saved wifi networks on a users device.

getConnectedWifi — Returns the currently connected wifi network.

This class also contains numerous other method to retrieve the wifi name from an id & vica versa, pretty useful when a broadcast receiver for wifi only returns an id.

BluetoothUtils

getPairedDevices — Returns a list of all the bluetooth devices paired to a device.

getDeviceAliasName — Easily retrieve the name of a bluetooth device based on it’s bluetooth address, pretty useful as it’ll return the custom name a user might have set for their bluetooth device if there is one i.e. “Kitchen Speaker”.

getConnectedBluetoothDevice — Returns the currently connected bluetooth device if there is one.

Simples.

There’s a few other methods in each class not mentioned but they’re all pretty sensibly named so feel free to take a look & see if there’s anything useful to you :)

You can find the library on GitHub here and you can add it to your project from Gradle via the JitPack repository (instructions on Jitpack if you don’t know how).

Feel free to use it, pick it apart, improve upon it.. And let me know what you think in the comments :)

Side note: This library will be continuously updated as my apps are built upon & I find needs for other functionality but as it stands this is the library currently used in ReadItToMe, Can’t Talk & Remindee so I’m pretty confident in it for production use.

Written by

Freelance Android Developer since 2012 🎙️Host of CoffeeAndCodingPod.com 🌍 World Tourist ☕ Coffee Addict | robj.me

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store