Working With Bluetooth

Enabling Bluetooth

First thing we need to work with Bluetooth is Bluetooth Adapter which allows us to work with Bluetooth, the same way we use Location Manager to work with Locations.

Set the Bluetooth Adapter variable in  the MainActivity class and let’s call it BA, as follows:

BluetoothAdapter BA;

Next we have to check whether the user has enabled Bluetooth or not. And if they didn’t, we attempt to turn it on. Bluetooth Adapter has a builtin method called isEnabled. It is a boolean, so we can test if device’s Bluetooth is enabled by using if condition. If it’s enabled we just display a Toast otherwise create a new Intent to request enabling Bluetooth using BluetoothAdapter. Then just to confirm we can do another test using the same isEnabled method and see if it’s now turned on.

if(BA.isEnabled()) {
    Toast.makeText(getApplicationContext(), "Bluetooth is enabled", Toast.LENGTH_SHORT).show();
} else {
    Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivity(i);

    if(BA.isEnabled()) {
        Toast.makeText(getApplicationContext(), "Bluetooth turned on", Toast.LENGTH_SHORT).show();
    }
}

Add the following code in AndroidManifest.xml file to enable Bluetooth permission.

<uses-permission android:name="android.permission.BLUETOOTH" />

Advanced Features

Add three buttons in the layout namely, Turn Bluetooth Off, Discover Bluetooth Devices, Show Paired Devices. We will also need to add a listView in the layout so we can see the list of paired devices.

Turn Bluetooth Off:

Turing off Bluetooth is fairly easy and can be achieved in just one line. But of course we need to first call the method that was assigned to this button view, turnBluetoothOff.

public void turnBluetoothOff(View view) {
        BA.disable();
        if(BA.isEnabled()) {
            Toast.makeText(getApplicationContext(), "Bluetooth could not be disabled", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getApplicationContext(), "Bluetooth turned off", Toast.LENGTH_SHORT).show();
        }
    }

It needs admin permissions to access this feature so add the following into the AndroidManifest.xml file.

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

Discover Bluetooth Devices:

We will write a new Intent to display discoverable devices. BluetoothAdapter has a method to request discoverable devices. It also manages pairing of the devices so we do not have to manually pair them.  Call the discoverableDevices method attached to the onClick listener of this button view and start the intent.

public void discoverableDevices(View view) {
    Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    startActivity(i);
}

Show Paired Devices:

To display paired devices we are going to need a Set which is similar to a list. The subtle difference between a list and a set is that a list is an ordered collection (sequence). The elements of a list can be searched by an integer index whereas a set is a collection that has no duplicate elements. So, call the showPairedDevices method attached to this button view and use set to contain all the paired devices. Define a Set and name it as pairedDevices and assign it to a method called getBondedDevices to capture all the paired devices. Now to display the paired devices define a ListView can call it pairedDevicesListView. A Set cannot be recognized by a ListView so convert the Set to ArrayList. Do define an ArrayList and call it pairedDevicesArrayList. Now, to convert the Set to an ArrayList, loop through each of the pairedDevices Set and add them to the ArrayList. Finally, create an ArrayAdapter to set the ArrayList paredDevicesArrayList to the ListView.

public void showPairedDevices(View view) {
    Set<BluetoothDevice> pairedDevices = BA.getBondedDevices();
    ListView pairedDevicesListView = (ListView) findViewById(R.id.pairedDevicesListView);
    ArrayList pairedDevicesArrayList = new ArrayList();

    for (BluetoothDevice bluetoothDevice : pairedDevices) {
        pairedDevicesArrayList.add(bluetoothDevice.getName());
    }

    ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, pairedDevicesArrayList);
    pairedDevicesListView.setAdapter(arrayAdapter);
}

 

Conclusion:

These are just some examples of how to work with Bluetooth settings in Android Marshmallow and Nougat. There are lot of other features as well that you can play with like sending files, retrieving data, etc. But the features are very specific to the type of device that you want to connect to like a smart watch or smart TV or audio system or car or anything else that has Bluetooth.

Note: These Bluetooth settings can’t be run on emulator so you need a physical device to test these features. Good luck with that!

Leave a Reply

Your email address will not be published. Required fields are marked *