如何在j2me蓝牙应用程序中将java对象转换为纯字符串?

发布于 2024-12-11 00:37:09 字数 2102 浏览 0 评论 0原文

我正在制作一个 j2me 蓝牙应用程序。我也是java世界的新手。我必须向用户显示蓝牙服务名称。到目前为止,除了服务名称之外,一切似乎都工作正常。我验证了我的蓝牙服务器是否由其他客户端正确广告服务名称(由 qt 完成)。我尝试如下:-

          public void commandAction(Command command, Item item) {
    if (item == deviceChoiceGroup) {
        if (command == servicesDiscoverCommand) {
            if(deviceList.size()==0) {
                return; 
            }

            UUID[] searchList = new UUID[1];
            searchList[0] = new UUID("11111111111111111111111111111111",false);

            int[] attrSet = new int[1];
            attrSet[0] = 0x100;

            RemoteDevice currentDevice = 
                (RemoteDevice) deviceList.elementAt(
                    getDeviceChoiceGroup().getSelectedIndex());

            if(currentDevice == null) { 
                return; 
            }

            try {
                transactionID = bluetoothDiscoveryAgent.searchServices(
                        new int[] {0x100}, searchList, currentDevice, this);
                printToForm("Start services under L2CAP searching...");
                form.addCommand(cancelServicesDiscoverCommand);
            } catch (BluetoothStateException e) {
                //TODO: write handler code
            }
        }
    }
}


public void servicesDiscovered(int transID, ServiceRecord[] serviceRecords){
    if (serviceRecords.length>0 && serviceRecords!=null)
    {
        connectionURL=serviceRecords[0].getConnectionURL(0, false);

        int[] ids=serviceRecords[0].getAttributeIDs();  
        DataElement ServiceName=serviceRecords[0].getAttributeValue(ids[1]);           
        // tried to convert objedct to string. 
        String str = (ServiceName.getValue()).toString();
        // out is put is like java.util.vector$1@3c60cd14c
        printToForm("#Service name: "+str); 

        printToForm("The Service name is: "+ServiceName.getValue());

    }
}

“DataElement.getValue()”返回对象。因此我可以看到服务名称为“java.util.vector$1@3c60cd14c”。我尝试将对象转换为字符串“String str = (ServiceName.getValue()).toString();”它无法正确转换。

那么如何将对象转换为字符串。这样我就可以看到纯文本的服务名称。谢谢!

I'm making a j2me Bluetooth application. I'm also new in java world. Where I have to display a Bluetooth service name to the user. So far it seems all is working correctly except service name. I verified my Bluetooth server is advertising service name correctly by other client (done by qt). I tried as follows:-

          public void commandAction(Command command, Item item) {
    if (item == deviceChoiceGroup) {
        if (command == servicesDiscoverCommand) {
            if(deviceList.size()==0) {
                return; 
            }

            UUID[] searchList = new UUID[1];
            searchList[0] = new UUID("11111111111111111111111111111111",false);

            int[] attrSet = new int[1];
            attrSet[0] = 0x100;

            RemoteDevice currentDevice = 
                (RemoteDevice) deviceList.elementAt(
                    getDeviceChoiceGroup().getSelectedIndex());

            if(currentDevice == null) { 
                return; 
            }

            try {
                transactionID = bluetoothDiscoveryAgent.searchServices(
                        new int[] {0x100}, searchList, currentDevice, this);
                printToForm("Start services under L2CAP searching...");
                form.addCommand(cancelServicesDiscoverCommand);
            } catch (BluetoothStateException e) {
                //TODO: write handler code
            }
        }
    }
}


public void servicesDiscovered(int transID, ServiceRecord[] serviceRecords){
    if (serviceRecords.length>0 && serviceRecords!=null)
    {
        connectionURL=serviceRecords[0].getConnectionURL(0, false);

        int[] ids=serviceRecords[0].getAttributeIDs();  
        DataElement ServiceName=serviceRecords[0].getAttributeValue(ids[1]);           
        // tried to convert objedct to string. 
        String str = (ServiceName.getValue()).toString();
        // out is put is like java.util.vector$1@3c60cd14c
        printToForm("#Service name: "+str); 

        printToForm("The Service name is: "+ServiceName.getValue());

    }
}

"DataElement.getValue()" which returns object. Thus I can see service name as "java.util.vector$1@3c60cd14c". I tried to convert object to string as "String str = (ServiceName.getValue()).toString();" It doesn't convert correctly.

So how to convert object to string. So that I could see the service name in plain text. Thanks!

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

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

发布评论

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

评论(1

维持三分热 2024-12-18 00:37:09

通过查看结果:java.util.vector$1@3c60cd14c我猜返回的对象的类型是Vector。

因此,尝试转换为 Vector 并迭代它以获取值。

Iterator itr = serviceName.getValue().iterator();//do something here
System.out.println("Iterating through Vector elements...");
while(itr.hasNext())
      System.out.println(itr.next());

By seeing the result : java.util.vector$1@3c60cd14c i guess the returned object's type is Vector.

So try to cast to the Vector and iterate through it to get the values.

Iterator itr = serviceName.getValue().iterator();//do something here
System.out.println("Iterating through Vector elements...");
while(itr.hasNext())
      System.out.println(itr.next());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文