简体   繁体   English

远程设备的Java蓝牙列表

[英]Java BlueTooth list of remote devices

我在网上搜索了所有内容,但找不到这个简单问题的答案:如何在j2me中显示附近所有蓝牙设备的列表?

You can only use handsets that have JSR82 to do this. 您只能使用具有JSR82的手机来执行此操作。

This site gives a full example. 该站点提供了完整的示例。

First, you need to choose a bluetooth stack. 首先,您需要选择一个蓝牙堆栈。

You have a choice of BlueCove or Avetana (these too I'm aware of). 您可以选择BlueCoveAvetana (我也知道)。

Then search their documentation for "device discovery". 然后在其文档中搜索“设备发现”。

First, bear in mind that in the emulator you can't detect 'real' Bluetooth devices without a third-party JSR-82 library such as Bluecove (it's a desktop implementation for Java Bluetooth). 首先,请记住,在仿真器中,如果没有第三方JSR-82库(例如Bluecove) (它是Java蓝牙的桌面实现),您将无法检测“真正的”蓝牙设备。 But when you deploy to a JSR-82 capable phone, it should work. 但是,当您部署到支持JSR-82的电话时,它应该可以工作。

But otherwise, it's easy with JSR-82! 但是否则,使用JSR-82很容易! You can use the DiscoveryAgent class. 您可以使用DiscoveryAgent类。

LocalDevice local = LocalDevice.getLocalDevice();  
DiscoveryAgent agent = local.getDiscoveryAgent();
boolean complete = agent.startInquiry(DiscoveryAgent.GIAC, new DiscoveryListener() {
   public void deviceDiscovered(RemoteDevice device, DeviceClass cod) {
      System.out.println("Discovered: " + device.getFriendlyName());    } 
});
while(!complete) {
    // wait until discovery completes before continuing 
}

Instead of printing the discovered devices as above, you could always put them into the Hashtable or Vector. 不必像上面那样打印发现的设备,您可以始终将它们放入哈希表或向量中。 The deviceDiscovered() method gets called everytime a device is discovered whilst the inquiry is running, and the inquiry usually returns in a decent time (a matter of 10s of seconds). 每当运行查询时发现设备时,都会调用deviceDiscovered()方法,并且查询通常在适当的时间(大约10秒)内返回。

LocalDevice local = LocalDevice.getLocalDevice();
DiscoveryAgent agent = local.getDiscoveryAgent();
// use inquiryStarted to make sure two inquiries aren't running at the same
// time.
inquiryStarted = agent.startInquiry(DiscoveryAgent.GIAC,
        new DiscoveryListener() {
            public void deviceDiscovered(RemoteDevice device,
                    DeviceClass cod) {
                System.out.println("Discovered: "
                        + device.getFriendlyName());
            }
                @Override
            public void inquiryCompleted(int arg0) {
                // TODO Auto-generated method stub

                }

                @Override
                public void serviceSearchCompleted(int arg0, int arg1) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void servicesDiscovered(int arg0,
                        ServiceRecord[] arg1) {
                    // TODO Auto-generated method stub

                }
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM