JSR82是J2ME的可选包,提供了支持蓝牙通讯的API
下边的代码打印移动设备的蓝牙地址和友好名
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.bluetooth.*;
public class BluetoothMidlet extends MIDlet implements CommandListener {
private Display display;
private Command exit;
LocalDevice local;
Alert a;
public BluetoothMidlet() {
display = Display.getDisplay(this);
exit = new Command("Exit",Command.EXIT, 0);
a = new Alert("Local Device");
a.addCommand(exit);
a.setCommandListener(this);
}
public void startApp() {
try {
// 获得本地兰牙设备对象
local = LocalDevice.getLocalDevice();
// 获得本地设备的兰牙地址
String address = local.getBluetoothAddress();
// 获得本地兰牙设备的友好名
String name = local.getFriendlyName();
System.out.println(address + name);
a.setString("Address is"+address+" Name is "+name);
display.setCurrent(a);
} catch(Exception e) {
e.printStackTrace();
}
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
public void commandAction(Command c,Displayable d) {
if(c == exit) {
notifyDestroyed();
destroyApp(true);
}
}
}
本文介绍JSR82可选包中用于蓝牙通信的API,并通过一个具体示例展示如何使用这些API来获取移动设备的蓝牙地址和友好名称。
2129





