【问题标题】:how to convert java object to plain string in j2me Bluetooth application?如何在 j2me 蓝牙应用程序中将 java 对象转换为纯字符串?
【发布时间】:2011-10-19 07:19:33
【问题描述】:

我正在制作一个 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();”它不能正确转换。

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

【问题讨论】:

    标签: java string object java-me


    【解决方案1】:

    通过查看结果: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());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-15
      • 1970-01-01
      • 2015-05-24
      • 2020-04-19
      • 2012-01-22
      相关资源
      最近更新 更多