📄 servicerecordimpl.java
字号:
/* Copyright 2004 Intel Corporation This file is part of Blue Cove. Blue Cove is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. Blue Cove is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with Blue Cove; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package com.intel.bluetooth;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.util.Enumeration;import java.util.Hashtable;import javax.bluetooth.BluetoothStateException;import javax.bluetooth.DataElement;import javax.bluetooth.LocalDevice;import javax.bluetooth.RemoteDevice;import javax.bluetooth.ServiceRecord;import javax.bluetooth.UUID;public class ServiceRecordImpl implements ServiceRecord { private RemoteDevice device; private int handle; Hashtable attributes; ServiceRecordImpl(RemoteDevice device, int handle) { this.device = device; this.handle = handle; attributes = new Hashtable(); } byte[] toByteArray() { DataElement element = new DataElement(DataElement.DATSEQ); for (Enumeration e = attributes.keys(); e.hasMoreElements();) { Integer key = (Integer) e.nextElement(); element.addElement(new DataElement(DataElement.U_INT_2, key .intValue())); element.addElement((DataElement) attributes.get(key)); } ByteArrayOutputStream out = new ByteArrayOutputStream(); try { (new SDPOutputStream(out)).writeElement(element); } catch (Exception e) { } return out.toByteArray(); } /* * Returns the value of the service attribute ID provided it is present in * the service record, otherwise this method returns null. Parameters: * attrID - the attribute whose value is to be returned Returns: the value * of the attribute ID if present in the service record, otherwise null * Throws: IllegalArgumentException - if attrID is negative or greater than * or equal to 2^16 */ public DataElement getAttributeValue(int attrID) { if (attrID < 0x0000 || attrID > 0xffff) throw new IllegalArgumentException(); return (DataElement) attributes.get(new Integer(attrID)); } /* * Returns the remote Bluetooth device that populated the service record * with attribute values. It is important to note that the Bluetooth device * that provided the value might not be reachable anymore, since it can * move, turn off, or change its security mode denying all further * transactions. Returns: the remote Bluetooth device that populated the * service record, or null if the local device populated this ServiceRecord */ public RemoteDevice getHostDevice() { return device; } /* * Returns the service attribute IDs whose value could be retrieved by a * call to getAttributeValue(). The list of attributes being returned is not * sorted and includes default attributes. Returns: an array of service * attribute IDs that are in this object and have values for them; if there * are no attribute IDs that have values, this method will return an array * of length zero. See Also: getAttributeValue(int) */ public int[] getAttributeIDs() { int[] attrIDs = new int[attributes.size()]; int i = 0; for (Enumeration e = attributes.keys(); e.hasMoreElements();) attrIDs[i++] = ((Integer) e.nextElement()).intValue(); return attrIDs; } /* * Retrieves the values by contacting the remote Bluetooth device for a set * of service attribute IDs of a service that is available on a Bluetooth * device. (This involves going over the air and contacting the remote * device for the attribute values.) The system might impose a limit on the * number of service attribute ID values one can request at a time. * Applications can obtain the value of this limit as a String by calling * LocalDevice.getProperty("bluetooth.sd.attr.retrievable.max"). The method * is blocking and will return when the results of the request are * available. Attribute IDs whose values could be obtained are added to this * service record. If there exist attribute IDs for which values are * retrieved this will cause the old values to be overwritten. If the remote * device cannot be reached, an IOException will be thrown. Parameters: * attrIDs - the list of service attributes IDs whose value are to be * retrieved; the number of attributes cannot exceed the property * bluetooth.sd.attr.retrievable.max; the attributes in the request must be * legal, i.e. their values are in the range of [0, 2^16-1]. The input * attribute IDs can include attribute IDs from the default attribute set * too. Returns: true if the request was successful in retrieving values for * some or all of the attribute IDs; false if it was unsuccessful in * retrieving any values Throws: java.io.IOException - if the local device * is unable to connect to the remote Bluetooth device that was the source * of this ServiceRecord; if this ServiceRecord was deleted from the SDDB of * the remote device IllegalArgumentException - if the size of attrIDs * exceeds the system specified limit as defined by * bluetooth.sd.attr.retrievable.max; if the attrIDs array length is zero; * if any of their values are not in the range of [0, 2^16-1]; if attrIDs * has duplicate values NullPointerException - if attrIDs is null * RuntimeException - if this ServiceRecord describes a service on the local * device rather than a service on a remote device */ public boolean populateRecord(int[] attrIDs) throws IOException { /* * check this is not a local service record */ if (device == null) throw new RuntimeException(); /* * check attrIDs is non-null and has length > 0 */ if (attrIDs.length == 0) throw new IllegalArgumentException(); /* * check attrIDs are in range */ for (int i = 0; i < attrIDs.length; i++) if (attrIDs[i] < 0x0000 || attrIDs[i] > 0xffff) throw new IllegalArgumentException(); /* * copy and sort attrIDs (required by MS Bluetooth) */ int[] sortIDs = new int[attrIDs.length]; System.arraycopy(attrIDs, 0, sortIDs, 0, attrIDs.length); for (int i = 0; i < sortIDs.length; i++) for (int j = 0; j < sortIDs.length - i - 1; j++) if (sortIDs[j] > sortIDs[j + 1]) { int temp = sortIDs[j]; sortIDs[j] = sortIDs[j + 1]; sortIDs[j + 1] = temp; } /* * check for duplicates */ for (int i = 0; i < sortIDs.length - 1; i++) if (sortIDs[i] == sortIDs[i + 1]) throw new IllegalArgumentException(); /* * retrieve SDP blob */ byte[] blob = (LocalDevice.getLocalDevice()).getBluetoothPeer() .getServiceAttributes(sortIDs, Long.parseLong(device.getBluetoothAddress(), 16), handle); if (blob.length > 0) try { DataElement element = (new SDPInputStream( new ByteArrayInputStream(blob))).readElement(); for (Enumeration e = (Enumeration) element.getValue(); e .hasMoreElements();) attributes.put(new Integer((int) ((DataElement) e .nextElement()).getLong()), e.nextElement()); return true; } catch (Exception e) { throw new IOException(); } else return false; } /* * Returns a String including optional parameters that can be used by a * client to connect to the service described by this ServiceRecord. The * return value can be used as the first argument to Connector.open(). In * the case of a Serial Port service record, this string might look like * "btspp://0050CD00321B:3;authenticate=true;encrypt=false;master=true",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -