Category Archives: Mobile Application Development

JSON in Javascript Explained

What is JSON in Javascript?

JSON stands for Javascript Object Notation. JSON is nothing but an object which contains key value pairs. Advantage of JSON over other data format is it is lightweight. JSON is platform independent as well as language independent data exchange format.

JSON object example

Example:

var jsonObj = {id:1,name:”hiteshagja”};

In this example Keys are id and name and Values are 1(typeof int), hiteshagja(typeof string).

So If you write alert(jsonObj.name) in javascript you will get alert containing text hiteshagja. Isn’t this so amazing about JSON.

Add New Property(key) to already existing JSON object.

Example 1:

jsonObj.email=”hiteshagja24@gmail.com”;

Or

jsonObj[“email”]= ”hiteshagja24@gmail.com”;

This will add new key email to already existing jsonObj. You don’t need to write anything else other than this.

But if you write like

Example 2:

jsonObj.name=”hiteshagja.com”;

Or

jsonObj[name]=”hiteshagja.com”;

This will only update our JSON object key value from hiteshagja to hiteshagja.com.

JSON currently supports

String
Number
Object
Array
Boolean
Data types and
Null value

Get JSON object’s keys count

Object.keys(jsonObj).length

will return number of keys in given JSON object. In our case this will return 3(id,name and email).

Delete key

delete jsonObj[“email”]

this will remove email key value pair from jsonObj. Delete is a keyword which will accomplish this task.

String to JSON object

Example:

var jsonStr=’{“id”:”1”,”name”,”hiteshagja”}’;

Let’s say you have string value just like shown above and you want to convert it into JSON object.

var

jsonObj=JSON.parse(jsonStr);

Or

var jsonObj=eval(jsonStr);

will convert JSON string into object.

JSON object to String

In some cases you will be needed converting JSON object into string there you can use conversion like shown below.

Example:

var jsonStr= JSON.stringify(jsonObj);

This will convert jsonObj into string.

JSON Array

Till now you have seen how to create single JSON object. Now in some cases we will need to store data as an array. Let’s see how we can do so.

Example 1:

var jsonObj={id:1,name:”hiteshagja”,phone:[1111111111,2222222222]};

Here as you can see I have written phone value as an array.

You can access phone number using jsonObj.phone[0] just like you are accessing other array objects.

Example 2:

var jsonObj={

Products:[ {name:”Product1”

color:”Red”

code:1},

{name:”Product2”

color:”Green”

code:2},

{name:”Product3”

color:”Blue”

code:3}]

};

Example 2 is real time scenario where you will have array of products. jsonObj is array of products which contains 3 products in our example.

If you want to access product 2 then you can simple write

Products[1][Key].

I will keep updating this post whenever I will find anything interesting about JSON.

jQuery Signature Pad using HTML5 Canvas

With introduction of HTML5 Canvas we can easily create jquery signature pad plug-in. This article is all about jquery based signature pad that works in almost all modern browsers.

Recently I come across situation where I need to provide browser based Signature Pad and It should work fine in all the devices including apple devices. So I don’t have any other option left then HTML5 Canvas.

If you want to create a browser based signature pad then visit the link below.

http://thomasjbradley.ca/lab/signature-pad/

