Sunday, July 1, 2012

Twitter Integration with Mobile Application

While integrating our mobile application with Twitter, I want to share the steps.

1 - Create you application in Twitter developer page using the mentioned link : https://dev.twitter.com/apps/new

2 - Fill all the details mentioned in the Twitter new application page, and create a new app.

3 - Now, Once your application will create, it will give you these information.
  • Consumer key
  • Consumer secret
  • Access token
  • Access token secret

Now, there are two ways to integrate Twitter with your mobile.
a - Single User Implementation :
This works only for a single user. It means, only one user will register with the application, and every time when user want to post any data in Twitter, it will post only to his/her Twitter page.
This can be achieved by this way :

//Get authorization to twitter.
Credential credentials = new Credential(consumerKey, consumerSecret, accessToken);

//Create account manager for tweet the post.
UserAccountManager accManager = UserAccountManager.getInstance(credentials);

//Verify credentials and post to twitter
try {
                    if(accManager.verifyCredential()) {
                        Dialog.alert("Authenticated");
                        //post data to Twitter

                    }else{
                        Dialog.alert("Authentication Failed");
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    System.out.println("IOException Occured: "+e.getMessage());
                    e.printStackTrace();
                } catch (LimitExceededException e) {
                    // TODO Auto-generated catch block
                    System.out.println("LimitExceededException Occured: "+e.getMessage());
                    e.printStackTrace();
                }


b - Multiple User Implementation :
This works for multiple user. Any user can enter his username and password, and post data to his/her twitter profile.
For this, first our application should be authorized by Twitter. To do this, we need to send an Authorization mail to api@twitter.com. Once, the application will authorize, you will get a confirmation mail.

Now, to share your post to Twitter, do this :

                Credential credentials = new Credential("username or email", "password", "consumerKey", "consumerSecret");
                UserAccountManager accManager = UserAccountManager.getInstance(credentials);
           
                //If your application is authenticated then it returns token value, else return null.
                Token token = accManager.getAccessToken();
               
                try {
                    if(accManager.verifyCredential()) {
                        Dialog.alert("Authenticated");
                        //post data to Twitter

                    }else{
                        Dialog.alert("Authentication Failed");
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    System.out.println("IOException Occured: "+e.getMessage());
                    e.printStackTrace();
                } catch (LimitExceededException e) {
                    // TODO Auto-generated catch block
                    System.out.println("LimitExceededException Occured: "+e.getMessage());
                    e.printStackTrace();
                }


- Here cunsumerKey and consumerSecret are reference to the application. And token is reference to authorized user. If your application is not authorized, application will throw Http 401 error (Unauthorized Exception).

Retrieve cell id for location, sign it for S40

How to retrieve cell id :

Here is the code to retrieve cell id, to know your location :

getCellId() {
//for s40 series
id = System.getProperty("Cell-ID");
//for s60 series
id = System.getProperty("com.nokia.mid.cellid");
}

This code will works only for s40 3rd edition fp1, and s60 3rd edition fp2.
for s60 fp2 devices, it shows you easily the cell id of device to know your location. But for s40 FP1, it gives you null value. To get the cell id, we need a certification for it.

Wednesday, March 28, 2012

Floating Point problem in Android OpenGL

While working with OpenGL in Android devices, I found a bit trouble with Float Point Values problem.

Some of the older version devices (mostly android 2.0 or less) hardware does not support floating point. Hence if we draw any 3D image using OpenGL, it will not display that drawing on older devices. To work on OpenGL ES it is better to draw the graphics using fixed point values.

List of devices that supports floating point values :
All Samsung Galaxy mobiles.
HTC Salsa, T-Mobile G2, AKA, HTC Dream,
Droid, Nexus
Motorola Milestone, NEON,

ARM-based Qualcomm MSM7201 CPU does not have VFP (floating point coprocessor) and therefore floating point calculations have to be done via software-float.

Sunday, December 13, 2009

Push API, open your application on push

J2ME Push API :

It's a very powerful concept to receive some information asynchronously on particular port, instead of forcing the application to use synchronous polling techniques.

It is introduced on MIDP 2.0 (JSR 118).

How to invoke application:

Initially your application is on sleep mode. As soon as any push message comes to a particular port which is register for push messages, in place of moving into inbox, it invokes the application first.
It makes an alternate entry point for application to invoke it.

It does not make any change in application life cycle, but only creates an alternative way to start your application without invoking by user.


fig 2



It's responsibility shared between AMS and MIDlet, which is split as :
1 : When MIDlet is on sleep : AMS monitors the push event continuously. When any push message hit the registered port, AMS invokes the appropriate MIDlet to handle it.
2 : When MIDlet is running : MIDlet directly handles the push event.


//This code execute when MIDlet activates by push
public void messageReceivedHandler (TextMessage receivedMessage) {
String senderAddress = receivedMessage.getAddress();
String receivedMessageBody = "No msg received as yet";
//Receives the text message from sms
receivedMessageBody = receivedMessage.getPayloadText();
System.out.println("Received Message = "+receivedMessageBody);
}



/**
* Notification that a message arrived.
* @param conn the connection with messages available
*/
public void notifyIncomingMessage(MessageConnection conn) {
if ((thread == null) || (conn == smsconn)) {
done = false;
thread = new Thread(this);
thread.start();
System.out.println("Notify Incoming Message received..");
}
else
System.out.println("Incoming Msg, but thread is alive");
}

Wednesday, November 4, 2009

Sign your J2ME Midlet

MIDlet Signing process steps can be summarized into :
1. Generate public/private key pairs.
2. Generate Certificate Signing Request (CSR) and submit it to CA .
3. Sign the MIDLet with the certificate.


Software requirements:
1. Sun java 2 SDK or JRE
2. Sun Wireless Tool Kit (WTK)

Steps :

1. Generate public/private key pairs
a. Create a Keystore:
From command line Navigate to java 2 SDK bin directory and type the below command :
keytool -genkey -keyalg rsa -keystore -alias
it prompts you to enter a password for your keystore, your name, organization, and address.
b. List the generated keypairs
To see your generated keypairs entry ,type the below command :
keytool -keystore -list

2. Generate Certificate Signing Request (CSR) and submit it to CA :
You need to generate a (CSR) for the enrollment process.
a. generate CSR
Type the below command to create a CSR for the key pair in the keystore:
keytool –certreq –file –keystore -alias
b. submit the generated CSR to CA

3. Sign the MIDLet with the certificate:
Once the CA has approved your request, you will receive the certificate, the steps are:
a. Import the certificate to your Keystore
Use the below command
keytool –import –trustcacerts –keystore -alias -file
b. Sign the JAD file
Use JadTool utility to signs a JAR file by adding both of the certificate and the JAR file’s digital signature to the Java Application Descriptor (JAD) file, the process will be :
b.a. Adding the certificate to JAD

use JadTool.jar tadd the certificate as the value of an attribute named MIDlet-Certificate-m-n, where m is the number of the certificate chain, and n is an integer that, for new certificates, begins at one and increments by one each time you add a new certificate to the JAD file.
Java -jar %WTK_HOME%\bin\JadTool.jar -addcert -keystore -alias -storepass -inputjad -outputjad
You should see the below attributes added to your output jad file MIDlet-Certificate-1-1,MIDlet-Certificate-1-2,MIDlet-Certificate-1-3.
b.b. Adding JAR’s digital signature to the JAD
Gets the JAR’ digital signature, stores it as the value of the MIDlet-Jar-RSA-SHA1 attribute of the output JAD file.
java -jar %WTK_HOME%\bin\jadtool.jar -addjarsig -jarfile -keystore -alias -storepass -keypass -inputjad -outputjad
Where The default value for -jarfile is the MIDlet-Jar-URL property in the JAD file.

Blackberry singleton object return issue, and chunk stream returning property

If you want to run to instance of any class, they do not work parallel at a time. That make a big issue that I found while working with a application. Like if you want to run / record audio and video stream using multimedia API's, then it is not possible. Because for that you need to run audio and video simultaneously. Which is not possible due to singleton object returning nature of lower layer of Blackberry OS.

Also, if you want to run it via. invocation of camera, it it also creates a big issue that it buffered all the stream data internally while you recording stream. Once you stop the stream then, it throws all the stream in a buffer provided by you. So, you can't do any live recording of audio or video strem due to the OS issue on blackberry.

Even multimedia api's also supported by 4.6 and above OS devices. Else, for the lower OS device, you really need to invoke the Camera.