【发布时间】: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