Trying to create a useful application for Pebble

    Greetings, harazhiteli!

    I recently became the proud owner of a Pebble smartwatch. In functional terms, my expectations were fully met, but I didn’t buy them just for the sake of using ready-made software that would greatly simplify / automate my life. For me, the very development itself was just the development for this gadget.

    Having played enough with examples from the official repository, having tested the cloud IDE, it was decided to do something really useful without fail. Given the fact that the watch itself does not bring much benefit and you get the main benefit only after integrating it with the phone, it was decided to implement yet-another-one notifier. A quick search on Google Play, plus going over the head of options for useful notifications led me to the idea of ​​monitoring the battery level in a mobile phone. I don’t know how anyone, but quite often I forget to charge the phone on time and I hope that another source of kicks about this will come in handy.

    The architecture is pretty trivial:
    1) A service that runs in the background and is subscribed to notifications of changes in battery level.
    2) A simple UI whose purpose is an ON / OFF service.
    3) A notifier that sends a warning from the phone to the watch that the charge is already low.

    And here are some nuances of implementation:
    First, I will make a reservation that the Pebble API is just one java class, which can be downloaded from the link [3].

    1) In order not to make the application annoying, I chose the following approach in notifications: to notify starting with a charge level of 25% every 5% (i.e. 25%, 20% ... and so on up to 5%) . To subscribe to receive system messages (including changing the charge level), 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) And here is the actual method of sending a message in hours:
        public Intent getPebbleNotificationIntent(final String title, final String body) {
            final Intent notificationIntent = new Intent("com.getpebble.action.SEND_NOTIFICATION");
            Map data = new HashMap() {{
                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 status of the service (everything is very simple, I will not give the code), as well as the status of connecting the clock, here 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 - it is better to see once ...





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

    That's all. On this, I will probably finish and go to come up with Appendix_№2. I will also be glad to reviews / criticism / advice.
    To whom it is interesting, below there are links to the application itself, source codes and some useful articles / resources used during development.

    Thank you for reading.

    Useful links:
    1) github.com/ice-pro/Phone-Battery-Status-in-Pebble- a repository with the source codes of the program
    2) play.google.com/store/apps/details?id=com.x_lab.ice.PhoneBatteryMonitorForPebble - the application itself 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

    Also popular now: