2011-10-19 10 views
0

私はj2me Bluetoothアプリケーションを作っています。私はまた、Javaの世界では新しいです。私はどこにユーザーにBluetoothサービス名を表示する必要があります。これまでのところ、サービス名以外はすべて正常に動作しているようです。 Bluetoothサーバーが他のクライアントによって正しくサービス名を広告していることを確認しました(qtによって完了)。私は次のようにしました: -j2me BluetoothアプリケーションでJavaオブジェクトをプレーンストリングに変換する方法は?

  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 [email protected] 
     printToForm("#Service name: "+str); 

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

    } 
} 

"DataElement.getValue()"はオブジェクトを返します。したがって、サービス名は "[email protected]"と表示されます。私はオブジェクトを "String str =(ServiceName.getValue())。toString();"という文字列に変換しようとしました。正しく変換されません。

オブジェクトを文字列に変換する方法。私は平文でサービス名を見ることができました。ありがとう!

答えて

0

結果を見ることによって:[email protected]私は返されたオブジェクトの型がVectorだと思います。

ベクトルにキャストし、それを繰り返して値を取得しようとします。

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