Android Bluetooth Tutorial
Android Bluetooth Tutorial
Summary: Constants | Inherited Constants | Fields | Methods | Inherited Methods | [Expand All] Since: API Level 5
BluetoothDevice
extends Object implements Parcelable java.lang.Object android.bluetooth.BluetoothDevice
Class Overview
Represents a remote Bluetooth device. A B u t o h e i e leotDvc ( r f r n e a d o d b u t o h B u t o h e i e h m )lets you create a /eeec/nri/leot/leotDvc.tl connection with the respective device or query information about it, such as the name, address, class, and bonding state. This class is really just a thin wrapper for a Bluetooth hardware address. Objects of this class are immutable. Operations on this class are performed on the remote Bluetooth hardware address, using the Butohdpe leotAatr ( r f r n e a d o d b u t o h B u t o h d p e . t l that was used /eeec/nri/leot/leotAatrhm) to create this B u t o h e i e leotDvc (rfrneadodbutohButoheiehm) /eeec/nri/leot/leotDvc.tl. To get a B u t o h e i e leotDvc
( r f r n e a d o d b u t o h B u t o h e i e h m ) use /eeec/nri/leot/leotDvc.tl,
Butohdpe.eRmtDvc(tig leotAatrgteoeeieSrn)
(rfrneadodbutohButohdpe.tlgteoeei /eeec/nri/leot/leotAatrhm#eRmtDvc e j v . a g S r n ) to create one representing a (aaln.tig)
device of a known MAC address (which you can get through device discovery with Butohdpe leotAatr ( r f r n e a d o d b u t o h B u t o h d p e . t l ) or get one from /eeec/nri/leot/leotAatrhm) the set of bonded devices returned by Butohdpe.eBneDvcs) leotAatrgtoddeie(
(rfrneadodbutohButohdpe.tlgtoddei /eeec/nri/leot/leotAatrhm#eBneDvc e ( ) You can then open a s).
Butohokt leotSce
( r f r n e a d o d b u t o h B u t o h o k t h m )for /eeec/nri/leot/leotSce.tl
developer.android.com/reference/android/bluetooth/BluetoothDevice.html
1/16
permission.
Developer Guides
For more information about using Bluetooth, read the Bluetooth (/guide/topics/wireless/bluetooth.html) developer guide. See Also Butohdpe leotAatr Butohokt leotSce
Summary
Constants String ACTION_ACL_CONNECTED Broadcast Action: Indicates a low level (ACL) connection has been established with a remote device. Broadcast Action: Indicates a low level (ACL) disconnection from a remote device.
String ACTION_ACL_DISCONNECTED
Broadcast Action: Indicates that a low level (ACL) disconnection has been String ACTION_ACL_DISCONNECT_REQUESTED requested for a remote device, and it will soon be disconnected. String ACTION_BOND_STATE_CHANGED Broadcast Action: Indicates a change in the bond state of a remote device. Broadcast Action: Bluetooth class of a remote device has changed. Broadcast Action: Remote device discovered. Broadcast Action: Indicates the friendly name of a remote device has been retrieved for the first time, or changed since the last retrieval. Broadcast Action: This intent is used to broadcast the U I UD wrapped as a P r e U i of aclud the remote device after it has
String ACTION_NAME_CHANGED
String ACTION_UUID
10/1/12
been fetched. int BOND_BONDED Indicates the remote device is bonded (paired). Indicates bonding (pairing) is in progress with the remote device. Indicates the remote device is not bonded (paired). Sentinel error value for this class. Used as an int extra field in ATO_ODSAECAGD CINBN_TT_HNE intents. Used as a Parcelable B u t o h l s extra field in leotCas A T O _ O N and CINFUD ATO_LS_HNE CINCASCAGD intents. Used as a Parcelable Butoheie l e o t D v c extra field in every intent broadcast by this class. Used as a String extra field in ATO_AECAGD C I N N M _ H N E and A T O _ O N intents. CINFUD Used as an int extra field in ATO_ODSAECAGD CINBN_TT_HNE intents. Used as an optional short extra field in A T O _ O N intents. CINFUD Used as an extra field in ATO_UD C I N U I intents, Contains the P r e U i s of the remote aclud device which is a parcelable version of U I . UD
[Expand]
String EXTRA_BOND_STATE
String EXTRA_CLASS
String EXTRA_DEVICE
String EXTRA_NAME
String EXTRA_UUID
Inherited Constants
From interface android.os.Parcelable Fields public static final Creator<BluetoothDevice> CREATOR Public Methods createInsecureRfcommSocketToServiceRecord (UUID uuid) BluetoothSocket Create an RFCOMM B u t o h o k t l e o t S c e socket ready to start an insecure outgoing connection to this
developer.android.com/reference/android/bluetooth/BluetoothDevice.html
10/1/12
remote device using SDP lookup of uuid. createRfcommSocketToServiceRecord (UUID uuid) BluetoothSocket Create an RFCOMM B u t o h o k t l e o t S c e ready to start a secure outgoing connection to this remote device using SDP lookup of uuid. describeContents16 API level: () Describe the kinds of special objects contained in this Parcelable's marshalled representation.
Android APIs
int
android android.accessibilityservice equals (Object o) android.accounts Compares this instance with the specified object and boolean android.animation indicates if they are equal. android.app fetchUuidsWithSdp () android.app.admin boolean Perform a service discovery on the remote device to android.app.backup android.appwidget get the UUIDs supported. android.bluetoothgetAddress () String Returns the hardware address of this android.content android.content.pm BluetoothDevice. android.content.res getBluetoothClass () BluetoothClass android.database Get the Bluetooth class of the remote device. android.database.sqlite getBondState () BluetoothA2dpint Get the bond state of the remote device. BluetoothAdapter getName () BluetoothAssignedNumbers String Get the friendly Bluetooth name of the remote device. BluetoothClass getUuids () BluetoothClass.Device ParcelUuid[] Returns the BluetoothClass.Device.Major supported features (UUIDs) of the remote BluetoothClass.Service device. BluetoothDevice hashCode () int BluetoothHeadset Returns an integer hash code for this object. BluetoothHealth toString () BluetoothHealthAppConfiguration BluetoothHealthCallback a string representation of this String Returns BluetoothDevice. BluetoothServerSocket BluetoothSocket
Use Tree Navigation
void
Constants
developer.android.com/reference/android/bluetooth/BluetoothDevice.html
10/1/12
Broadcast Action: Indicates a low level (ACL) connection has been established with a remote device. Always contains the extra field E T A D V C XR_EIE
(rfrneadodbutohButoheiehm#XR_EIE /eeec/nri/leot/leotDvc.tlETADVC ) .
ACL connections are managed automatically by the Android Bluetooth stack. Requires B U T O H LEOT
( r f r n e a d o d M n f s . e m s i n h m # L E O T )to /eeec/nri/aietpriso.tlBUTOH
Broadcast Action: Indicates a low level (ACL) disconnection from a remote device. Always contains the extra field E T A D V C XR_EIE
(rfrneadodbutohButoheiehm#XR_EIE /eeec/nri/leot/leotDvc.tlETADVC ) .
ACL connections are managed automatically by the Android Bluetooth stack. Requires B U T O H LEOT
( r f r n e a d o d M n f s . e m s i n h m # L E O T )to /eeec/nri/aietpriso.tlBUTOH
Broadcast Action: Indicates that a low level (ACL) disconnection has been requested for a remote device, and it will soon be disconnected. This is useful for graceful disconnection. Applications should use this intent as a hint to immediately terminate higher level connections (RFCOMM, L2CAP, or profile connections) to the remote device.
developer.android.com/reference/android/bluetooth/BluetoothDevice.html
5/16
10/1/12
Requires B U T O H LEOT
( r f r n e a d o d M n f s . e m s i n h m # L E O T )to /eeec/nri/aietpriso.tlBUTOH
Broadcast Action: Indicates a change in the bond state of a remote device. For example, if a device is bonded (paired). Always contains the extra fields E T A D V C XR_EIE
(rfrneadodbutohButoheiehm#XR_EIE /eeec/nri/leot/leotDvc.tlETADVC )ETABN_TT , XR_ODSAE (rfrneadodbutohButoheiehm#XR_ODS /eeec/nri/leot/leotDvc.tlETABN_ T T )and E T A P E I U _ O D S A E AE XR_RVOSBN_TT (rfrneadodbutohButoheiehm#XR_RVO /eeec/nri/leot/leotDvc.tlETAPEI U_ODSAE. SBN_TT)
Requires B U T O H LEOT
( r f r n e a d o d M n f s . e m s i n h m # L E O T )to /eeec/nri/aietpriso.tlBUTOH
Requires B U T O H LEOT
( r f r n e a d o d M n f s . e m s i n h m # L E O T )to /eeec/nri/aietpriso.tlBUTOH
developer.android.com/reference/android/bluetooth/BluetoothDevice.html
6/16
10/1/12
"android.bluetooth.device.action.CLASS_CHANGED"
and/or E T A R S XR_SI
( r f r n e a d o d b u t o h B u t o h e i e h m # X R _ S I if /eeec/nri/leot/leotDvc.tlETARS)
Broadcast Action: Indicates the friendly name of a remote device has been retrieved for the first time, or changed since the last retrieval. Always contains the extra fields E T A D V C XR_EIE
(rfrneadodbutohButoheiehm#XR_EIE /eeec/nri/leot/leotDvc.tlETADVC )and E T A N M XR_AE (rfrneadodbutohButoheiehm#XR_AE. /eeec/nri/leot/leotDvc.tlETANM)
Requires B U T O H LEOT
( r f r n e a d o d M n f s . e m s i n h m # L E O T )to /eeec/nri/aietpriso.tlBUTOH
Broadcast Action: This intent is used to broadcast the U I UD ( r f r n e j v / t l U I . t l wrapped as a P r e U i /eeec/aaui/UDhm) aclud ( r f r n e a d o d o / a c l u d h m )of the remote device after /eeec/nri/sPreUi.tl it has been fetched. This intent is sent only when the UUIDs of the remote device are requested to be fetched using Service Discovery Protocol
developer.android.com/reference/android/bluetooth/BluetoothDevice.html
7/16
10/1/12
Requires B U T O H LEOT
( r f r n e a d o d M n f s . e m s i n h m # L E O T )to /eeec/nri/aietpriso.tlBUTOH
Being bonded (paired) with a remote device does not necessarily mean the device is currently connected. It just means that the pending procedure was completed at some earlier time, and the link key is still stored locally, ready to use on the next connection. Constant Value: 12 (0x0000000c)
Indicates bonding (pairing) is in progress with the remote device. Constant Value: 11 (0x0000000b)
There is no shared link key with the remote device, so communication (if it is allowed at all) will be unauthenticated and unencrypted. Constant Value: 10 (0x0000000a)
Sentinel error value for this class. Guaranteed to not equal any other integer constant in this class. Provided as a convenience for functions that require a sentinel error value, for example: Itn.eItxr(leotDvc.XR_ODSAE netgtnEtaButoheieETABN_TT,
developer.android.com/reference/android/bluetooth/BluetoothDevice.html
8/16
10/1/12
BN_ODN ODBNIG
(rfrneadodbutohButoheiehm#ODBNIG /eeec/nri/leot/leotDvc.tlBN_ODN )BN_ODD , ODBNE (rfrneadodbutohButoheiehm#ODBNE) /eeec/nri/leot/leotDvc.tlBN_ODD.
( r f r n e a d o d b u t o h B u t o h l s . t l extra /eeec/nri/leot/leotCashm)
field in
ATO_ON CINFUD
(rfrneadodbutohButoheiehm#CINFUD /eeec/nri/leot/leotDvc.tlATO_ON )and A T O _ L S _ H N E CINCASCAGD (rfrneadodbutohButoheiehm#CINCAS /eeec/nri/leot/leotDvc.tlATO_LS _ H N E )intents. CAGD
( r f r n e a d o d b u t o h B u t o h e i e h m )extra /eeec/nri/leot/leotDvc.tl
field in every intent broadcast by this class. It contains the B u t o h e i e leotDvc ( r f r n e a d o d b u t o h B u t o h e i e h m )that the /eeec/nri/leot/leotDvc.tl intent applies to. Constant Value: "android.bluetooth.device.extra.DEVICE"
developer.android.com/reference/android/bluetooth/BluetoothDevice.html
10/1/12
the remote
BN_ODN ODBNIG
(rfrneadodbutohButoheiehm#ODBNIG /eeec/nri/leot/leotDvc.tlBN_ODN )BN_ODD , ODBNE (rfrneadodbutohButoheiehm#ODBNE) /eeec/nri/leot/leotDvc.tlBN_ODD.
(rfrneadodbutohButoheiehm#CINUI) /eeec/nri/leot/leotDvc.tlATO_UD
Fields
public static final Creator<BluetoothDevice> CREATOR
developer.android.com/reference/android/bluetooth/BluetoothDevice.html
10/1/12
Public Methods
public BluetoothSocket createInsecureRfcommSocketToServiceRecord (UUID uuid)
Create an RFCOMM B u t o h o k t leotSce
( r f r n e a d o d b u t o h B u t o h o k t h m )socket ready /eeec/nri/leot/leotSce.tl
to start an insecure outgoing connection to this remote device using SDP lookup of uuid. The communication channel will not have an authenticated link key i.e it will be subject to man-in-the-middle attacks. For Bluetooth 2.1 devices, the link key will be encrypted, as encryption is mandatory. For legacy devices (pre Bluetooth 2.1 devices) the link key will be not be encrypted. Use c e t R c m S c e T S r i e e o d U I ) raefomoktoevcRcr(UD
(rfrneadodbutohButoheiehm#raefom /eeec/nri/leot/leotDvc.tlcetRcm S c e T S r i e e o d j v . t l U I ) if oktoevcRcr(aaui.UD)
an encrypted and authenticated communication channel is desired. This is designed to be used with lseUigneuefomihevcRcr(tig itnsnIscrRcmWtSrieeodSrn, UI) UD
(rfrneadodbutohButohdpe.tllseUig /eeec/nri/leot/leotAatrhm#itnsn IscrRcmWtSrieeodjv.agSrn, neuefomihevcRcr(aaln.tig j v . t l U I ) for peer-peer Bluetooth applications. aaui.UD)
Use c n e t ) onc(
( r f r n e a d o d b u t o h B u t o h o k t h m # o n c ( )to /eeec/nri/leot/leotSce.tlcnet)
initiate the outgoing connection. This will also perform an SDP lookup of the given uuid to determine which channel to connect to. The remote device will be authenticated and communication on this socket will be encrypted. Hint: If you are connecting to a Bluetooth serial board then try using the well-known SPP UUID 00001101-0000-1000-800000805F9B34FB. However if you are connecting to an Android peer then please generate your own unique UUID. Requires B U T O H LEOT
(rfrneadodMnfs.emsinhm#LEOT) /eeec/nri/aietpriso.tlBUTOH
developer.android.com/reference/android/bluetooth/BluetoothDevice.html
11/16
10/1/12
Returns a RFCOMM BluetoothServerSocket ready for an outgoing connection Throws IOException on error, for example Bluetooth not available, or insufficient permissions
a secure outgoing connection to this remote device using SDP lookup of uuid. This is designed to be used with lseUigfomihevcRcr(tig UI) itnsnRcmWtSrieeodSrn, UD
(rfrneadodbutohButohdpe.tllseUig /eeec/nri/leot/leotAatrhm#itnsn R c m W t S r i e e o d j v . a g S r n , j v . t l U I ) for fomihevcRcr(aaln.tig aaui.UD)
initiate the outgoing connection. This will also perform an SDP lookup of the given uuid to determine which channel to connect to. The remote device will be authenticated and communication on this socket will be encrypted. Use this socket only if an authenticated socket link is possible. Authentication refers to the authentication of the link key to prevent man-in-the-middle type of attacks. For example, for Bluetooth 2.1 devices, if any of the devices does not have an input and output capability or just has the ability to display a numeric key, a secure socket connection is not possible. In such a case, use {#link createInsecureRfcommSocketToServiceRecord}. For more details, refer to the Security Model section 5.2 (vol 3) of Bluetooth Core Specification version 2.1 + EDR. Hint: If you are connecting to a Bluetooth serial board then try using the well-known SPP UUID 00001101-0000-1000-800000805F9B34FB. However if you are connecting to an Android peer then please generate your own unique UUID. Requires B U T O H LEOT
(rfrneadodMnfs.emsinhm#LEOT) /eeec/nri/aietpriso.tlBUTOH
Parameters
developer.android.com/reference/android/bluetooth/BluetoothDevice.html
12/16
uuid Returns
a RFCOMM BluetoothServerSocket ready for an outgoing connection Throws IOException on error, for example Bluetooth not available, or insufficient permissions
Describe the kinds of special objects contained in this Parcelable's marshalled representation. Returns a bitmask indicating the set of special object types marshalled by the Parcelable.
Compares this instance with the specified object and indicates if they are equal. In order to be equal, o must represent the same object as this instance using a class-specific comparison. The general contract is that this comparison should be reflexive, symmetric, and transitive. Also, no object reference other than null is equal to null. The default implementation returns t u only if t i = o See re hs = . Writing a correct e u l method qas (/reference/java/lang/Object.html#writing_equals) if you intend implementing your own e u l method. qas The general contract for the e u l and h s C d ( qas ahoe) ( r f r n e j v / a g O j c . t l h s C d ( )methods is that if /eeec/aaln/bethm#ahoe) e u l returns t u for any two objects, then h s C d ( must qas re ahoe) return the same value for these objects. This means that subclasses of O j c usually override either both methods or neither of them. bet Parameters o the object to compare this instance with.
Perform a service discovery on the remote device to get the UUIDs supported.
13/16
10/1/12
intent is sent, with the UUIDs supported by the remote end. If there is an error in getting the SDP records or if the process takes a long time, ATO_UD CINUI
(rfrneadodbutohButoheiehm#CINUI) /eeec/nri/leot/leotDvc.tlATO_UD
intent is sent with the UUIDs that is currently present in the cache. Clients should use the g t u d ( eUis)
(rfrneadodbutohButoheiehm#eUis) /eeec/nri/leot/leotDvc.tlgtud()
Returns False if the sanity check fails, True if the process of initiating an ACL connection to the remote device was started.
(rfrneadodMnfs.emsinhm#LEOT) /eeec/nri/aietpriso.tlBUTOH.
(rfrneadodbutohButoheiehm#ODNN) /eeec/nri/leot/leotDvc.tlBN_OE,
BN_ODN ODBNIG
(rfrneadodbutohButoheiehm#ODBNIG /eeec/nri/leot/leotDvc.tlBN_ODN )BN_ODD , ODBNE (rfrneadodbutohButoheiehm#ODBNE) /eeec/nri/leot/leotDvc.tlBN_ODD.
developer.android.com/reference/android/bluetooth/BluetoothDevice.html
10/1/12
Requires B U T O H LEOT
(rfrneadodMnfs.emsinhm#LEOT) /eeec/nri/aietpriso.tlBUTOH.
The local adapter will automatically retrieve remote names when performing a device scan, and will cache them. This method just returns the name for this device from the cache. Requires B U T O H LEOT
(rfrneadodMnfs.emsinhm#LEOT) /eeec/nri/aietpriso.tlBUTOH
Returns the supported features (UUIDs) of the remote device. This method does not start a service discovery procedure to retrieve the UUIDs from the remote device. Instead, the local cached copy of the service UUIDs are returned. Use f t h u d W t S p ) ecUisihd(
(rfrneadodbutohButoheiehm#ecUisi /eeec/nri/leot/leotDvc.tlfthudW t S p ) if hd()
Requires B U T O H LEOT
(rfrneadodMnfs.emsinhm#LEOT) /eeec/nri/aietpriso.tlBUTOH.
Returns the supported features (UUIDs) of the remote device, or null on error
Returns an integer hash code for this object. By contract, any two objects for which e u l ( b e t qasOjc)
(rfrnejv/agOjc.tleul(aaln.bet) /eeec/aaln/bethm#qasjv.agOjc)
returns t u must return the same hash code value. This means that re subclasses of O j c usually override both methods or neither bet method. Note that hash values must not change over time unless information
developer.android.com/reference/android/bluetooth/BluetoothDevice.html
15/16
10/1/12
used in equals comparisons also changes. See Writing a correct h s C d method ahoe
(/reference/java/lang/Object.html#writing_hashCode)
Currently this is the Bluetooth hardware address, for example "00:11:22:AA:BB:CC". However, you should always use g t d r s ( eAdes)
(rfrneadodbutohButoheiehm#eAdes) /eeec/nri/leot/leotDvc.tlgtdrs( )if
you explicitly require the Bluetooth hardware address in case the tSrn( otig)
(rfrneadodbutohButoheiehm#otig) /eeec/nri/leot/leotDvc.tltSrn()
The Parcel in which the object should be written. Additional flags about how the object should be written. May be 0 or P R E A L _ R T _ E U N V L E ACLBEWIERTR_AU.
developer.android.com/reference/android/bluetooth/BluetoothDevice.html
16/16