13,100
社区成员
发帖
与我相关
我的任务
分享
public void deviceDiscovered(RemoteDevice arg0, DeviceClass arg1)
{
// 这个回调函数保存你搜索到的设备到列表
if (vectorDevice.indexOf(arg0) != -1);
{
vectorDevice.addElement(arg0);
}
}
LocalDevice.getLocalDevice()..getDiscoveryAgent().searchServices(new int[] { 0x100 },new UUID[] { new UUID(0x1101) }, remotedevice, this/*是个listener*/)
public void servicesDiscovered(int arg0, ServiceRecord[] arg1)
{
// 这里是在设备上发现的所有服务
for (int i = 0; i < arg1.length; i++)
{
this.vector_serviceRecord.addElement(arg1[i]);
}
}
ServiceRecord sRecord = (ServiceRecord) this.vector_serviceRecord
.elementAt(i);
int[] ids = sRecord.getAttributeIDs();
for (int j = 0; j < ids.length; j++) {
DataElement element = sRecord.getAttributeValue(ids[j]);
String sn = this.getServiceName(element);
if (sn != null)
//这个sn就是服务名
}
//获得服务名
private String getServiceName(DataElement element)
{
int elementType = element.getDataType();
if (elementType == DataElement.STRING)
return element.getValue().toString();
else if (elementType == DataElement.DATSEQ)
{
{
Enumeration children = (Enumeration) element.getValue();
while (children.hasMoreElements())
{
String serviceName = this
.getServiceName((DataElement) children
.nextElement());
if (serviceName != null)
return serviceName;
}
}
}
return null;
}
private String getConnectionURL(ServiceRecord record)
{
DataElement protocolDescriptorList = record.getAttributeValue(0x0004);
Enumeration e = (Enumeration) protocolDescriptorList.getValue(); // DATSEQ
// |
// DATALT
e.nextElement(); // L2CAP (ignored)
DataElement protocolDescriptorRFCOMM = (DataElement) e.nextElement();
e = (Enumeration) protocolDescriptorRFCOMM.getValue(); // DATSEQ
e.nextElement(); // UUID (ignored)
DataElement channelRFCOMM = (DataElement) e.nextElement();
long channel = channelRFCOMM.getLong(); // U_INT_1
StringBuffer nameBuffer = new StringBuffer(69); // 5+3+12+1+2+19+14+13);
nameBuffer.append("btspp://");
nameBuffer.append(record.getHostDevice().getBluetoothAddress());
nameBuffer.append(":");
nameBuffer.append(channel);
nameBuffer.append(";authenticate=false");
nameBuffer.append(";encrypt=false");
nameBuffer.append(";master=false");
return nameBuffer.toString();
}