Thursday, January 27, 2011

Blackberry Installation with sample code


System requirements

Item
Requirement
development environment
One of the following development environments:
  • BlackBerry® Java® Plug-in for Eclipse® 1.1 or later with BlackBerry® Java® SDK 6.0
  • BlackBerry® Java® Development Environment 6.0
  • another third-party development environment, such as NetBeans™, that is running BlackBerry Java SDK 6.0

BlackBerry Smartphone Simulator or BlackBerry device
Any BlackBerry Smartphone Simulator or device that is running BlackBerry® Device Software 6.0 or later

Technical requirements

  • Eclipse 3.5 classic
  • 32-bit Windows® XP, Windows Vista™ or Windows 7 (Note: 64-bit versions require 32-bit Java and Eclipse)
  • Monitor with a resolution of 1024 x 768 or higher
  • PC with Intel® Pentium® 4 processor or compatible (2.5 GHz or higher, 2 GB RAM, 1.5 GB HD free)
  • Java® SE Development Kit (JDK) 6, update 10 or later.
BlackBerry Java Plug-in for Eclipse Update Site

Using the BlackBerry® Java® Plug-in for Eclipse® update site, you can download and install Eclipse® update components directly into an Eclipse install.
Follow the installation instructions below to learn how to download the BlackBerry® Java® SDK v6.0

Installation instructions

If you’re using Java® 2 SDK, Standard Edition v6.0, obtain Update 16 of the Java 2 SDK v6.0 from the Sun Microsystems website before downloading the plug-in using the Eclipse Update Mechanism.
  1. From the Help menu, select Install New Software to open the Install window.
  2. Click the Add button:
    1. In the Add Site dialog, type the URL http://www.blackberry.com/go/eclipseUpdate/3.5/java into the location text box and specify BlackBerry Java Plug-in Update Site in the name text box.
    2. Click the OK button, BlackBerry Update Site appears in the Available Software list.
  3. Select the BlackBerry Java Plug-in item and at least one BlackBerry Component Pack item you want to work on.
  4. Click the Next button.
  5. Click the Next button after reviewing the items to be installed (if the BlackBerry Java Plug-in for Eclipse was selected the first time, the BlackBerry Java SDK 5.0.0 is automatically selected if no other SDK was selected).
  6. Check the I accept the terms of the license agreement radio button after reviewing licenses.
  7. Click the Finish button to begin installation.
  8. Note: you'll need to enter your BlackBerry® Developer Zone login ID and password in an authentication dialog. Due to security policy, you may be authenticated multiple times. If you don’t have a BlackBerry Developer Zone login, register for access to the BlackBerry Developer Zone.
  9. After successfully downloading the files, you’ll be prompted to restart the Eclipse Platform. Choose to restart the platform.
Development Tools & Downloads

  • Web development
  • Themes and animated graphics
  • Java® development
Simulators
  • BlackBerry smartphone simulators
  • Download the BlackBerry email and MDS services simulator package v4.1.4


Coding :

import java.util.*;

import net.rim.device.api.system.*;

import net.rim.device.api.system.AccelerometerSensor.*;

import net.rim.device.api.ui.*;

import net.rim.device.api.ui.container.*;

import net.rim.device.api.ui.component.*;

 

/**

 * This sample demonstrates the Accelerometer API.  The DrawThread opens the

 * accelerometer channel and periodically queries for the current data reading.

 * The data is then used to apply corresponding force to a ball drawn on the

 * screen.

 */

public final class AccelerometerDemo extends UiApplication

{

    private AccelerometerDemoScreen _screen;

    private DrawThread _thread;

 

    private Bitmap _ball;

    private int _ballWidth;

    private int _ballHeight;  

   

    private int _x;

    private int _y;

    private float _xSpeed;

    private float _ySpeed;

 

    private Random _r;

 

    private boolean _simulated;  

    private Channel _accChannel;

    private short[] _xyz = new short[ 3 ];     

   

    private static final float G_NORM = 9.8066f / AccelerometerSensor.G_FORCE_VALUE;   

    private static final float TABLE_FRICTION = 0.98f;

    private static final float BOUNCE_SLOWDOWN = 0.6f;

 

    private static final int DEFAULT_ORIENTATION = Display.DIRECTION_NORTH;  

 

    private int _tick = 0; 

 

    /**

     * Entry point for application.

     * @param args Command line arguments (not used)

     */

    public static void main( String[] args )

    {

        // Create a new instance of the application and make the currently

        // running thread the application's event dispatch thread.

        AccelerometerDemo app = new AccelerometerDemo();

        app.enterEventDispatcher();

    }

 

    // Constructor

    public AccelerometerDemo()

    {

        if( AccelerometerSensor.isSupported() )

        {

            // Initialize UI

            _screen = new AccelerometerDemoScreen();

            _screen.addMenuItem(_startMenuItem);

            _screen.addMenuItem(_stopMenuItem);

            pushScreen( _screen );

           

            _ball = Bitmap.getBitmapResource( "img/ball.png" );

            _r = new Random();

           

            if( _ball != null ) {

                _ballWidth = _ball.getWidth();

                _ballHeight = _ball.getHeight();

            }

           

            // Prevent UI from rotating our screen.

            Ui.getUiEngineInstance().setAcceptableDirections( DEFAULT_ORIENTATION );

        }

        else

        {

            UiApplication.getUiApplication().invokeLater(new Runnable()

            {

                public void run()

                {

                    Dialog.alert("This device does not support accelerometer.");

                    System.exit(0);

                }

            });

        }

    } 

   

    /**

     * Calculates ball position.

     * @param xAcc x axis acceleration

     * @param yAcc y axis acceleration

     */

