📜 ⬆️ ⬇️

Attempt to create a useful application for Pebble

Greetings, habrazhiteli!

Recently, I became the proud owner of Pebble smart watches. In functional terms, my expectations were fully met, only I bought them not only for the sake of using ready-made software, which allows pretty much to simplify / automate life. For me, a very interesting side was the development for this gadget itself.

Having played enough with examples from the official repository, having tested the cloud-based IDE, it was decided to do something really useful. Considering the fact that the clock itself does not bring much benefit and you get the main benefit only after integrating it with the phone, it was decided to implement the yet-another-one notifier. A quick search on Google Play, plus a brute-force search for useful notification options, led me to the idea of ​​monitoring the battery level in a mobile phone. I do not know how anyone, but I often forget to charge the phone on time and I hope that another source of kicks about this will be the way.

The architecture is rather trivial:
1) Service that runs in the background and is subscribed to notifications about changes in battery level.
2) Simple UI, the purpose of which is the ON / OFF service.
3) Notifier sending from phone to watch a warning that the charge is too low.
')
Here are some implementation details:
To begin with, I’ll make a reservation that the Pebble API is just one java class that can be downloaded from the link [3].

1) In order not to make the application annoying, I chose the following approach in the notifications: notify starting from charge level 25% every 5% (ie 25%, 20% ... and so on up to 5%) . To subscribe to receive system messages (including a change in the level of charge), we need a BroadcastReceiver.
My looks like this:
private BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() { @Override public void onReceive(Context c, Intent i) { //    int currentLevel = i.getIntExtra("level", 0); //         if((currentLevel <= 25) && (currentLevel % 5 == 0) && isPebbleConnected()) { Intent message = pebbleKit.getPebbleNotificationIntent( getString(R.string.msgWarningLabel), getString(R.string.msgWarningText) + currentLevel + "%" ); sendBroadcast(message); } } }; 

This receiver must be registered as follows:
  registerReceiver(batteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); 

2) But the actual way to send a message to the clock:
  public Intent getPebbleNotificationIntent(final String title, final String body) { final Intent notificationIntent = new Intent("com.getpebble.action.SEND_NOTIFICATION"); Map<String, String> data = new HashMap<String, String>() {{ put("title", title); put("body", body); }}; final JSONObject notificationPack = new JSONObject(data); final String notificationData = new JSONArray().put(notificationPack).toString(); notificationIntent.putExtra("messageType", "PEBBLE_ALERT"); notificationIntent.putExtra("sender", "BatteryMonitor"); notificationIntent.putExtra("notificationData", notificationData); return notificationIntent; } 

3) There is also a little user interface consisting of an icon in the status bar and the main screen of the application, where you can see the service status (everything is very simple, I will not give the code), as well as the connection status of the clock, everything is again implemented using BroadcastReceiver:
  PebbleKit.registerPebbleConnectedReceiver(ui.getApplicationContext(), new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { ui.pebbleConnected(); } }); PebbleKit.registerPebbleDisconnectedReceiver(ui.getApplicationContext(), new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { ui.pebbleDisconnected(); } }); 

Result:
I will be brief, and I will follow the proverb - better to see once ...





Summarizing:
1) A couple of hours of Java coding, which allowed us to stretch our brains and recall the basic components of the Android application.
2) Photoshop, a little mess with the design of the application and resources - we develop creative, as well as fine motor skills.
3) A ready-made app on Google Play - positive emotions and good mood (until the criticism has fallen down) is priceless!

That's all. On this I will probably finish and go to come up with Appendix_№2. Also I will be happy reviews / criticism / advice.
Who cares, below there are links to the application itself, the source code and some useful articles / resources used in the development.

Thanks for reading.

Useful links:
1) github.com/ice-pro/Phone-Battery-Status-in-Pebble - a repository with the source code of the program
2) play.google.com/store/apps/details?id=com.x_lab.ice.PhoneBatteryMonitorForPebble - the actual application in Google Play
3) cloudpebble.net/ide - cloud development environment
4) developer.getpebble.com/2/getting-started/linux - Pebble SDK
5) github.com/pebble/pebble-sdk-examples - examples of working with Pebble SDK
6) developer.android.com/guide/topics/ui/notifiers/notifications.html - implementation of notifications
7) developer.android.com/training/monitoring-device-state/battery-monitoring.html - battery level
8) developer.android.com/reference/android/app/Service.html - services on Android
9) www.tutorialspoint.com/android/android_broadcast_receivers.htm - BroadcastReceiver

Source: https://habr.com/ru/post/229637/


All Articles