This document discusses building an Android application to connect to Bluetooth devices. It covers the basics of the Android Bluetooth APIs, including scanning for devices, querying paired devices, establishing connections, and transferring data. It also discusses the permissions needed, setting up Bluetooth on an Android device, managing connections, and using handlers. The document mentions connecting an Android phone to an Arduino using an RN42 Bluetooth module and a FlexiForce sensor.
3. INTRODUCTION
Using the Bluetooth APIs, an Android application can perform
the following:
•Scan for other Bluetooth devices.
•Query the local Bluetooth adapter for paired Bluetooth devices.
•Establish RFCOMM channels.
•Connect to other devices through discovery.
•Transfer data to and from other devices.
4. BLUETOOTH BAsICs
All Bluetooth API’s are present in android.bluetooth package.
1.Bluetooth Adapter: It’s the entry-point for all Bluetooth
interaction.
2.Bluetooth Device: It is used to request a connection with a
remote device through a BluetoothSocket or query information
about the device such as its name, address, class, and bonding
state.
3.Bluetooth Socket: This is the connection point that allows an
application to exchange data with another Bluetooth device via
InputStream and OutputStream.
5. Cont..
4.
Bluetooth Server socket: Represents an open server socket that
listens for incoming requests.
5.
Bluetooth Class: Describes the general characteristics and
capabilities of a Bluetooth device.
6. Bluetooth Permission
We need this permission to perform any Bluetooth
communication e.g. requesting a connection, accepting a
connection and transferring data.
If you want your app to initiate device discovery or
manipulate Bluetooth settings, you must also declare the
BLUETOOTH_ADMIN permission.
8. setting uP the Bluetooth
Before your app can communicate over Bluetooth verify that Bluetooth is
supported on your mobile.
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
if(btAdapter==null) {
errorExit("Fatal Error", "Bluetooth not support");
} else {
if (btAdapter.isEnabled()) {
//promt user that the Bluetooth is already On
} else {
//Prompt user to turn on Bluetooth
Intent enableBtIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}
}
9. Managing a connection
The ConnectThread is a inner class, it passes a message to the
Handler which then carries out the required action
11. Handler
Allows you to send and process message and runnable object
associated with a thread’s Message queue.
The main uses of Handler are:1.To schedule messages that can be executed as some point in the
future.
2.To Enqueue an action to be performed on a different thread.