1<HTML> 2<BODY> 3Provides classes that manage Bluetooth functionality, such as scanning for 4devices, connecting with devices, and managing data transfer between devices. 5 6<p>The Bluetooth APIs let applications:</p> 7<ul> 8 <li>Scan for other Bluetooth devices</li> 9 <li>Query the local Bluetooth adapter for paired Bluetooth devices</li> 10 <li>Establish RFCOMM channels/sockets</li> 11 <li>Connect to specified sockets on other devices</li> 12 <li>Transfer data to and from other devices</li> 13</ul> 14 15<p class="note"><strong>Note:</strong> 16To perform Bluetooth communication using these APIs, an application must 17declare the {@link android.Manifest.permission#BLUETOOTH} permission. Some 18additional functionality, such as requesting device discovery and 19pairing also requires the {@link android.Manifest.permission#BLUETOOTH_ADMIN} 20permission. 21</p> 22 23<h3>Overview</h3> 24 25<p>Here's a basic introduction to the Bluetooth classes:</p> 26<dl> 27 <dt>{@link android.bluetooth.BluetoothAdapter}</dt> 28 <dd>This represents the local Bluetooth adapter, which is essentially the 29 entry-point to performing any interaction with Bluetooth. With it, you can 30 discover other Bluetooth devices, query a list of bonded (paired) devices, 31 initialize a {@link android.bluetooth.BluetoothDevice} using a known MAC 32 address, and create a {@link android.bluetooth.BluetoothServerSocket} to 33 listen for communications from other devices.</dd> 34 35 <dt>{@link android.bluetooth.BluetoothDevice}</dt> 36 <dd>This represents a remote Bluetooth device. Use this to request a 37 connection with a remote device through a 38 {@link android.bluetooth.BluetoothSocket} 39 or query information about the device such as its name, address, class, and 40 bonding state.</dd> 41 42 <dt>{@link android.bluetooth.BluetoothSocket}</dt> 43 <dd>This represents the interface for a Bluetooth socket 44 (similar to a TCP client-side {@link java.net.Socket}). This is the 45 connection point that allows an app to transfer data with another Bluetooth 46 device via {@link java.io.InputStream} and {@link java.io.OutputStream}.</dd> 47 <dt>{@link android.bluetooth.BluetoothServerSocket}</dt> 48 49 <dd>This represents an open server socket that listens for incoming requests 50 (similar to a TCP server-side {@link java.net.ServerSocket}). 51 When attempting to connect two Android devices, one device will need to open 52 a server socket with this class. When a connection is accepted, a new 53 {@link android.bluetooth.BluetoothSocket} will be returned, 54 which can be used to manage the connection and transfer data.</dd> 55 56 <dt>{@link android.bluetooth.BluetoothClass}</dt> 57 <dd>This represents the Bluetooth class for a device which describes general 58 characteristics and capabilities of a device. This class and its subclasses 59 don't provide any actual functionality. The sub-classes are entirely composed 60 of constants for the device and service class definitions.</dd> 61</dl> 62 63 64<h3>Example Procedure</h3> 65 66<p>For example, here's an pseudo-code procedure for discovering and 67connecting a remote device, and transfering data:</p> 68 69<ol> 70 <li>Register a {@link android.content.BroadcastReceiver} that accepts the 71 {@link android.bluetooth.BluetoothDevice#ACTION_FOUND} Intent.</li> 72 <li>Call {@link android.bluetooth.BluetoothAdapter#getDefaultAdapter} to 73 retrieve the Android system's local 74 {@link android.bluetooth.BluetoothAdapter}.</li> 75 <li>Call {@link android.bluetooth.BluetoothAdapter#startDiscovery() 76 BluetoothAdapter.startDiscovery()} to scan for local devices. This is where 77 the BroadcastReceiver comes in; Android now scans for devices and will 78 broadcast the {@link android.bluetooth.BluetoothDevice#ACTION_FOUND} Intent 79 for each remote device discovered. The 80 {@link android.content.BroadcastReceiver} 81 you created will receive each Intent.</li> 82 <li>The {@link android.bluetooth.BluetoothDevice#ACTION_FOUND} Intent 83 includes the {@link android.bluetooth.BluetoothDevice#EXTRA_DEVICE} 84 Parcelable extra, which is a {@link android.bluetooth.BluetoothDevice} 85 object. Extract this from the Intent and call 86 {@link android.bluetooth.BluetoothDevice#createRfcommSocketToServiceRecord(java.util.UUID) 87 BluetoothDevice.createRfcommSocketToServiceRecord()} 88 to open a {@link android.bluetooth.BluetoothSocket} with a chosen 89 remote device.</li> 90 <li>Call {@link android.bluetooth.BluetoothSocket#connect() 91 BluetoothSocket.connect()} to connect with the remote device.</li> 92 <li>When successfully connected, call 93 {@link android.bluetooth.BluetoothSocket#getInputStream() 94 BluetoothSocket.getInputStream()} and/or 95 {@link android.bluetooth.BluetoothSocket#getOutputStream() 96 BluetoothSocket.getOutputStream()} to retreive an 97 {@link java.io.InputStream} and {@link java.io.OutputStream}, respectively, 98 which are hooked into the socket.</li> 99 <li>Use {@link java.io.InputStream#read(byte[]) InputStream.read()} and 100 {@link java.io.OutputStream#write(byte[]) OutputStream.write()} to transfer 101 data.</li> 102</ol> 103 104 105 106<p class="note"><strong>Note:</strong> 107Not all Android devices are guaranteed to have Bluetooth functionality.</p> 108</BODY> 109</HTML> 110