2

我正在使用 eclipse / WTK 2.5.2 开发一个 J2ME 程序,并且在使用蓝牙连接两个模拟器时遇到问题。有一个服务器和一个 .client 在两个不同的模拟器上运行。

问题是客户端程序无法发现任何蓝牙设备。这是服务器和客户端代码:

public Server()
{
    try
    {
        LocalDevice local = LocalDevice.getLocalDevice();
        local.setDiscoverable(DiscoveryAgent.GIAC);

        server = (StreamConnectionNotifier)   
            Connector.open("btspp://localhost:" 
                + UUID_STRING + ";name=" + SERVICE_NAME);

        Util.Log("EchoServer() Server connector open!");
    }
    catch (Exception e)
    {}
}

调用 Connector.open 后,我在控制台中收到以下警告,我认为这是相关的:

警告:未注册设备:未指定

和搜索设备的客户端代码:

public SearchForDevices(String uuid, String nm)
{
    UUIDStr = uuid;
    srchServiceName = nm;
    try
    {
        LocalDevice local = LocalDevice.getLocalDevice();
        agent = local.getDiscoveryAgent();

        deviceList = new Vector();

        agent.startInquiry(DiscoveryAgent.GIAC, this); // non-blocking
    }
    catch (Exception e)
    {}
}

系统从不调用 deviceDiscovered,而是使用 INQUIRY_COMPLETED 参数调用inquiryCompleted(),所以我想客户端程序运行良好。

在模拟器设置中启用蓝牙..

4

1 回答 1

3

我使用 WTK 2.5.2_01 模拟器从 NetBeans IDE 6.8 测试了几乎相同的代码,它运行良好。(我的意思是它发现了设备)

public void startBTServer() {
    try
    {
        LocalDevice local = LocalDevice.getLocalDevice();
        local.setDiscoverable(DiscoveryAgent.GIAC);

        StreamConnectionNotifier server = (StreamConnectionNotifier)
            Connector.open("btspp://localhost:F0E0D0C0B0A000908070605040302010"
                + ";name=" + ";test");
    }
    catch (Exception e)
    {}
}

public void startBTClient() {
    String UUIDStr = "F0E0D0C0B0A000908070605040302010";
    try
    {
        LocalDevice local = LocalDevice.getLocalDevice();
        DiscoveryAgent agent = local.getDiscoveryAgent();

        agent.startInquiry(DiscoveryAgent.GIAC, (DiscoveryListener) this);
    }
    catch (Exception e)
    {}

}

public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
    System.out.println("device discovered:" + btDevice.toString());
}

此代码打印出以下日志:

从服务器:

Running in the identified_third_party security domain
Device Bluetooth Address: 0000000DECAF

来自客户:

Device Bluetooth Address: 0123456789AF
device discovered:RemoteDevice[address=0000000DECAF, name=null, encrypted=false, authenticated=false]
于 2010-03-16T04:09:57.650 回答