To save signed signature visit the link below. (CODE IS IN C#)

http://www.dotnetfunda.com/articles/article1662-saving-html-5-canvas-as-image-on-the-server-using-aspnet.aspx

Encode Text Message to PDU

Encode Text Message to PDU before submitting it to network. [Note: this is applicable if you are going to send SMS in PDU mode]. In this post I am going to explain how you can do this, but before that please download sample code from Planet Source code. This code contains Convert, Sending and Receiving modules. Frankly speaking I have got encoding/decoding logic from this code and it works perfectly for me. Only thing missing in this code is Padding Bit logic. Code is quite simpler and easily understandable. Please go through it and tell me if you have problem encoding text message into PDU.

How to add Padding bit for multipart SMS PDU?
Open the code > go to Convert Module > Find “CharHex”  Method.
Do replace this function with the code shown below.

Public Function CharHex(ByVal Txt As String, ByVal bit As Integer, ByVal PaddingBit As Boolean)
Dim i As Integer, bin As String, nbin As String, n As String
Dim bil As Integer, sisa As Integer, lbin As Integer, nol As String
bin = ""
nbin = ""
If bit = 7 Then
For i = 1 To Len(Txt) Step 2
n = Mid(Txt, i, 2)
bin = HexToBin(n) & bin
Next
bil = Len(bin) bit
sisa = Len(bin) Mod bit
For i = 1 To (Len(bin) - sisa) Step bit
' MsgBox Chr$(HexToDec(BinToHex(Mid(bin, i + Sisa, bit))))
nbin = Chr$(HexToDec(BinToHex(Mid(bin, i + sisa, bit)))) & nbin
Next
Else
For i = 1 To Len(Txt)
n = Mid(Txt, i, 1)
bin = Biner(Asc(n)) & bin
Next
If PaddingBit = True Then
bin = bin + "0"
End If
sisa = Len(bin) Mod bit
If sisa > 0 Then
For i = 1 To bit - sisa
nol = nol & "0"
Next
End If
bin = nol & bin
bil = Len(bin) bit
For i = 1 To bil
nbin = nbin & BinToHex(Mid(bin, Len(bin) + 1 - bit * i, bit))
Next
End If
CharHex = nbin
End Function

That’s it !!
Do let me know if you have any problem encoding text to PDU.

Update:

I am getting lot of queries regarding Message PDU for single message and concatenated message. So What I have done is I have created a quick sample to generate PDU for single message and for multipart SMS.

Download Sample (Generate SMS PDU)

Send Long SMS or Multipart SMS using AT Command

In this post I will explain you, how to send long SMS or multipart SMS or concatenated SMS using at command. I am writing about most awaited post of my blog with great excitement. Before I explain you I want to share my experience during searching for the secret of long SMS. When I started searching on internet about “how to send concatenated sms”, I found resource on wiki which explains the basic of long SMS and PDU format for sending long SMS. But this page (wiki) contains link at bottom of the page and this link (trust me) one of the best resource on the internet to learn about concatenated SMS. Here is the link if you want to read this blog : http://mobiletidings.com/2009/02/18/combining-sms-messages/.  But still I was having same problem not able to send long SMS using AT command. But reading this blog I was sure that I was missing something in my PDU which cause problem. Then I thought of taking help from someone who is master in this so I did contact Franco (owner of http://www.franco7777.net/), who is one of the commenter on “http://mobiletidings.com/2009/02/18/combining-sms-messages/” blog. He gave me very nice response and explained me where my mistake was.  And after that I had got the clear idea about sending long SMS over network.

Now I want to show you how you can send long SMS. But before reading this post further I will strongly recommend you to read http://www.dreamfabric.com/sms/ and grab basic idea about SMS PDU structure. When you send single SMS ( <= 160 Character) there is not UDH exist in the message but when you try to send long SMS ( > 160 Character) you need to add this UDH field into the message PDU. So the basic question is how to add this header in to the message. One more thing you should know to send concatenated SMS, and the thing is when you send single SMS there is no User Data Header so little less confusion and you don’t need to add padding bit to encode your message into GSM 7-bit encoding scheme but if you are going to send concatenated SMS, UDHL is exist and it is in septets so you need to add one padding bit compulsory to encode your SMS. Read http://mobiletidings.com/2009/02/18/combining-sms-messages/ blog to get the clear idea because I don’t want to post same content again in this post.

So basically there are 2 secret to send long SMS

  1. Add User Data Header
  2. Add Padding bit to encode your message.

If I want to send

Message:

  • Message 1: “This is Part 1.
  • Message 2: “This is Part 2.

Message PDU should be

Message 1 - PDU

Message 1 – PDU

Message 2 - PDU

Message 2 – PDU

That’s it , you are done !!

Things to know before sending Long SMS is , UDH will occupy 6 octets so original message size should be reduce.

Please let me know if I am missing something so I can update this post.

Update:

I am getting lot of queries regarding Message PDU for single message and concatenated message. So What I have done is I have created a quick sample to generate PDU for single message and for multipart SMS.

Download Sample (Generate SMS PDU)

Send Long SMS/Multipart SMS/Concatenated SMS

Send Long SMS/Multipart SMS/Concatenated SMS

Here I am writing about most awaited post of my blog with great excitement.  In this post I will explain you , how to send long SMS or multipart SMS or concatenated SMS. Before I explain you I want to share my experience during searching for the secret of long SMS. When I started searching on internet about “how to send long sms”, I found resource on wiki which explains the basic of long SMS and PDU format for sending long SMS. But this page (wiki) contains link at bottom of the page and this link (trust me) one of the best resource on the internet to learn about concatenated SMS. Here is the link if you want to read this blog : “http://mobiletidings.com/2009/02/18/combining-sms-messages/”.  But still I am having same problem not able to send long SMS using AT command. But reading this blog I am sure that I am missing something in my PDU which cause problem. What I have done is I have contacted Franco (owner of “http://www.franco7777.net/”), who is one of the commenter on “http://mobiletidings.com/2009/02/18/combining-sms-messages/” blog. He gave me very nice response and explains me where my mistake is.  And after that I have got the clear idea about sending long SMS over network.

Now I want to show you how you can send long SMS. But before reading this post further I will strongly recommend you to read “http://www.dreamfabric.com/sms/” and grab basic idea about SMS PDU structure. When you send single SMS ( <= 160 Character) there is not UDH exist in the message but when you try to send long SMS ( > 160 Character) you need to add this UDH field into the message PDU. So the basic question is how to add this header in to the message. One more thing you should know to send concatenated SMS, and the thing is when you send single SMS there is no User Data Header so little less confusion and you don’t need to add padding bit to encode your message into GSM 7-bit encoding scheme but if you are going to send concatenated SMS, UDHL is exist and it is in septets so you need to add one padding bit compulsory to encode your SMS. Read “http://mobiletidings.com/2009/02/18/combining-sms-messages/” blog to get the clear idea because I don’t want to post same content again in this post.

So basically there are 2 secret to send long SMS

  1. Add User Data Header
  2. Add Padding bit to encode your message.

If I want to send

Message:

Message 1: “This is Part 1.

Message 2: “This is Part 2.

Message PDU should be

******Image*******

That’s it , you are done !!

Things to know before sending Long SMS is , UDH will occupy 6 octets so original message size should be reduce.

Please let me know if I am missing something so I can update this post.

Receiving SMS Using AT Command

Receiving SMS using AT command is not as simple as send a message. Reading message from memory location is always a big headache J. I waste lot of time just to read an SMS using AT command. That is the reason why I have written this post to help others.

Before I start explaining, you must be sure that your handset supports SMS read functionality using an AT command.

How can I test whether my handset supports or not?

Answer is pretty simple, try executing AT+CMGR=? AT Command. If it returns ”rnOKrn” then congratulation your handset modem supports Read SMS functionality.

If you know SMS memory location then task will be simpler.

  1. First of all set memory type.  Use  AT+CPMS=”ME” command  to set memory from which you are going to read SMS.
  2. After that execute  AT+CMGR=”messageID”  to read message.

Tip : Whenever you execute AT command never forget quotation mark (“), Carriage Return etc.

You will get message text directly if you are working under TEXT mode. In case of PDU mode you need to convert PDU into Original Message Text by writing some algorithms.

If you want indication for incoming SMS in your application then you need to enable this functionality.

Try “AT+CNMI=?” Command to test whether new message indication functionality is available in your handset or not.

If result does not contain any error and will return some parameters then congratulation again J

Now, use “AT+CNMI=2,1,0,1″ AT command to enable new message indication to TE (terminal equipment).

Last but not the least, don’t forget to use “AT+CSMS=0″ AT command set  ISO 8859-1 character set.

Send SMS using AT Commands

If you are writing an application which send SMS using AT commands then, you must read this post. Few days ago I started developing an application which is completely AT command based. Here I am going to explain the things that I learnt from experience.

First and the foremost thing before you start developing an application based on AT command , know which mode (text/pdu) your mobile handset supports.

If handset supports TEXT mode then work is simple.

  1. First you need to set text mode by using AT+CMFG=1 Command.
  2. And then use AT+CMGS=”+CellPhoneNumber” <enter>.
  3. If command execute successfully then It will return “>” as a response.  
  4. After getting success response simply write your message ex. “this is test SMS”. This will submit SMS to network and will return the Message ID.
  5. If you want to send long message i.e. greater than 160 character length message then, this is really very useful.

 I will cover how to send long message in my succeeding posts.

 If handset supports PDU mode then work will be little lengthy and need some more efforts. 

  1. For PDU mode, you need to set pdu mode by using AT+CMFG=0 Command.
  2. And then write AT+CMGS=<length><CR> command for modem to response. Here you can’t send the length of actual message. You need to convert the text into 7-bit or 8-bit encoding scheme. I included some useful links and sample application at the bottom of the page to accomplish this task.
  3. If command execute successfully then It will return “>” as a response.  
  4. After getting success response, send <PDU><ctr-z/esc > sequence. This will submit SMS to network and will return the Message ID.
  5. If you want to send long message i.e. greater than 160 character length message then, this is really very useful.

 Reference :

  1. PDU message format : http://www.dreamfabric.com/sms/
  2. Complete information about all AT Commands : http://www.developershome.com/sms/operatingMode.asp
  3. Text to PDU converter : http://nerdlabs.org/tools/pdu.php

 Sample Application :

  1. PDU Decoder : http://www.codeproject.com/KB/IP/PDUDecoder.aspx
  2. Sample Applications : http://www.scampers.org/steve/sms/samples.htm

Do share new things with me.

Working with AT Commands (Mobile Application)

In this article I am going to explain how you can use at commands for mobile application development. Before I am going to explain AT command I want to share some knowledge with you. AT commands are for GSM modem only so in order to execute at command you need mobile with GSM modem. Each mobile has either a Text mode or PDU mode or both. Depend upon the different mobile manufacture. So there are different at commands for different modes.

How can you execute AT Command ?
You can execute AT commands by programmatically or by HyperTerminal.
Go to Start >> Programs >> Accessories >> Communications >> HyperTerminal

And then connect your mobile using serial port and start play with AT commands.

If you want to develop a mobile application then you should go with C/C++, VB, VB.net, C#.net. My choice of language is C#.net.

.net has a class for serial port communication which you can use directly and built your application. You need the deep knowledge of threading and delegate to develop such kind of application.
Capabilities of AT commands are seamless. By using AT commands you can send SMS,Receive SMS, Call etc… In short you can handle all your mobile handset function by using your PC.
AT commands for Text Mode :
AT+CSMS Select Message Service
AT+CPMS Preferred Message Storage
AT+CMGF Message Format
AT+CSCA Service Centre Address
AT+CSMP Set Text Mode Parameters
AT+CSDH Show Text Mode Parameters
AT+CSCB Select Cell Broadcast Message Types
AT+CSAS Save Settings
AT+CRES Restore Settings
AT+CNMI New Message Indications to TE
AT+CMGL List Messages
AT+CMGR Read Message
AT+CNMA New Message Acknowledgement to ME/TA
AT+CMGS Send Message
AT+CMSS Send Message from Storage
AT+CMGW Write Message to Memory
AT+CMGD Delete Message

AT commands for PDU Mode :
AT Attention command
AT+CMGL List message
AT+CMGR Read message
AT+CMGS Send message
AT+CMSS Send from storage
AT+CMGW Write message to memory
AT+CMGD Delete message
You can download complete AT command list from the links below.

For text mode :
 Download PDF
For pdu mode :
Download PDF
These two files contain the detailed information on what are the commands for Mobile Handset and what will be the output of each command. It is easy believe me !!
 
NOTE : all the content I wrote is related to mobile application development using AT commands. AT commands are very useful for serial port communication with attached device on serial port.