Useful Java ME 8 Embedded code snippets

Collection of ME 8 Embedded code snippets:

  • Using java.util.TimerTask and Timer – for scheduling periodic tasks (reading a sensor, checking a status etc eg
MyTimerTask myTimerTask = new MyTimerTask();
Timer timer = new Timer();
timer.schedule(myTimerTask, millisBeforeStarting, millisBetweenExecution);
  • Opening a GPIO pin: =;; // true = on, false = off
  • Adding an EventListener to a switch:
GPIOPinConfig config = new GPIOPinConfig(this.portId, this.pinId,
    GPIOPinConfig.DIR_INPUT_ONLY, PeripheralConfig.DEFAULT,

… where this implements PinListener and valueChanged() method:

public void valueChanged(PinEvent event) {
    GPIOPin eventPin = event.getPeripheral();
  • Opening an I2C device for input/output:
I2CDeviceConfig config = new I2CDeviceConfig(i2cBus, address, addressSizeBits, serialClock);
myDevice =;
  • Read from a UART device:
this.uart =;

InputStream is = Channels.newInputStream(uart);
this.serialBufferedReader = new BufferedReader(new InputStreamReader(is));
  • Read using Generic Connection Framework:
CommConnection conn = (CommConnection)"comm:/dev/ttyAMA0;baudrate=9600");
InputStream inputStream = conn.openInputStream();
this.serialBufferedReader = new BufferedReader(new
  • Open and write to a record store: = RecordStore.openRecordStore(storeName, true);
byte[] dataBytes = data.getBytes();
recordNum = store.addRecord(dataBytes, 0, dataBytes.length);

Resizing your Debian root fs on Raspberry Pi

Once you’ve copied one of the Debian disk images to an SD card, you may have noticed that the disk image is only for 2GB and you have a lot of unused space on your SD card. The raspi-config util has an option (Expand Root FS) to resize your root partition for you. There’s also plenty of other utils to do the job. I used gparted from a gparted LiveCD booted on my Mac to resize my partition and it worked well.

Configuring a static IP on Debian Wheezy for Raspberry Pi

$ sudo nano /etc/network/interfaces


iface eth0 inet dhcp


iface eth0 inet static

Below this line enter the following, replacing x where necessary for your network config.

address 192.168.x.x
network 192.168.x.0
broadcast 192.168.x.255
gateway 192.168.x.x