2009-06-04 8 views

Répondre

1

D'un coup d'œil sur les spécifications de la JSR:

(vous pouvez chercher des exemples de code, en commençant par l'annexe D de la spécification elle-même, le dernier JavaME SDK, site du développeur Sony Ericsson, Google)

Comme toujours, je serais inquiet de la fragmentation dans les diverses implémentations de la JSR, mais voici ma première idée:

import javax.microedition.sensor.*; 

SensorInfo[] powerSensorInfoArray = SensorManager.findSensors("power","ambient"); 

//let's assume there is one SensorInfo in the array. 

//open a connection to the sensor. 

SensorConnection connection = (SensorConnection)Connector.open(powerSensorInfoArray[0].getUrl(), Connector.READ); 

// add a DataListener to the connection 

connection.setDataListener(new MyDataListener(), 1); 

// implement the data listener 

public class MyDataListener implements DataListener { 

public void dataReceived(SensorConnection aSensor, Data[] aDataArray, boolean isDataLost) { 

//let's assume there is only one channel for the sensor and no data was lost. 

// figure out what kind of data the channel provides. 

int dataType = aDataArray[0].getChannelInfo().getDataType(); 

//now, I suggest you switch on dataType and print the value on the screen 

// experimentation on the JSR256 implementation you're targetting seems to be 

// the only way to figure out out power data is formatted and what values mean. 

//only one of the following 3 lines will work: 

double[] valueArray = aDataArray[0].getDoubleValues(); 
int[] valueArray = aDataArray[0].getIntValues(); 
Object[] valueArray = aDataArray[0].getObjectValues(); 

// let's assume one value in the valueArray 

String valueToPrint = "" + valueArray[0]; 

// see what happens with that and you plug or unplug the power supply cable. 

} 

} 

Vous devrez ajouter javax.microedition.io.Connector.sensor à vos autorisations de MIDlets.

------- ------ EDIT

Documentation de la mise en œuvre JSR-256 sur le téléphone Sony-Ericsson Satio (5ème édition S60):

Le capteur de charge de la batterie a les caractéristiques suivantes:

  • Quantité: battery_charge

  • type de contexte:

    dispositif
  • URL: capteur: battery_charge; contextType = dispositif; model = SonyEricsson

  • Canaux: (index: nom, gamme, unité)

  • 0: battery_charge, 0-100, pour cent

  • 1: charger_state, 0-1, booléen

+0

merci beaucoup, mec! J'ai été d'une grande aide! Je vais essayer et vous revenir 2. – Attilah

1

Vous ajouteriez javax.microedition.io.Connector.sensor aux autorisations API onglet du descripteur d'application de la projec propriétés t

Questions connexes