-
Notifications
You must be signed in to change notification settings - Fork 0
bluetooth

Bluetooth is a wireless technology standard for exchanging data over short distances.
- Suggested Reading
- Bluetooth - Basic concepts of Bluetooth
- Serial Communication – Bluetooth is like a RF version of serial communication.
- Hexadecimal – Bluetooth devices all have a unique address, which is usually presented as a hexadecimal value.


Android platform includes support for the Bluetooth framework that allows a device to wirelessly exchange data with other Bluetooth devices.
Android provides Bluetooth API to perform these different operations:
- Scan for other Bluetooth devices
- Query the local Bluetooth adapter for paired Bluetooth devices
- Establish RFCOMM channels
- Connect to other devices through service discovery
- Transfer data to and from other devices
- Manage multiple connections
You need to get Bluetooth adaptor:
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBluetoothAdapter == null) { // Device does not support Bluetooth }
If getDefaultAdapter() returns null, then the device does not support Bluetooth and your story ends here.
if (!mBluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); }
we can just call the method .disable() of bluetooth adaptor.
mBluetoothAdapter.disable();
you can query all paired devices and then show the name of each device to the user:
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); // If there are paired devices if (pairedDevices.size() > 0) { // Loop through paired devices for (BluetoothDevice device : pairedDevices) { // Add the name and address to an array adapter to show in a ListView mArrayAdapter.add(device.getName() + "\n" + device.getAddress()); } }
Utilize startActivityForResult(Intent, int) with the ACTION_REQUEST_DISCOVERABLE action Intent to issue a request to enable discoverable mode through the system settings.
By default, the device will become discoverable for 120 seconds. You can define a different duration by adding the EXTRA_DISCOVERABLE_DURATION Intent extra.
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); startActivity(discoverableIntent);