Package totalcross.io.device.bluetooth
Class DiscoveryAgent
- java.lang.Object
-
- totalcross.io.device.bluetooth.DiscoveryAgent
-
public class DiscoveryAgent extends java.lang.ObjectTheDiscoveryAgentclass provides methods to perform device and service discovery. A local device must have only oneDiscoveryAgentobject. This object must be retrieved by a call togetDiscoveryAgent()on theLocalDeviceobject.- Since:
- TotalCross 1.15
-
-
Field Summary
Fields Modifier and Type Field Description static intCACHEDUsed with theretrieveDevices()method to return those devices that were found via a previous inquiry.static intGIACThe inquiry access code for General/Unlimited Inquiry Access Code (GIAC).static intLIACThe inquiry access code for Limited Dedicated Inquiry Access Code (LIAC).static intNOT_DISCOVERABLETakes the device out of discoverable mode.static intPREKNOWNUsed with theretrieveDevices()method to return those devices that are defined to be pre-known devices.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancancelInquiry(DiscoveryListener listener)Removes the device from inquiry mode.booleancancelServiceSearch(int transID)Cancels the service search transaction that has the specified transaction ID.RemoteDevice[]retrieveDevices(int option)Returns an array of Bluetooth devices that have either been found by the local device during previous inquiry requests or been specified as a pre-known device depending on the argument.intsearchServices(int[] attrSet, UUID[] uuidSet, RemoteDevice btDev, DiscoveryListener discListener)Searches for services on a remote Bluetooth device that have all the UUIDs specified inuuidSet.java.lang.StringselectService(UUID uuid, int security, boolean master)Attempts to locate a service that containsuuidin the ServiceClassIDList of its service record.booleanstartInquiry(int accessCode, DiscoveryListener listener)Places the device into inquiry mode.
-
-
-
Field Detail
-
CACHED
public static final int CACHED
Used with theretrieveDevices()method to return those devices that were found via a previous inquiry. If no inquiries have been started, this will cause the method to returnnull.The value of
CACHEDis 0x00 (0).- See Also:
- Constant Field Values
-
GIAC
public static final int GIAC
The inquiry access code for General/Unlimited Inquiry Access Code (GIAC). This is used to specify the type of inquiry to complete or respond to.The value of
GIACis 0x9E8B33 (10390323). This value is defined in the Bluetooth Assigned Numbers document.- See Also:
- Constant Field Values
-
LIAC
public static final int LIAC
The inquiry access code for Limited Dedicated Inquiry Access Code (LIAC). This is used to specify the type of inquiry to complete or respond to.The value of
LIACis 0x9E8B00 (10390272). This value is defined in the Bluetooth Assigned Numbers document.- See Also:
- Constant Field Values
-
NOT_DISCOVERABLE
public static final int NOT_DISCOVERABLE
Takes the device out of discoverable mode.The value of
NOT_DISCOVERABLEis 0x00 (0).- See Also:
- Constant Field Values
-
PREKNOWN
public static final int PREKNOWN
Used with theretrieveDevices()method to return those devices that are defined to be pre-known devices. Pre-known devices are specified in the BCC. These are devices that are specified by the user as devices with which the local device will frequently communicate.The value of
PREKNOWNis 0x01 (1).- See Also:
- Constant Field Values
-
-
Method Detail
-
cancelInquiry
public boolean cancelInquiry(DiscoveryListener listener)
Removes the device from inquiry mode.An
inquiryCompleted()event will occur with a type ofINQUIRY_TERMINATEDas a result of calling this method. After receiving this event, no furtherdeviceDiscovered()events will occur as a result of this inquiry.This method will only cancel the inquiry if the
Not implemented on Android.listenerprovided is the listener that started the inquiry.- Parameters:
listener- the listener that is receiving inquiry events- Returns:
trueif the inquiry was canceled; otherwisefalseif the inquiry was not canceled or if the inquiry was not started usinglistener- Throws:
java.lang.NullPointerException- iflistenerisnull- Since:
- TotalCross 1.2
-
cancelServiceSearch
public boolean cancelServiceSearch(int transID)
Cancels the service search transaction that has the specified transaction ID. The ID was assigned to the transaction by the methodsearchServices(). AserviceSearchCompleted()event with a discovery type ofSERVICE_SEARCH_TERMINATEDwill occur when this method is called. After receiving this event, no furtherservicesDiscovered()events will occur as a result of this search. Not implemented on Android and Windows CE.- Parameters:
transID- the ID of the service search transaction to cancel; returned bysearchServices()- Returns:
trueif the service search transaction is terminated, elsefalseiftransIDdoes not represent an active service search transaction- Since:
- TotalCross 1.2
-
retrieveDevices
public RemoteDevice[] retrieveDevices(int option)
Returns an array of Bluetooth devices that have either been found by the local device during previous inquiry requests or been specified as a pre-known device depending on the argument. The list of previously found devices is maintained by the implementation of this API. (In other words, maintenance of the list of previously found devices is an implementation detail.) A device can be set as a pre-known device in the Bluetooth Control Center. Works on Android. Pass CACHED to list the unpaired devices, and PREKNOWN to list the paired devices. Note that paired devices may not be at reach at the moment.- Parameters:
option-option-CACHEDif previously found devices should be returned;PREKNOWNif pre-known devices should be returned- Returns:
- an array containing the Bluetooth devices that were previously found if
optionisCACHED; an array of devices that are pre-known devices ifoptionisPREKNOWN;nullif no devices meet the criteria - Throws:
java.lang.IllegalArgumentException- ifoptionis notCACHEDorPREKNOWN
-
searchServices
public int searchServices(int[] attrSet, UUID[] uuidSet, RemoteDevice btDev, DiscoveryListener discListener) throws IOExceptionSearches for services on a remote Bluetooth device that have all the UUIDs specified inuuidSet. Once the service is found, the attributes specified inattrSetand the default attributes are retrieved. The default attributes are ServiceRecordHandle (0x0000), ServiceClassIDList (0x0001), ServiceRecordState (0x0002), ServiceID (0x0003), and ProtocolDescriptorList (0x0004). IfattrSetisnullthen only the default attributes will be retrieved.attrSetdoes not have to be sorted in increasing order, but must only contain values in the range [0 - (216-1)]. Not implemented on Android.- Parameters:
attrSet- indicates the attributes whose values will be retrieved on services which have the UUIDs specified inuuidSetuuidSet- the set of UUIDs that are being searched for; all services returned will contain all the UUIDs specified herebtDev- the remote Bluetooth device to search for services ondiscListener- the object that will receive events when services are discovered- Returns:
- the transaction ID of the service search, which is a positive number.
- Throws:
java.lang.NullPointerException- ifuuidSet,btDev, ordiscListenerisnull; if an element inuuidSetarray isnulljava.lang.IllegalArgumentException- ifattrSethas an illegal service attribute ID or exceeds the propertybluetooth.sd.attr.retrievable.maxdefined in the classLocalDevice; ifattrSetoruuidSetis of length 0; ifattrSetoruuidSetcontains duplicatesIOException- if the number of concurrent service search transactions exceeds the limit specified by thebluetooth.sd.trans.maxproperty obtained from the classLocalDeviceor the system is unable to start one due to current conditions
-
selectService
public java.lang.String selectService(UUID uuid, int security, boolean master) throws IOException
Attempts to locate a service that containsuuidin the ServiceClassIDList of its service record. This method will return a string that may be used inConnector.open()to establish a connection to the service. How the service is selected if there are multiple services withuuidand which devices to search is implementation dependent. Not implemented on Android.- Parameters:
uuid- the UUID to search for in the ServiceClassIDListsecurity- specifies the security requirements for a connection to this service; must be one ofServiceRecord.NOAUTHENTICATE_NOENCRYPT,ServiceRecord.AUTHENTICATE_NOENCRYPT, orServiceRecord.AUTHENTICATE_ENCRYPTmaster- determines if this client must be the master of the connection;trueif the client must be the master;falseif the client can be the master or the slave- Returns:
- the connection string used to connect to the service with a UUID of
uuid; ornullif no service could be found with a UUID ofuuidin the ServiceClassIDList - Throws:
java.lang.NullPointerException- ifuuidisnulljava.lang.IllegalArgumentException- ifsecurityis notServiceRecord.NOAUTHENTICATE_NOENCRYPT,ServiceRecord.AUTHENTICATE_NOENCRYPT, orServiceRecord.AUTHENTICATE_ENCRYPTIOException- if the Bluetooth system cannot start the request due to the current state of the Bluetooth system- Since:
- TotalCross 1.2
-
startInquiry
public boolean startInquiry(int accessCode, DiscoveryListener listener) throws IOExceptionPlaces the device into inquiry mode. The length of the inquiry is implementation dependent. This method will search for devices with the specified inquiry access code. Devices that responded to the inquiry are returned to the application via the methoddeviceDiscovered()of the interfaceDiscoveryListener. ThecancelInquiry()method is called to stop the inquiry. Not implemented on Android.- Parameters:
accessCode- the type of inquiry to completelistener- the event listener that will receive device discovery events- Returns:
trueif the inquiry was started;falseif the inquiry was not started because theaccessCodeis not supported- Throws:
java.lang.IllegalArgumentException- if the access code provided is notLIAC,GIAC, or in the range 0x9E8B00 to 0x9E8B3Fjava.lang.NullPointerException- iflistenerisnullIOException- if the Bluetooth device does not allow an inquiry to be started due to other operations that are being performed by the device- Since:
- TotalCross 1.2
-
-