[blackberry]代码库
/**
* BlackBerry 蓝牙编程 - 搜索蓝牙设备
*
*/
public class BlueEngine implements Runnable, DiscoveryListener {
private DiscoveryAgent agent = null;
private StreamConnection streamConnection = null;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private Thread thread = null;
private int maxServiceSearches = 0;
private int serviceSearchCount;
private ServiceRecord record = null;
private Vector deviceList;
public String command = null;
public String url = null;
public String macId = null;
private Object bluelock = new Object();
public BlueEngine() {
}
/**
* 初始化蓝牙设备
*
* @throws Exception
*/
public void initBluetooth() throws Exception {
LocalDevice local = LocalDevice.getLocalDevice();// 获取本地设备
macId = local.getBluetoothAddress();// 获取本地蓝牙设备MACID
agent = local.getDiscoveryAgent();// 获取蓝牙设备搜索代理
try { // 获取最大服务搜索数
maxServiceSearches = Integer.parseInt(LocalDevice
.getProperty("bluetooth.sd.trans.max"));
} catch (NumberFormatException e) {
}
deviceList = new Vector(); // 蓝牙设备队列
}
/**
* 获取蓝牙地址
*
* @throws Exception
*/
public void getBluetoothUrl() throws Exception {
record = findService();
url = record.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT,
false);
}
/**
* 搜索蓝牙设备
*
* @return
*/
public ServiceRecord findService() {
try {
agent.startInquiry(DiscoveryAgent.GIAC, this);// 启动搜索代理发现设备 并与
// DiscoveryListener
// 注册回调
// 设备搜索过程是异步的 ,这里需要同步搜索过 程
// 设备搜索完毕会通知主线程继续运行
synchronized (bluelock) {
try {
bluelock.wait();
} catch (Exception e) {
}
}
} catch (BluetoothStateException e) {
System.out.println("Unable to find devices to search");
}
// 蓝牙设备 队列不为 空,启动 服务搜 索
if (deviceList.size() > 0) {
if (searchServices(deviceList)) {
return record;
}
}
return null;
}
/**
* 发现蓝牙设备
*
* @param btDevice
* @param cod
*/
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
// 当发现蓝牙设备时,如 果该设备蓝牙模块的 id 以 00027 开头
// 则收入该设备
if (btDevice.getBluetoothAddress().startsWith("00027")) {
deviceList.addElement(btDevice);
}
}
/**
* 搜索完毕
*
* @param discType
*/
public void inquiryCompleted(int discType) {
// 蓝牙搜索执行完 毕,通知主线程继续
synchronized (bluelock) {
try {
bluelock.notify();
} catch (Exception e) {
}
}
}
}
初级程序员
by: 的没别的 发表于:2013-04-21 17:37:34 顶(1) | 踩(0) 回复
黑莓?
网友回复
回复小蜜锋 : 呵呵~~~
顶(0) 踩(0) 2013-04-22 23:26:25
回复评论