JSR 256 电池事件

发布于 2024-07-22 06:38:28 字数 35 浏览 9 评论 0原文

如何使用 JSR 256 检测电源线何时从电源插座拔出?

How can I detect whenever the power cord is unplugged from electrical socket using JSR 256?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

街角迷惘 2024-07-29 06:38:28

您可以将 javax.microedition.io.Connector.sensor 添加到项目属性的应用程序描述符的 API 权限选项卡中。

You would add javax.microedition.io.Connector.sensor to the API Permissions tab of the Application Descriptor of the project properties.

能怎样 2024-07-29 06:38:28

快速浏览一下 JSR 的规范:(

您可能想要寻找代码示例,从规范本身的附录 D 开始,最新的 JavaME SDK,索尼爱立信开发人员网站,然后谷歌)

一如既往,我会担心关于 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 权限。

-------编辑------

索尼爱立信 Satio 手机(S60 第 5 版)上 JSR-256 实施的文档:

电池电量传感器具有以下特征:

  • 数量:battery_charge

  • 上下文类型:设备

  • URL:sensor:battery_charge;contextType=device;model=SonyEricsson

  • 频道:(索引:名称、范围、单位)

  • 0:电池电量、0-100、百分比

  • 1:charger_state,0-1,布尔值

From a quick look at the specifications of the JSR:

(you might want to look for code examples, starting with Appendix D of the spec itself, the latest JavaME SDK, Sony Ericsson developer website, then google)

As always, I would be worried about fragmentation in the diverse implementations of the JSR, but here's my first idea:

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.

}

}

You'll need to add javax.microedition.io.Connector.sensor to your MIDlet permissions.

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

Documentation from the JSR-256 implementation on Sony-Ericsson Satio phone (S60 5th edition):

The battery charge sensor has the following characteristics:

  • Quantity: battery_charge

  • Context type: device

  • URL: sensor:battery_charge;contextType=device;model=SonyEricsson

  • Channels: (index: name, range, unit)

  • 0: battery_charge, 0-100, percent

  • 1: charger_state, 0-1, boolean

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文