Page tree
Skip to end of metadata
Go to start of metadata

This guide explains "Device" object which gives information about the device used.

Device Methods

There are 8 methods about Device and these are ;

  • canOpenurl : Returns whether an app can open a given URL resource. Returns true if it can open or returns false if it cannot open.

    var url = "myscheme://myparam";
    var canOpen = Device.canOpenUrl(url);
  • getPackageInfo : This method is available for Android only. Returns versionName value of Android getPackageManager().getPackageInfo() method.

    Device.getPackageInfo({
    	packageName : "io.smartface.SmartfaceDemo",
    	onSuccess : function (e) {
    		alert("app version name:" + e.versionName);
    	},
    	onError : function (e) {
    		alert("Package doesn’t exist");
    	}
    });
  • isApplicationInstalled : Returns an application with given package name is installed on device. Returns true if it is installed or returns false if it is not installed.

    var pn = "biz.smartface.android.apps.smarftace_demo";
    var isAppInstalled = Device.isApplicationInstalled(pn);
  • makeCall: Calls the given phone number.

    var SOS = true;
    if (SOS) {
    	Device.makeCall("911");
    }
  • setAccelerometerStatus : Enables to set whether accelerometer status open or not.

    Device.setAccelerometerStatus(true);
  • setGPSStatus : Enables to set whether GPS status open or not.

    if (Device.GPSAllowed == false) {
    	Device.setGPSStatus(true);
    }
  • vibrate : Runs the device vibrate property on or off. This action is used to vibrate the handset for given time period in milliseconds.

    Device.vibrate(3000);


captureScreen: Used to get screen capture of device. Returns "e.path" value which gives the captured image’s path. You can use it directly by giving this path to an image object. captureScreen method has 5 input parameters:

Device.captureScreen(path, callback, rectangle, format, compressionRate);

If rectangle is not provided, it gets the entire screenshot as in the below example.
Let's use a label to show "e.path" value, an image object to show the captured image and a button to trigger captureScreen method.

var myLabel = new SMF.UI.Label({
	left : "5%",
	top : "5%",
	width : "90%",
	height : "30%",
	text : ""
});
page1.add(myLabel);
var myImage = new SMF.UI.Image({
	left : "15%",
	top : "30%",
	width : "70%",
	height : "50%"
});
page1.add(myImage);
var myCaptureButton = new SMF.UI.TextButton({
	left : "15%",
	top : "85%",
	width : "70%",
	height : "10%",
	text : "Capture Screen",
	onPressed : function (e) {
		Device.captureScreen(
			"test.png",
			function (e) {
				if (!e.error) {
					alert(e.path);
					myLabel.text = e.path;
					myImage.image = e.path;
				}
			},
			SMF.ImageFormat.PNG,
			0.7
		);
	}
});
page1.add(myCaptureButton);

As you see in the code above, the label will show on top of the page, below that there will be an image object, and at the buttom there will be a textButton object.

  • At first it will look like as in the 1 picture above.
  • When pressed “Capture Screen” button for once, it will look like as in the 2nd picture.
  • And others are the view of other presses.

In the above example, no rectangle was set. Now let's use captureScreen method by setting rectangle and see the result.

var myLabel = new SMF.UI.Label({
		left : "5%",
		top : "5%",
		width : "90%",
		height : "30%",
		text : ""
	});
page1.add(myLabel);
var myImage = new SMF.UI.Image({
		left : "15%",
		top : "30%",
		width : "70%",
		height : "50%"

	});
Pages.page1.add(myImage);
var myCaptureButton = new SMF.UI.TextButton({
	left : "15%",
	top : "85%",
	width : "70%",
	height : "10%",
	text : "Capture Screen",
	onPressed : function (e) {
		Device.captureScreen(
			"test.png",
			function (e) {
				if (!e.error) {
					alert(e.path);
					myLabel.text = e.path;
					myImage.image = e.path;
				}
			}, 
			{
				top : 0,
				left : 0,
				width : 300,
				height : 200
			},
			SMF.ImageFormat.PNG,
			0.7
		);
	}
});
page1.add(myCaptureButton);


Because we set rectangle’s top and left 0,  method took the screenshot beginning from 0,0 point on screen, and the result looks like as in the screenshot.


Device Properties

"Device" has 37 properties, please refer to the API Docs.

Device Events

There are 3 events for a device and these are ;

  • onAccelerate : Creates action(s) that are run when Accelerometer detect a change.

    var varAccX = 0;
    var varAccY = 0;
    var varAccZ = 0;
    Device.onAccelerate = function (e) {
    	varAccX = e.x;
    	varAccY = e.y;
    	varAccZ = e.z;
    }
  • onConnectionTypeChanged : Creates action(s) that are run when type of the network connection changed.

    Device.onConnectionTypeChanged =function (e) {
    	alert("your connection type is : " + Device.connectionType);
    }
  • onLocationChanged : Creates action(s) that are run when location changed.

    var myLat = null;
    var myLng = null;
    Device.onLocationChanged = function (e) {
    	myLat = e.lat;
    	myLng = e.lng;
    }