Chapter 17. Bluetooth

Bluetooth technology allows users to connect a variety of peripherals to a computer, tablet, or phone. Headsets, speakers, keyboards, and printers; medical devices such as glucometers and ECG machines; these are only some of the numerous types of devices that can be connected via Bluetooth. Some, such as headsets, are supported automatically by Android; more esoteric ones will require some programming. Some of these other devices use the Serial Port Protocol (SPP), which is basically an unstructured protocol that requires you to write code to format data yourself.

This chapter has recipes on how to ensure that Bluetooth is turned on, how to make your device discoverable, how to discover other devices, and how to read from and write to another device over a Bluetooth connection.1

A future edition of this work will provide coverage of the Bluetooth Health Device Profile (HDP) standardized by the Continua Health Alliance.

17.1 Enabling Bluetooth and Making the Device Discoverable

Rachee Singh

Problem

The application requires that the Bluetooth adapter be switched on, so you need to check if this capability is enabled. If it isn’t, you need to prompt the user to enable Bluetooth. To allow remote devices to detect the host device, you must make the host device discoverable.

Solution

Use Intents to prompt the user to enable Bluetooth and make the device discoverable.

Discussion

Before performing any action with an instance of the BluetoothAdapter class, ...

Get Android Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.