如何使蓝牙服务公开发现?

问题描述:

我尝试创建一个提供公共可用服务的MIDlet,但下面的代码是不够的。该服务已开启(无例外),但仍无法发现。如何使蓝牙服务公开发现?

public StreamConnection waitForConnection() throws IOException { 

    if (this.notifier == null) { 
     // Create a server connection (a notifier) 
     this.notifier = (StreamConnectionNotifier) Connector.open(serviceURL); 
    } 
    return this.notifier.acceptAndOpen(); 
} 

的URL的结构如下

private final static String serviceURL = "btspp://localhost:" + servieceUUID + 
     ";name=" + serviceName + ";authenticate=false;master=false;encrypt=false"; 

经过一番google搜索,我发现,像这样HOULD帮助代码:

 final ServiceRecord sr = LocalDevice.getLocalDevice().getRecord(this.notifier); 
     //Public browse group UUID 
     final DataElement element = new DataElement(DataElement.DATSEQ); 
     element.addElement(new DataElement(DataElement.UUID, new UUID(0x1002))); 
     sr.setAttributeValue(0x0005, element); 
     LocalDevice.getLocalDevice().updateRecord(sr); 

但首先它并没有解决问题,第二我不知道它实际上做了什么。

我使用诺基亚E70。

任何想法?

在此先感谢。

+0

你想创建什么类型的服务?什么在做发现?你有没有编写试图发现它的代码? – funkybro 2009-12-10 10:01:51

您是否尝试过使用setDiscoverable方法?

+0

朱 - 只是忘了把它放在一个代码示例埠它在那里:) – 2010-01-04 13:46:10

我知道这个问题真的很老,但我只是碰到它,经过一天我的头撞墙,我找到了解决方案。

你需要该服务的服务类ID设置为已知的东西:

ServiceRecord sr = LocalDevice.getLocalDevice().getRecord(service); 
DataElement de = new DataElement(DataElement.DATSEQ); 
DataElement uuid = new DataElement(DataElement.UUID, new UUID("1101",false)); 
de.addElement(uuid); 
sr.setAttributeValue(0x0001, de); 
LocalDevice.getLocalDevice().updateRecord(sr); 

UUID 0x1101是一个串行设备(我选择了这个任意)和属性是0x0001的服务类ID属性。