有没有人遇到过使用 j2me 连接蓝牙服务的问题?

发布于 2024-10-29 01:40:23 字数 2108 浏览 1 评论 0原文

我一直在尝试使用 j2me 连接到蓝牙服务,但出现空异常...

客户端能够识别附近区域的设备,但无法连接到其服务。我正在粘贴我的代码,如果有人能告诉我原因以及代码是否正确,我将非常感激。

//服务器

public  void startServer() {

    System.out.println("server is running...");
            UUID uuid = new UUID("1101", true);
            String connectionString = "btspp://localhost:" + uuid +";name=Server";
    try {
        // create a server connection
        StreamConnectionNotifier notifier =(StreamConnectionNotifier) Connector.open(connectionString);
        // accept client connections
        StreamConnection connection = notifier.acceptAndOpen();

                LocalDevice localDevice = LocalDevice.getLocalDevice();
                stringItem.setText(localDevice.getFriendlyName()+" : "+localDevice.getBluetoothAddress());
                System.out.println("Address: "+localDevice.getBluetoothAddress());

                System.out.println("Name: "+localDevice.getFriendlyName());

        // prepare to send/receive data
        byte buffer[] = new byte[100];
        String msg = "hello there, client";
        InputStream is = connection.openInputStream();
        OutputStream os = connection.openOutputStream();
        // send data to the client
        os.write(msg.getBytes());
        // read data from client
        is.read(buffer);
                int len = is.available();

                stringItem.setText(Integer.toString(len));
        connection.close();
    } catch(IOException e) {
      e.printStackTrace();
    }

}

//客户端

private void deServiceSearch(RemoteDevice device) {
    //int[] attr={10203040607040A1B1C1DE100};
System.out.println("TEst4..");
    setError("2");
    UUID[] uuids = new UUID[1];
    uuids[0] = new UUID(1101);


    try {

        System.out.println("TEst6..");

        UUID[] uuidSet = new UUID[1];

        uuidSet[0]=new UUID("1101",false);

        System.out.println("\nSearching for service...");
        setError("3");
        agent.searchServices(null,uuidSet,device,this);

    } catch (BluetoothStateException ex) {
        setError("4");
    }
}

I've been trying to connect to a bluetooth service using j2me but I am getting a null exception...

The client is able to identify devices in the nearby regions but not being able to connect to their service. I am pasting my codes and would be highly obliged if anyone could tell me the reason and if the codes are correct.

//Server

public  void startServer() {

    System.out.println("server is running...");
            UUID uuid = new UUID("1101", true);
            String connectionString = "btspp://localhost:" + uuid +";name=Server";
    try {
        // create a server connection
        StreamConnectionNotifier notifier =(StreamConnectionNotifier) Connector.open(connectionString);
        // accept client connections
        StreamConnection connection = notifier.acceptAndOpen();

                LocalDevice localDevice = LocalDevice.getLocalDevice();
                stringItem.setText(localDevice.getFriendlyName()+" : "+localDevice.getBluetoothAddress());
                System.out.println("Address: "+localDevice.getBluetoothAddress());

                System.out.println("Name: "+localDevice.getFriendlyName());

        // prepare to send/receive data
        byte buffer[] = new byte[100];
        String msg = "hello there, client";
        InputStream is = connection.openInputStream();
        OutputStream os = connection.openOutputStream();
        // send data to the client
        os.write(msg.getBytes());
        // read data from client
        is.read(buffer);
                int len = is.available();

                stringItem.setText(Integer.toString(len));
        connection.close();
    } catch(IOException e) {
      e.printStackTrace();
    }

}

//Client

private void deServiceSearch(RemoteDevice device) {
    //int[] attr={10203040607040A1B1C1DE100};
System.out.println("TEst4..");
    setError("2");
    UUID[] uuids = new UUID[1];
    uuids[0] = new UUID(1101);


    try {

        System.out.println("TEst6..");

        UUID[] uuidSet = new UUID[1];

        uuidSet[0]=new UUID("1101",false);

        System.out.println("\nSearching for service...");
        setError("3");
        agent.searchServices(null,uuidSet,device,this);

    } catch (BluetoothStateException ex) {
        setError("4");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

国产ˉ祖宗 2024-11-05 01:40:23

您在要获取的服务的属性中指定 null。请设置属性,就像我在下面所做的那样。

//搜索服务

public void doServiceSearch(RemoteDevice device){  
    //0x100 - Service name attribute  
    //0x101 - Service Description attribute  
    //0x102 - Provider Name attribute  
    //0x1002 - return all the services that are public browseable  
    int[] attributes = {0x100,0x101,0x102};  
    UUID[] uuids = new UUID[1];  
    uuids[0] = new UUID(0x1002);  
    try{  
        agent.searchServices(attributes, uuids, device, this);  
    }catch(BluetoothStateException bse){  
        bse.printStackTrace();  
    }  
}

you are specifying null in the attributes of the services which you want to fetch. Please set the attributes, as I 've done below.

//Searching for Services

public void doServiceSearch(RemoteDevice device){  
    //0x100 - Service name attribute  
    //0x101 - Service Description attribute  
    //0x102 - Provider Name attribute  
    //0x1002 - return all the services that are public browseable  
    int[] attributes = {0x100,0x101,0x102};  
    UUID[] uuids = new UUID[1];  
    uuids[0] = new UUID(0x1002);  
    try{  
        agent.searchServices(attributes, uuids, device, this);  
    }catch(BluetoothStateException bse){  
        bse.printStackTrace();  
    }  
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文