This time I would like to use the Wii remote controller in Java.
There are few Japanese documents and they are old, so I hope you can use them as new documents.
It will not work unless you pair the Wii remote with your PC as a Bluetooth device. Be sure to pair before executing.
Windows 10 Home Edition Eclipse 4.6 Java 1.8
We will proceed as a normal project, not as Maven.
WiiuseJ Wiiusej 0.12a.zip at https://code.google.com/archive/p/wiiusej/downloads
Put wiiuse.dll and WiiUseJ.dll for Windows and libwiiuse.so and libWiiuseJ.so for Mac users in the resource folder.
If a Wii remote is found, the wiimote variable will contain the Wii remote object.
example.java
import com.github.awvalenti.wiiusej.WiiusejNativeLibraryLoadingException;
import wiiusej.WiiUseApiManager;
import wiiusej.Wiimote;
public class MainAccessPoint {
private Wiimote wiimote;
private Wiimote[] wiimotes;
public MainAccessPoint() throws InterruptedException {
try {
System.out.println("Finding wiimotes...");
while(wiimotes == null) {
/*2019/8/5 There was a mistake. getWiimotes(1);Not getWiimotes(1, true);was. Also, WiiUseApiManager was statically accessed.*/
wiimotes = WiiUseApiManager.getWiimotes(1, true);
if(wiimotes != null && wiimotes.length > 0) {
wiimote = wiimotes[0];
System.out.println("Wii remote control found. Id: " + wiimote.getId());
break;
}
wiimotes = null;
continue;
}
} catch (WiiusejNativeLibraryLoadingException e) {
e.printStackTrace();
}
}
public static void main(String args[]) throws InterruptedException {
MainAccessPoint main = new MainAccessPoint();
}
}
Specify boolean in order from the left from the first argument.
python
wiimote.setLeds(true, false, false, false);
Register the following listeners in wiimote.
Listener.java
import wiiusej.values.GForce;
import wiiusej.wiiusejevents.physicalevents.ExpansionEvent;
import wiiusej.wiiusejevents.physicalevents.IREvent;
import wiiusej.wiiusejevents.physicalevents.MotionSensingEvent;
import wiiusej.wiiusejevents.physicalevents.WiimoteButtonsEvent;
import wiiusej.wiiusejevents.utils.WiimoteListener;
import wiiusej.wiiusejevents.wiiuseapievents.ClassicControllerInsertedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.ClassicControllerRemovedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.DisconnectionEvent;
import wiiusej.wiiusejevents.wiiuseapievents.GuitarHeroInsertedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.GuitarHeroRemovedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.NunchukInsertedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.NunchukRemovedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.StatusEvent;
public class Listener implements WiimoteListener {
public Listener() {
}
//Ignite when the button is pressed. You can tell which button it is by the method name.
@Override
public void onButtonsEvent(WiimoteButtonsEvent arg0) {
if(arg0.isButtonHomeJustPressed()) {
} else if(arg0.isButtonHomeJustReleased()) {
} else if(arg0.isButtonPlusJustPressed()) {
} else if(arg0.isButtonPlusJustReleased()) {
} else if(arg0.isButtonMinusJustPressed()) {
} else if(arg0.isButtonMinusJustReleased()) {
} else if(arg0.isButtonOneJustPressed()) {
} else if(arg0.isButtonOneJustReleased()) {
} else if(arg0.isButtonTwoJustPressed()) {
} else if(arg0.isButtonTwoJustReleased()) {
} else if(arg0.isButtonAJustPressed()) {
} else if(arg0.isButtonAJustReleased()) {
} else if(arg0.isButtonBJustPressed()) {
} else if(arg0.isButtonBJustReleased()) {
} else if(arg0.isButtonRightJustPressed()) {
} else if(arg0.isButtonRightJustReleased()) {
} else if(arg0.isButtonLeftJustPressed()) {
} else if(arg0.isButtonLeftJustReleased()) {
} else if(arg0.isButtonUpJustPressed()) {
} else if(arg0.isButtonUpJustReleased()) {
} else if(arg0.isButtonDownJustPressed()) {
} else if(arg0.isButtonDownJustReleased()) {
}
}
//Ignite when the Wii remote controller moves.
@Override
public void onMotionSensingEvent(MotionSensingEvent arg0) {
//The value of the 3-axis accelerometer is 1 to-It will be returned in the range of 1.
GForce force = arg0.getGforce();
System.out.println("X: " + force.getX());
System.out.println("Y: " + force.getY());
System.out.println("Z: " + force.getZ());
}
@Override public void onStatusEvent(StatusEvent arg0) {}
@Override public void onDisconnectionEvent(DisconnectionEvent arg0) {}
@Override public void onClassicControllerInsertedEvent(ClassicControllerInsertedEvent arg0) {}
@Override public void onClassicControllerRemovedEvent(ClassicControllerRemovedEvent arg0) {}
@Override public void onExpansionEvent(ExpansionEvent arg0) {}
@Override public void onGuitarHeroInsertedEvent(GuitarHeroInsertedEvent arg0) {}
@Override public void onGuitarHeroRemovedEvent(GuitarHeroRemovedEvent arg0) {}
@Override public void onIrEvent(IREvent arg0) {}
@Override public void onNunchukInsertedEvent(NunchukInsertedEvent arg0) {}
@Override public void onNunchukRemovedEvent(NunchukRemovedEvent arg0) {}
}
wiimote.addWiiMoteEventListeners(new Listener());
You need to write the following code to get the motion.
wiimote.activateMotionSensing();
I will add it when I find out something. If you have any questions, please leave a comment.
Recommended Posts