Script your life

Nowadays 89% of People use smartphones. That is phones, which are said to be smart. Because, what makes a silicium and plastic compound smart? The general image is, that you can surf the net with it, although this is only new for the teens of today. Already, in 1999 the wap had a fairly acceptable price…you just have to wait till a WRML page is downloaded :). So, we can reach the world wide web with it, has a GPS receiver, you can take pictures with it, moreover having only one lens to do so is considered embarrassing. There are even apps for it. So, if we take a step back and examine it, we can see that it is a device that has 10+1 ways to connect us to groups, information clusters and the only smart function it has is to notify us when there is something new.
It may be a bit disappointing, but let’s not throw right away our beloved gadget. If we start thinking about the uses of all the interfaces and sensors that were stuffed inside this thin device, we can fall into an even deeper pit. In case of an Android phone, you must download the ANDROID SDK, then connect it to some sort of a development device, speaking in Java does not hurt either, moreover, if we do not want to try our program on our own phone, we can build virtual ones….

Now, that I have demoralized everyone, let me introduce a little app, that I bumped into not so long ago.

The concept is the following: we can download an agent to our device. Then, on a web interface, with JavaScript, you can initiate events according to the signals produced by the phone’s sensors. A typical example is, when the battery level drops to 8%, your device automatically sends a message to your partner. Something like: „Honey, my battery is dead, I’m on my way home!”. After we’ve written the command, the only thing left is to save it. The agent then downloads it and starts using it right away.

What we’re going to need:

  • A device, capable of using Android (in our case it’s an Android 2.3.5 HTC Desire s)
  • Access to Facebook.
  • And the client, which can be downloaded from here.


http://goo.gl/xE3pm

everything goes well, we’ll have the icon of ON{X} amongst our apps.

After this, visit https://onx.ms . This little garage comes from the labs of project Microsoft and it is still in beta phase, that is why you can access it with Facebook. This was met with some negative feedback from the users, but I think it is for the increase of popularity.
After logging in, we can find rather good reference instructions, with plenty of example codes. We’ll have a look at some of them here:

Every programmers first program is „Hello World!”

// Triggers the function when the screen unlocks
device.screen.on(“unlock”, function(){
var notification = device.notifications.createNotification(‘Hello world!’);
notification.show();
console.log(‘Hello World notification was sent to the phone’);
});

After we saved this, our phone taken out, the screen lock unlocked our smart software  starts.

Let’s see something „more useful”. Above I’ve already mentioned the automatic SMS to our partner if the electric leash  discharges. Then our only thing is to do to write this script:

// Initializing variables
var friend = { name : “my wife”,phoneNumber : “+1234567890” } ;
var messageText = “my phone’s battery is dying”;
var batteryPercent = “8” /*8% */;

            // End of variables initializing

            console.log(‘Started script: Text ‘ + friend.name+ ‘,  when the battery reaches ‘ + batteryPercent + ‘%’);
var hoursBetweenTexts = 4;
var millisecondsBetweenTexts = hoursBetweenTexts * 60 * 60 * 1000;

    // Function to fire when the battery status is updated
device.battery.on(‘updated’, function (signal) {
var currentPercent = signal.data.percentage;
var now = new Date().getTime();

        // get the last seen percentage, default to the current percentage if have not seen any
var lastBatteryPercent = device.localStorage.getItem(‘lastBatteryPercent’) || currentPercent;
lastBatteryPercent = JSON.parse(lastBatteryPercent);

        var lastTextSentTimestamp = device.localStorage.getItem(‘lastTextSentTimestamp’) || now – millisecondsBetweenTexts;
lastTextSentTimestamp = JSON.parse(lastTextSentTimestamp);

        // Send text when:
//   The battery is at or below the batteryPercent level
//   and the percent is going down (not charging)
//   and it has been more than the hoursBetweenTexts since the last text

if (currentPercent <= batteryPercent && (lastBatteryPercent > currentPercent) && (now – lastTextSentTimestamp >= millisecondsBetweenTexts)) {
device.messaging.sendSms(
{
to: friend.phoneNumber,
body: messageText
},
function (err) {
if (err) {
console.error(‘Error sending text message: ‘ + JSON.stringify(err));
return device.notifications.createNotification(‘Battery at ‘ + currentPercent + ‘%. Could not text ‘ + friend.name + ‘.’).show();
}

                     device.localStorage.setItem(‘lastTextSentTimestamp’, JSON.stringify(now));
return device.notifications.createNotification(‘Battery at ‘ + currentPercent + ‘%. Texted ‘ + friend.name + ‘.’).show();
});
}
device.localStorage.setItem(‘lastBatteryPercent’, JSON.stringify(currentPercent));
});
console.log(‘Completed script: Text ‘ + friend.name+ ‘,  when the battery reaches ‘ + batteryPercent + ‘%’);

Have fun!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s