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.

3 comments:

  1. was really very helpful to me. Thanks.

    ReplyDelete
  2. Can you please upload of the BlackberryInfo.jar file
    i am not able to find it

    ReplyDelete