📄 listallbtdevices.java
字号:
import java.util.Vector;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class ListAllBTDevices extends MIDlet {
private Display display = null;
public Form form = new Form("搜索全部的蓝牙设备");
//搜索时显示搜索到的设备数量
StringItem siMsg = new StringItem("", null);
public ListAllBTDevices() {
super();
// TODO 自动生成构造函数存根
}
protected void startApp() throws MIDletStateChangeException {
display = Display.getDisplay(this);
display.setCurrent(form);
try {
LocalDevice localDevice = LocalDevice.getLocalDevice();
DiscoveryAgent discoveryAgent = localDevice.getDiscoveryAgent();
discoveryAgent.startInquiry(DiscoveryAgent.GIAC,
new MyDiscoveryListener(this));
form.append("开始搜索...\n");
System.out.println("开始搜索...\n");
form.append(siMsg);
} catch (Exception e) {
System.out.println(e);
}
}
protected void pauseApp() {
// TODO 自动生成方法存根
}
protected void destroyApp(boolean arg0)
throws MIDletStateChangeException {
// TODO 自动生成方法存根
}
}
class MyDiscoveryListener implements DiscoveryListener {
//用于保存搜索到的设备
Vector devices = new Vector();
ListAllBTDevices midlet;
public MyDiscoveryListener(ListAllBTDevices midlet) {
this.midlet = midlet;
}
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
devices.addElement(btDevice);
midlet.siMsg.setText("搜索到设备数:" + devices.size()+"\n");
System.out.println("搜索到设备数: " + devices.size()+"\n");
}
public void inquiryCompleted(int discType) {
midlet.form.append("查询完毕\n");
System.out.println("查询完.\n");
showDevices();
}
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
}
public void serviceSearchCompleted(int transID, int responseCode) {
}
void showDevices() {
midlet.form.append("输出搜索结果\n\n");
System.out.println("输出搜索结果\n\n");
for (int i=0;i<devices.size();i++) {
RemoteDevice btDevice = (RemoteDevice)devices.elementAt(i);
try {
String name = btDevice.getFriendlyName(true);
midlet.form.append(" \n发现设备:" + name);
System.out.println(" \n发现设备:" + name);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -