The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


Basic Bluetooth RFCOMM

For short-range communications, Bluetooth is fairly popular. It is widespread, available on mobile devices, notebooks, and many Internet of Things platforms. It performs reasonably well, at least for moderate amounts of data. Android has a variety of classes in the Android SDK for adding Bluetooth communications to an app.

However, Bluetooth overall is a vast topic. The documentation for the Android SDK classes is spotty. And it can be fairly difficult to make sense of how all the different pieces are supposed to plug in together.

In this chapter, we will explore a sample app that demonstrates Bluetooth communications between two Android devices and use that to see how to work with Bluetooth on Android. For extra fun, we will also peek a bit at how things differ when you try to use Bluetooth on an Android Things device, such as a Raspberry Pi.

Prerequisites

This chapter makes use of RxJava, foreground services, RecyclerView, and data binding.

If you want to run the sample app, you will need two Android 5.0+ devices, each with working Bluetooth.

A Quick Bit of Scope

As mentioned, Bluetooth is vast, much more than can be covered in a single chapter.

This chapter will focus on Bluetooth, not Bluetooth Low Energy (BLE). When most people think of Bluetooth, they are thinking of “full” Bluetooth. BLE is designed for low-power environments and lower data throughput.

This chapter will focus on RFCOMM. Bluetooth is based around “profiles”, which describe particular standards of data exchange between parties. If you think of Bluetooth as being HTTPS, a Bluetooth profile is a particular Web service API. RFCOMM is a general-purpose mechanism designed for communications that fall outside any standard profile.

And, this chapter will focus on one particular recipe for using Bluetooth. As with many of the book examples, the code shown here is not bulletproof, but is here to illustrate the use of various APIs and concepts. A production-grade app will need to handle concerns that lie outside the scope of the chapter, such as:

About the Sample App

The preview of this section was last seen in the Bermuda Triangle.

Bluetooth and Permissions

The preview of this section is sleeping in.

The Rx for Your Bluetooth

The preview of this section is off trying to sweet-talk the Khaleesi into providing us with a dragon.

I Can Haz Bluetooth?

The preview of this section is out seeking fame and fortune as the Dread Pirate Roberts.

I Feel a Bond Between Us

The preview of this section is in an invisible, microscopic font.

A Voyage of Discovery

The preview of this section may contain nuts.

Serving and Shouting

The preview of this section was eaten by a grue.

Reach Out and Touch Someone

The preview of this section was the victim of a MITM ('Martian in the middle') attack.

Ping and Pong

The preview of this section was stepped on by Godzilla.

Differences with Android Things

The preview of this section was stepped on by Godzilla.