50% OFF!!!

Sunday, May 23, 2010

J2ME | read BlackBerry device name, device id (under MIDP)

Hello to all.

I searched the web and not found any implemantaion of reading device name or device id using J2ME MIDP for blackberry.

There exists an RIM api: "net.rim.device.api.system.DeviceInfo.getDeviceName()" which allow you to read the blackberry device information, but on when creating a global application for all MIDP devices, this api will not work for other devices like Sony-Ericsson, NOKIA, Samsung, Motorola, HTC and so...

The solution is to dynamicly load an outer JAR (dynamic library = dll) only when a blackberry device detected and read all data.
NOTE: if we will access to this library (JAR) without validating the RIM device, an excaption will be thrown!!

So, the solution is:
Step 1:
Create a new project: Mobile Class Library.
Name it: BlackberryInfo

Step 2:
create new Class named: DeviceInfo
with the following code:

Code for step 2:
package BlackberryInfo;

public class DeviceInfo
{
   static public String getDeviceName()
   {
      return net.rim.device.api.system.DeviceInfo.getDeviceName();
   }

   static public String getManufacturerName()
   {
      return net.rim.device.api.system.DeviceInfo.getManufacturerName();
   }

   static public int getDeviceId()
   {
      return net.rim.device.api.system.DeviceInfo.getDeviceId();
   }

   static public String getPlatformVersion()
   {
      return net.rim.device.api.system.DeviceInfo.getPlatformVersion();
   }
}

Step 3:
Build project and get JAR and JAD named: BlackberryInfo.jar
Note: build this JAR using Blackberry BlackBerry JDE (I used 5.0.0) (BlackBerry JDE downloads)
if you want, i can upload this library to the blog...

Step 4:
In your application (J2ME MIDP), just add BlackberryInfo.jar as library.

Step 5:
Now, all you have to do, is just to validate that this is RIM Blackberry device, and the read all device info.

Code for step 5:
static public boolean isBlackberryPlatform()
{
    try
    {
        Class.forName("net.rim.blackberry.api.browser.Browser");
        return true;
    }
    catch (Exception ex)
    {
        return false;
    }
}

// some func for read device information (device name, device id, device
 if (isBlackberryPlatform() == true)
{
            System.out.println("Blackberry platform:");
            System.out.println(BlackberryInfo.DeviceInfo.getDeviceName());
            System.out.println(BlackberryInfo.DeviceInfo.getManufacturerName());
            System.out.println(BlackberryInfo.DeviceInfo.getPlatformVersion());
            System.out.println(BlackberryInfo.DeviceInfo.getDeviceId() + "");
}



Notice that, the JAR is loaded only when trying to access it, so other devices expect RIM blackberry will NOT get to this line so they won't try to load unavailable api of rim blackberry.

reference to DeviceInfo implemantation (5.0.0)
http://www.blackberry.com/developers/docs/5.0.0api/net/rim/device/api/system/DeviceInfo.html

hope i could help someone outthere...
p.s.
this method can work for every OUTER/UNKNOWN API in J2ME.

Monday, May 17, 2010

J2ME | determine BlackBerry device (OS)

Here is a code for determine whether a J2ME application is running over Blackberry device.

The idea is to check for a class that exists only in Blackberry platform and not on J2ME MIDP Operation system.
here I choose to determine using "net.rim.blackberry.api.browser.Browser" which exists only in Blackberry OS (NOT in other OSs).

The code using Class.forClass method (link) which returns the Class object associated with the class or interface with the given string name. if the class exists (so we in Blackberry) the method return an object, otherwise throws an exception.

here is a simple code:
static public boolean isBlackberryPlatform()
{
        try
        {
            Class.forName("net.rim.blackberry.api.browser.Browser");
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
}

enjoy, and post replies...

J2ME | identify MIDP runner over Android OS

Here is a good code for determine whether a J2ME application is running over MIDP runner over Android OS.

The idea is to check for a class that exists only in Android platform and not on J2ME MIDP Operaion system.
here I choose to determine using "android.Manifest" which exists in Android OS and NOT in other OSs.

The code using Class.forClass method (link) which returns the Class object associated with the class or interface with the given string name. if the class exists (so we in Android) the method return an object, otherwise throws an exception.

here is a simple code:
static public boolean isAndroidPlatform()
{
        try
        {
            Class.forName("android.Manifest");
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
}

enjoy, and post replies...