用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - blackberry代码库

BlackBerry 蓝牙编程 - 搜索蓝牙设备

2012-09-14 作者: 神马举报

[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) {
            }
        }
    }
}


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...