Below are the tasks intended in this sample
- Enable bluetooth on a device
- Display a list of paired devices
- Discover and list nearest Bluetooth devices
- Connect and send data to another bluetooth device
There are two kinds of permissions
1. BLUETOOTH => used to connect, disconnect and transfer data with another bluetooth device
2. BLUTOOTH_ADMIN => allows to discover new bluetooth device and change device’s bluetooth settings
Application will need to use Bluetooth adapter for interfacing with the Bluetooth.
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
if the Adapter is returned to be null, then the device does not have the Bluetooth feature
Application can get to know if the Bluetooth is enabled or not by using the API isEnabled. If not
enabled, it can enable it by using an intent provided by Android
if(!btAdapter.isEnabled())
{
Intent enableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBT,REQUEST_BLUETOOTH);
}
A bluetooth device can be in any of the below states from perspective of Mobile Device
- Unknown
- paired
- connected
Paired devices know each other’s existence and share a link key, which can be used to authenticate, resulting in a
connection. Devices are automatically paired once encrypted connection is established.
Connected devices share RFCOMM channel, allowing them to send and receive data. A device may have many paired devices
but it can have only one connected devices at a time.
References:
No comments:
Post a Comment