JSR 256 を使用して、電源コードがコンセントから抜かれていることを検出するにはどうすればよいですか?
2 に答える
JSR の仕様をざっと見てみると、次のようになります。
(仕様自体の付録 D、最新の JavaME SDK、Sony Ericsson 開発者 Web サイト、そして google から始まるコード例を探すことができます)
いつものように、JSR の多様な実装における断片化が心配ですが、最初のアイデアは次のとおりです。
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.
}
}
javax.microedition.io.Connector.sensor
MIDlet パーミッションに追加する必要があります。
- - - -編集 - - -
Sony-Ericsson Satio phone (S60 第 5 版) での JSR-256 実装のドキュメント:
バッテリ充電センサーには、次の特性があります。
数量: battery_charge
コンテキスト タイプ: デバイス
URL: sensor:battery_charge;contextType=device;model=SonyEricsson
チャネル: (インデックス: 名前、範囲、単位)
0: battery_charge、0 ~ 100、パーセント
1: 充電器の状態、0 ~ 1、ブール値
プロジェクト プロパティのアプリケーション記述子の API 権限タブに javax.microedition.io.Connector.sensor を追加します。