DAAPI B (b02)
- Java ME Embedded 3.3 Release

Package com.oracle.deviceaccess.generic

Interfaces and classes for controlling devices using generic I/O operations.

See: Description

Package com.oracle.deviceaccess.generic Description

Interfaces and classes for controlling devices using generic I/O operations.

The generic device API allows for accessing peripheral devices when there are no more specific standard Java API such as I2CDevice, SPIDevice, GPIOPin or GPIOPort...

This API offers 3 main interfaces:

GenericDevice
Device control operations and event listener registration. A device may implements this sole interface if it does not support any read and write operations.
GenericBufferIODevice
Device control operations and event listener registration as inherited from GenericBufferIODevice and byte buffer read and write operations.
GenericStreamIODevice
Device control operations and event listener registration as inherited from GenericBufferIODevice and stream-based read and write operations.
In order to access a device using its generic interface, an application should first open and obtain a GenericDevice instance for the device using its numerical ID, name, type (interface) and/or properties:
Using its ID
 GenericDevice device = (GenericDevice) PeripheralManager.open(17);
 
Using its name and interface
 GenericStreamIODevice device = (GenericDevice) PeripheralManager.open("STORAGE", GenericStreamIODevice.class, null);
 
Once the peripheral opened, the application can set and get controls, read and write data using methods of the GenericDevice, GenericBufferIODevice and/or GenericStreamIODevice interfaces.
 device.read(buffer, 0, buffer.length);
 
When done, the application should call the GenericDevice.close() method to release Generic device.
 device.close();
 
The following sample codes give examples of using the Generic API to communicate with Real Time Clock device and a microphone (which may be accessible over USB for example):
 public static final int EVT_ALARM = 0;
 public static final int SECONDS = 0;
 public static final int SEC_ALARM = 1;
 public static final int MINUTES = 2;
 public static final int MIN_ALARM = 3;
 public static final int HR_ALARM = 4;
 public static final int HOURS = 5;
 public static final int ALARM_ENABLED = 6;
 private GenericDevice rtc = null;
 
 // Sets the daily alarm for after some delay
 public void setAlarm(byte delaySeconds, byte delayMinutes, byte delayHours) throws IOException, PeripheralException {
     rtc = (GenericDevice) PeripheralManager.open("RTC", GenericDevice.class, (String[]) null);
     byte currentSeconds = ((Byte) rtc.getControl(SECONDS)).byteValue();
     byte currentMinutes = ((Byte) rtc.getControl(MINUTES)).byteValue();
     byte currentHours = ((Byte) rtc.getControl(HOURS)).byteValue();
     byte i = (byte) ((currentSeconds + delaySeconds) % 60);
     byte j = (byte) ((currentSeconds + delaySeconds) / 60);
     rtc.setControl(SEC_ALARM, new Byte(i));
     i = (byte) ((currentMinutes + delayMinutes + j) % 60);
     j = (byte) ((currentMinutes + delayMinutes + j) / 60);
     rtc.setControl(MIN_ALARM, new Byte(i));
     i = (byte) ((currentHours + delayHours + j) % 24);
     rtc.setControl(HR_ALARM, new Byte(i));
 
     rtc.setEventListener(EVT_ALARM, new GenericEventListener() {
         public void eventDispatched(GenericEvent event) {
             GenericDevice rtc = (GenericDevice) event.getPeripheral();
             // Notify application of alarm
         }
     });
     // Enable alarm.
     rtc.setControl(ALARM_ENABLED, Boolean.TRUE);
 }
 
 public void close() {
     try {
         rtc.close();
     } catch (IOException ex) {
     }
 }
 
 public static final int EVT_VOLUME_CHANGED = 0;
 public static final int MIC_VOLUME = 0;
 public static final int MIC_SAMPLE_RATE = 1;
 public static final int MIC_AUTOMATIC_GAIN = 2;
 public static final int MIC_MUTE = 3;
 
 public void audioCapture(byte[] buffer, float sampleRate, boolean agc) throws IOException, PeripheralException {
     GenericBufferIODevice mic = null;
     try {
         mic = (GenericBufferIODevice) PeripheralManager
                 .open("MICROPHONE", GenericBufferIODevice.class, (String[]) null);
         mic.setControl(MIC_SAMPLE_RATE, new Float(sampleRate));
         mic.setControl(MIC_AUTOMATIC_GAIN, agc ? Boolean.TRUE : Boolean.FALSE);
         mic.setControl(MIC_MUTE, Boolean.FALSE);
 
         mic.setEventListener(EVT_VOLUME_CHANGED, new GenericEventListener() {
 
             public void eventDispatched(GenericEvent event) {
                 GenericDevice mic = (GenericDevice) event.getPeripheral();
                 try {
                     float currentVolume = ((Float) mic.getControl(MIC_VOLUME)).floatValue();
                     // ...
                 } catch (IOException ex) {
                     ex.printStackTrace();
                 } catch (PeripheralNotAvailableException ex) {
                     ex.printStackTrace();
                 }
             }
         });
         mic.read(buffer, 0, buffer.length);
     } finally {
         if (mic != null) {
             mic.close();
         }
     }
 }
 

Security

Generic devices are opened by invoking one of the com.oracle.deviceaccess.PeripheralManager.open methods. The "com.oracle.deviceaccess.generic" permission allows access to be granted to generic devices as a whole.
DAAPI B (b02)
5-February-2013 04:40

Copyright (c) 2012, Oracle and/or its affiliates. All Rights Reserved. Use of this specification is subject to license terms.