See: Description
| Interface | Description |
|---|---|
| GenericBufferIODevice |
The
GenericBufferIODevice interface defines generic methods for accessing and controlling peripheral devices
using read and write operations. |
| GenericDevice |
The
GenericDevice interface defines methods for setting and getting peripheral device-specific configuration
and access (I/O) controls as well as registering event listeners. |
| GenericEventListener |
The
GenericEventListener interface defines methods for getting notified of events fired by peripheral devices
that implement the GenericDevice interface. |
| GenericStreamIODevice |
The
GenericStreamIODevice interface defines generic methods for accessing and controlling peripheral devices
capable of working with input and output streams. |
| Class | Description |
|---|---|
| GenericEvent |
The
GenericEvent class encapsulates events fired by peripherals that implement the GenericDevice
interface. |
I2CDevice, SPIDevice,
GPIOPin or GPIOPort...
This API offers 3 main interfaces:
GenericDeviceGenericBufferIODeviceGenericBufferIODevice and byte buffer read and write operations.GenericStreamIODeviceGenericBufferIODevice and stream-based read and write operations.GenericDevice instance for the device using its numerical ID, name, type
(interface) and/or properties:
GenericDevice device = (GenericDevice) PeripheralManager.open(17);
GenericStreamIODevice device = (GenericDevice) PeripheralManager.open("STORAGE", GenericStreamIODevice.class, null);
GenericDevice, GenericBufferIODevice
and/or GenericStreamIODevice interfaces. When done, the application should call thedevice.read(buffer, 0, buffer.length);
GenericDevice.close() method to release Generic device.
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):device.close();
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();
}
}
}
com.oracle.deviceaccess.PeripheralManager.open methods. The "com.oracle.deviceaccess.generic" permission
allows access to be granted to generic devices as a whole.Copyright (c) 2012, Oracle and/or its affiliates. All Rights Reserved. Use of this specification is subject to license terms.