    private void applyForce( int xAcc, int yAcc )

    {

        // Calculate new speed.

        _xSpeed += xAcc * G_NORM;

        _ySpeed += yAcc * G_NORM;

       

        // Apply table friction.

        _xSpeed *= TABLE_FRICTION;

        _ySpeed *= TABLE_FRICTION;

       

        // Move the ball.

        _x += _xSpeed;

        _y += _ySpeed;

       

        if( _x < 0 )

        {

            _x = 0;

            _xSpeed = -( _xSpeed * BOUNCE_SLOWDOWN );

        }

        else {

            int screenWidth = _screen.getWidth();

            if( _x > screenWidth - _ballWidth )

            {

                _x = screenWidth - _ballHeight;

                _xSpeed = -( _xSpeed * BOUNCE_SLOWDOWN );

            }

        }

 

        if( _y < 0 )

        {

            _y = 0;

            _ySpeed = -( _ySpeed * BOUNCE_SLOWDOWN );

        }

        else {

            int screenHeight = _screen.getHeight();

            if( _y > screenHeight - _ballHeight )

            {

                _y = screenHeight - _ballHeight;

                _ySpeed = -( _ySpeed * BOUNCE_SLOWDOWN );

            }

        }

    }

 

    /**

     * A thread class to handle screen updates.

     */

    private class DrawThread extends Thread

    {

        private boolean _running;          

       

        public void run()

        {

            _running = true;

 

            // Start querying the accelerometer sensor.

            openAccelerometerConnection();

 

            while( _running )

            {

                _tick++;

 

                // Get current acceleration.

                readAcceleration();

 

                // Apply force to the ball.

                applyForce( -_xyz[ 0 ], _xyz[ 1 ] );

              

                try

                {

                    synchronized( this )

                    {

                        wait( 50 );

                    }

                }

                catch( InterruptedException e )

                {

                    UiApplication.getUiApplication().invokeLater(new Runnable()

                    {

                        public void run()

                        {

                            Dialog.alert("wait(long) threw InterruptedException");

                        }               

                    });

                }

               

                if( !_running )

                {

                    break;

                }

 

                _screen.invalidate();

            }

 

            // Stop querying the sensor to save battery charge.

            closeAccelerometerConnection();

        }

    }

 

    /**

     * Opens the data channel.

     */

    private void openAccelerometerConnection()

    {

        if( DeviceInfo.isSimulator() )

        {

            _simulated = true;

        }

        else

        {

            _accChannel = AccelerometerSensor.openRawDataChannel( AccelerometerDemo.this );

            _simulated = false;

        }

    }

 

    /**

     * Gets the latest acceleromenter data.

     */

    private void readAcceleration()

    {

        if( _simulated )

        {

            // Running in a simulator, simulate random.

            if(_tick % 10 == 0)

            {

                _xyz[0] = (short) ( _r.nextInt( 400 ) - 200 );

                _xyz[1] = (short) ( _r.nextInt( 400 ) - 200 );

            }

        }

        else

        {

            // Real device, call the API for samples.

            _accChannel.getLastAccelerationData( _xyz );

        }

    }

 

    /**

     * Closes the data channel.

     */

    private void closeAccelerometerConnection()

    {

        if( _accChannel != null )

        {

            _accChannel.close();

            _accChannel = null;

        }

    }

   

    /**

     * Menu item to start the ball moving.

     */

    private MenuItem _startMenuItem = new MenuItem("Start" , 0, 0)

    {

        public void run()

        {

            if(_thread == null)

            {

                // Start drawing

                _thread = new DrawThread();

                _thread.start();

            }

                       

        }

    };

   

    /**

     * Menu item to stop the ball moving.

     */

    private MenuItem _stopMenuItem = new MenuItem("Stop" , 0, 0)

    {

        public void run()

        {

            if( _thread!= null )

            {

                synchronized( _thread )

                {

                    _thread._running = false;

                    _thread.notifyAll();

                    _thread = null;

                }

            }           

        }

    };

 

   /**

    * A screen on which to display the ball.

    */

    private class AccelerometerDemoScreen extends MainScreen

    {

        /**

         * @see Screen#paint(Graphics)

         */

        protected void paint( Graphics graphics )

        {

            if( _ball != null )

            {

                graphics.drawBitmap( _x, _y, _ballWidth, _ballHeight, _ball, 0, 0 );

            }

        }

           

        /**

         * @see Screen#onClose()

         */           

        public boolean onClose()

        {

            if( _thread != null )

            {

                synchronized( _thread )

                {

                    _thread._running = false;

                    _thread.notifyAll();

                }

                Thread.yield();

            }

            return super.onClose();

        }

    }

}

Install the sample application

·  On the taskbar, click Start > Programs > Research In Motion > BlackBerry JDE 4.7.0 > JDE.
·  Open the workspace that you want to add the sample application to.
·  In the workspace tree, right-click the project that you want to add the sample application to.
·  Click Add project to <workspace directory>.
·  In the Add project to <workspace> dialog box, browse to the folder where you extracted the sample application.
·  Click the AcceleratometerDemo.jdp file.
·  Click Open

Run the sample application

  1. In the workspace where you added the accelerometerdemo project, right-click accelerometerdemo.
  2. Click Build project.
  3. On the taskbar, click Start > Applications > Research in Motion > BlackBerry JDE 4.7.0 > Device Simulator.
  4. On the Home screen of the BlackBerry® Smartphone Simulator, click the Downloads folder.
  5. Click the accelerometerdemo icon.
  6. In the accelerometer sample application, press the Menu key.
     7.  Click Start. 

No comments:

Post a Comment