You are viewing outdated content for BUG. If you have a BUG Y.T. edition or 2.0 series device, please visit our updated wiki: http://wiki.buglabs.net



Create a basic application with the Motion Sensor module

From BUG Wiki

Jump to: navigation, search

Create a basic application for BUG that displays a message on the system output console.

  1. If necessary, switch Perspective to the Dragonfly perspective.
  2. Select the New BUG Project icon in the toolbar.
  3. In the New BUG Project window, in the Name field, enter: MotionSensorExample
  4. Click Next.
  5. Click Start Virtual BUG
  6. Once the Virtual Bug has started, right click on one of the slots and select Motion
  7. Under Target BUG select Virtual BUG
  8. Under Required Services begin typing IMotionSubject until it filters out everything but com.buglabs.bug.module.motion.pub.IMotionSubject, and check the box next to that that service
  9. Click Finish
  10. Right click on the MotionSensorExample project in the package explorer, and select New->Other->Class
  11. Click Next.
  12. Under package enter motionsensorexample.watcher
  13. Under name enter MotionSensorWatcher
  14. Click Add next to Interfaces
  15. Type IMotionObserver and then click Ok
  16. Click Finish
  17. In left-side Project Explorer view, expand the node for the MotionSensorExample application
  18. Expand the node for the motionsensorexample.servicetracker package
  19. Open the MotionSensorExampleServiceTracker.java file
  20. Add the following code to the import block at the top of the file
import com.buglabs.bug.module.motion.pub.IMotionSubject;
import motionsensorexample.watcher.*;
  1. Near the top of the MotionSensorExampleServiceTracker class add the variable
private IMotionSubject motion;
  1. Under the doStart() method of MotionSensorExampleServiceTracker add the following code
motion = (IMotionSubject) getService(IMotionSubject.class);
MotionSensorWatcher watcher = new MotionSensorWatcher(motion);
motion.register(watcher);
  1. Open the MotionSensorWatcher.java file in the motionsensorexample.watcher package
  1. Add the following code to the import block
import com.buglabs.bug.module.motion.pub.IMotionSubject;
  1. Replace the class's code with the following
private IMotionSubject motion;

public MotionSensorWatcher (IMotionSubject motion) {
  this.motion = motion;
}
public void motionDetected() {
   System.out.println("Motion has been detected");
}
  1. Go to File > Save all
  1. Click Run As and choose Virtual BUG
  1. Right click on a slot on the virtual bug and choose the motion module
  1. Type motion in the console of the Dragonfly SDK after the last line of output
  1. The module will run the motionDetected() method, printing "Motion has been detected" in the console of the Dragonfly SDK
  1. There is a working version of this program located here: http://www.buglabs.net/applications/kschultz/MotionSensorExample