Examples

The motej-demos package contains sample code, that should help you in adopting motej for your own projects.

AdvancedDiscovery Example

To discover and connect a Wiimote, implement a MoteFinderListener, add it to the MoteFinder and start the Discovery, as shown below.

        MoteFinderListener listener = new MoteFinderListener() {
                
                public void moteFound(Mote mote) {
                        System.out.println("Found mote: " + mote.getBluetoothAddress());
                        mote.rumble(2000l);
                        motes.add(mote);
                }
                
        };
                
        MoteFinder finder = MoteFinder.getMoteFinder();
        finder.addMoteFinderListener(listener);
                
        finder.startDiscovery();
        Thread.sleep(30000l);
        finder.stopDiscovery();

Source Code

ButtonsDemo Example

To react on button events propagated by motej, simply attach a CoreButtonListener instance to your Mote object.

        mote.addCoreButtonListener(new CoreButtonListener() {
                
                public void buttonPressed(CoreButtonEvent evt) {
                        if (evt.isButtonAPressed()) {
                                System.out.println("Button A pressed!");
                        }
                        if (evt.isButtonBPressed()) {
                                System.out.println("Button B pressed!");
                        }
                        if (evt.isNoButtonPressed()) {
                                System.out.println("No button pressed.");
                        }
                }
                
        });

Source Code

AccelerometerDemo Example

In order to intercept Accelerometer Events, just add a AccelerometerListener to your Mote instance and set a report mode that contains accelerometer data.

        AccelerometerListener listener = new AccelerometerListener() {
                
                public void accelerometerChanged(AccelerometerEvent evt) {
                        System.out.println(evt.getX() + " : " + evt.getY() + " : " + evt.getZ());
                }
                
        };
                
        Mote mote = MoteFinder.getMoteFinder().findMote();
        mote.addAccelerometerListener(listener);
        mote.setReportMode(ReportModeRequest.DATA_REPORT_0x31);
                
        Thread.sleep(60000l);
                
        mote.setReportMode(ReportModeRequest.DATA_REPORT_0x30);
        mote.disconnect();

I do reset the report mode to 'Core Button data only' before disconnecting the Wiimote, as I've found my Bluetooth stack crashing sometimes if I didn't.

Source Code

IrCameraDemo Example

Using IrCamera events is quite similar to accessing the accelerometer data. Just add the IrCameraListener to your Mote instance.

        IrCameraListener listener = new IrCameraListener() {
                
                public void irImageChanged(IrCameraEvent evt) {
                        System.out.println(evt.getX() + " : " + evt.getY());
                }
                
        };
                
        Mote mote = MoteFinder.getMoteFinder().findMote();
        mote.addIrCameraListener(listener);
        mote.enableIrCamera();
        mote.setReportMode(ReportModeRequest.DATA_REPORT_0x36);
                
        Thread.sleep(60000l);
                
        mote.setReportMode(ReportModeRequest.DATA_REPORT_0x30);
        mote.disableIrCamera();
        mote.disconnect();

Source Code