js/sms.js
changeset 0 54063d8b0412
equal deleted inserted replaced
-1:000000000000 0:54063d8b0412
       
     1 /**
       
     2  * This class provides access to the device SMS functionality.
       
     3  * @constructor
       
     4  */
       
     5 function Sms() {
       
     6 
       
     7 }
       
     8 
       
     9 /**
       
    10  * Sends an SMS message.
       
    11  * @param {Integer} number The phone number to send the message to.
       
    12  * @param {String} message The contents of the SMS message to send.
       
    13  * @param {Function} successCallback The function to call when the SMS message is sent.
       
    14  * @param {Function} errorCallback The function to call when there is an error sending the SMS message.
       
    15  * @param {PositionOptions} options The options for accessing the GPS location such as timeout and accuracy.
       
    16  */
       
    17 Sms.prototype.send = function(number, message, successCallback, errorCallback, options) {
       
    18     try {
       
    19 		if (!this.serviceObj)
       
    20 			this.serviceObj = this.getServiceObj();
       
    21 			
       
    22 	    // Setup input params using dot syntax
       
    23 	    var criteria = new Object();
       
    24 	    criteria.MessageType = 'SMS';
       
    25 	    criteria.To = number;
       
    26 	    criteria.BodyText = message;
       
    27 
       
    28       	var result = this.serviceObj.IMessaging.Send(criteria);
       
    29     	if (result.ErrorCode != 0 && result.ErrorCode != "0")
       
    30 		{
       
    31 			var exception = { name: "SMSError", message: result.ErrorMessage };
       
    32 			throw exception;
       
    33 		} else {
       
    34 			successCallback.call();
       
    35 		}
       
    36     }
       
    37   	catch(ex)
       
    38   	{
       
    39 		errorCallback.call({ name: "SmsError", message: ex.name + ": " + ex.message });
       
    40   	}
       
    41 
       
    42 };
       
    43 
       
    44 
       
    45 //gets the Sms Service Object from WRT
       
    46 Sms.prototype.getServiceObj = function() {
       
    47 	var so;
       
    48 	
       
    49     try {
       
    50         so = device.getServiceObject("Service.Messaging", "IMessaging");
       
    51     } catch (ex) {
       
    52 		throw {
       
    53 			name: "SmsError",
       
    54 			message: "Failed to load sms service (" + ex.name + ": " + ex.message + ")"
       
    55 		};
       
    56     }		
       
    57 	return so;
       
    58 };
       
    59 
       
    60 if (typeof navigator.sms == "undefined") navigator.sms = new Sms();