Understanding the BC127 Bluetooth Module

Pages
Contributors: SFUptownMaker
Favorited Favorite 7

BC127 Profiles

The BC127 supports several different "profiles", which are connection types specified in the Bluetooth standard. These profiles determine the capabilities of the device and how the device responds to input.

General Connection Information

Connections between devices are managed differently when the connecting device is Bluetooth Classic or Bluetooth 4.0 (also referred to as Bluetooth Low Energy, or BLE).


Classic Mode

Here are useful commands for Classic mode connection control:

alt text

CLOSE disconnects from one of the links returned by STATUS. Pass as a parameter either a single digit corresponding to the link ID returned by STATUS or ALL, to disconnect all connections. Note that it is possible to disconnect a single profile connection but remain connected to a device with other profiles. Also note that, so long as the parameter passed is valid, this command will return OK, whether there is a connection with that link ID or not.

As you can see above, there are two responses from this command: OK, which indicates that the command was received locally successfully (i.e., no typos) and CLOSE_OK SPP, which tells you that the remote device was disconnected successfully. If there had been no connection open with link ID 0, the CLOSE_OK message would not have appeared.

DISCOVERABLE mode makes the device discoverable by other Bluetooth Classic devices in range (ON). The device will automatically switch to OFF when a connection is made; executing DISCOVERABLE ON will disconnect any existing connections.

alt text

INQUIRY looks for local Bluetooth Classic devices that are discoverable. Requires an integer parameter between 1 and 48; that value determines the length of time the inquiry will last; the timeout will be equal to 1.28s times the passed value.

The response is as seen above. The INQUIRY string denotes that a device has been found. It is followed by 12 hexadecimal digits, which are the address of the device, and then six more hexadecimal digits conveying the device's capabilities. This six-digit value, the Class of Device value, is generally beyond the scope of this document. Finally, you'll see a signal strength indicator. Anything above -70db is probably strong enough to make a good connection.

Once the OK response string has been received, the inquiry process is complete.

LIST prints a list of all devices currently paired with this module, whether they are connected or not. The BC127 can store up to 8 devices in this list. Devices in this list persist through resets or power cycles even without a WRITE having been executed

alt text

OPEN establishes a connection to a device at a given address of a given type. The above example shows a connection of type Serial Port Protocol with a device at address 20FABB0101CD. Note that all of the letters in the address and connection type must be in all caps!

The responses for this can vary. A successful connection will return the above messages: PAIR_PENDING indicates that the device was found, PAIR_OK followed by an address indicates that the pairing process completed successfully, and OPEN_OK indicates that the connection with the stated profile was successful. If there is an error in the command syntax (i.e., not enough characters for a valid Bluetooth address, or invalid profile type), the response will be ERROR. If no device at that address is found, you'll receive OPEN_ERROR. If the device is not accepting connections of the specified profile, you will get the PAIR_OK message, but you'll see the OPEN_ERROR message for the connection.

Available connection types are A2DP, AVRCP, MAP, HFP, PBAP, SPP and BLE. We'll talk about these connection types (usually referred to as "Profiles") later on in this tutorial.

RSSI returns the current link's signal strength.

alt text

STATUS returns information about the current state of the module. As you can see above, the first line of the response is STATE followed by some information. Options here are CONNECTED, DISCOVERABLE, CONNECTABLE, or ADVERTISING. DISCOVERABLE and CONNECTABLE may both be returned at once.

If the module is CONNECTED, a list of links will follow the STATE response. Each link is given a reference ID, status, profile type, and address of connected device. In the above example, we have one connection: Link ID 0 is connected with serial port profile to a device at address 20FABB0101CD.

UNPAIR clears the list of paired devices. This does not affect the remote devices' idea of what they are paired with, however.

These are parameters which are useful for Classic connections. To change or view them, use the SET and GET commands as detailed on the previous page.

AUTOCONN=value controls the automatic connection of paired devices upon reset or power up. When value is 0, the BC127 will not attempt to connected to any previously paired devices at startup. When value is 1, the BC127 will attempt each of the paired devices (as reported by the LIST command; the number of attempts to connect to each device is set in the MAX_REC parameter. When value is 2, the BC127 will attempt to connect to a device whose address is stored in parameter REMOTE_ADDR on startup. To change this value, a WRITE RESET cycle is required.

CLASSIC_ROLE=value determines the role of the device: 0 for sink, 1 for source. This becomes important when connecting BC127 devices together for audio purposes; it determines which device sends audio and which receives.

DISCOVERABLE=value timeout sets the devices mode on startup. This is different to the DISCOVERABLE command! The command of the same name controls the immediate discoverability of the module; this sets the default state on startup.

0 makes the device undiscoverable on boot. 1 makes the device discoverable on boot, but only if it fails to autoconnect to one of its paired devices. 2 makes it immediately discoverable and overrides the autoconnect setting entirely. The value of timeout determines how long it will remain in discoverable mode before going to power save mode, in milliseconds; set that value to 0 for infinite wait.

LOCAL_ADDR=value prints the modules address to the serial port. Any attempt to change this will generate and error.

MAX_REC=value sets the number of times to attempt to reconnect to each paired device in the list of paired devices when autoconnecting. Defaults to 2 times.

REMOTE_ADDR=value provides a single address to attempt to reconnect to at startup (assuming AUTOCONN=2). The BC127 will attempt to connect MAX_REC times.

RSSI_THRESH=value puts a bottom limit to the signal strength the module will accept as a solid connection. Default is -75db.


Bluetooth 4.0 Mode

Many of those same settings and commands can be applied to Bluetooth 4.0 connections; here are the BLE-specific commands:

ADVERTISING value can be passed ON or OFF to start and stop BLE advertising. The BLE_ROLE value must be 1 for this to work; we'll discuss that later.

alt text

SCAN is equivalent to INQUIRY for Bluetooth 4.0 devices. Note that before executing SCAN, the module must be made into a Bluetooth 4.0 central device by setting parameter BLE_ROLE to 2, writing that value to non-volatile memory, and resetting the device.

The response is very similar to that for INQUIRY; the first field of the response is the address, the second field is the "short name" of the device, the third gives information about the device (0A indicates a general purpose dual-mode device; 02 would be a single mode device), and the fourth is the received signal strength.

SEND BLE data transmits data across the BLE link. The BLE connection does not currently support data mode; data can only be sent and received in command mode via the SEND command.

There is only one parameter associated with Bluetooth 4.0 connection only:

BLE_ROLE=value sets the type of device the BC127 will function as. SCAN can only be performed if BLE_ROLE=2; ADVERTISING will only work if BLE_ROLE=1. BLE can be disabled by setting this parameter to 0.

As we discuss individual connection protocols, we'll add more information as required for each one.


Serial Port Protocol

Most of the tinkering with Bluetooth devices in the past has focused on SPP- the Serial Port Protocol. In this mode, the device emulates a physical serial port, passing data (and, potentially, handshaking signals) across the ether as though it were a physical, wired connection.

There are several commands associated with SPP connections:

$$$$ will cause a BC127 in data mode to exit data mode and return to command mode. In order for this to work, however, there must be a guard time before the first character and after the last character; this guard time is set in paramter CMD_TO and will be discussed in the parameters section.

ENTER_DATA will put the device in data mode; any serial data coming into the serial port will be transparently forwarded to the remote device.

SEND SPP string will cause string (or whatever other data preceds the carriage return at the end of the command) to be sent over the serial connection. This allows data to be sent without entering data mode. If data is received and the module is not in data mode, the data will be prepended with RECV SPP.

There are quite a few parameters which can be set to allow for very flexible connections with the BC127 over serial:

BAUD=value sets the baud rate of the incoming and outgoing serial data. Valid values are 9600, 19200, 38400, 57600, 115200, 230400, 460800 and 921200; modules default to 9600 when set to factory defaults.

CMD_TO=value controls the guard time for detecting the $$$$ command to exit data mode. There should be a 20ms * value break in serial data before and after the $$$$ for it to be recognized; failure to respect this guard time will result in a string of four dollar signs being sent rather than causing the device to exit data mode.

ENABLE_SPP=value allows the user to selectively disable connections over SPP. Default is ON; any change in state requires a WRITE RESET to take effect.

FLOW_CTRL=value enables and disables the use of the hardware flow control pins on the BC127. Requires WRITE RESET to take effect.

PARITY=value enables and disables a parity bit for the serial data, where 1 is no parity (default), 2 is even parity, and 3 is odd parity.

SPP_TRANSPARENT=value determines whether a new SPP connection immediately activates data mode or not. OFF by default, and requires WRITE RESET for any change to take effect.


AVRCP AND A2DP Protocols

A2DP stands for Advanced Audio Distribution Profile; AVRCP is the Audio/Visual Remote Control Profile. The two are intricately related; you cannot have an AVRCP connection open without an A2DP connection open as well. You can, however, have only an A2DP connection open.

A2DP is a two-channel audio streaming protocol, which transfers data from a source to a sink. The source can be a cell phone, a media player, or the audio input on another BC127 (or similar) module. AVRCP allows the BC127 to send commands to the remote device which will cause the remote device to play, pause, stop, advance or reverse playback; some remote devices will transmit track meta data back to the BC127 via the AVRCP protocol.

The BC127 also supports the Hands-Free Protocol (HFP); we'll talk about that later, but for now be aware that incoming calls over the HFP connection will override data on the A2DP connection.

Here are the commands you'll need to know to get A2DP and AVRCP working properly:

MUSIC instruction sends a command over the AVRCP link to the A2DP source device. Valid options for instruction are PLAY, PAUSE, STOP, FORWARD and BACKWARD.

VOLUME flag has different options depending on whether the device is a sink or source (as defined by the CLASSIC_ROLE parameter discussed below). If the device is a sink, the local volume will change, but no remote volume change will occur. If the device is a source, the volume at the remote device will change. Valid values for flag are UP, DOWN, and A2DP; A2DP will report the volume level set for the A2DP speaker.

If flag is formatted as A2DP=value, that will set the volume as above to a value between 0 and 15.

These are the parameters governing A2DP/AVRCP connections:

AUDIO=value routes the A2DP audio stream from various sources/sinks. Options are 0 for analog (default), 1 for PCM Master, 2 for PCM Slave, 3 for I2S Master, 4 for I2S Slave, and 5 for SPDIF. WRITE RESET required after changes.

BPS=value sets the bits per sample for I2S and PCM codecs. Valid values are 8, 13, and 16 for PCM and 16 and 24 for I2S. WRITE RESET required after changes.

CLASSIC_ROLE=value allows user to set the device to source (1) or sink (0). This is important when attempting to make a connection between two BC127 modules, as it determines which module is expecting to send audio data and commands and which is expecting to receive. We'll cover that in an example, later. WRITE RESET required after changes.

ENABLE_A2DP=value and ENABLE_AVRCP=value allow the user to selectively enable and disable connections over A2DP and AVRCP. WRITE RESET required after changes.

I2S=value controls various parameters of the I2S system See page 15 of the BC127 manual for details on this. WRITE RESET required after changes.

INPUT_GAIN=value controls the gain level of the analog input to the device. Supports values between 0 and 15; the default value is reported as 16, although it is in fact 15.

On the next page, we'll walk through examples of interfacing the BC127 with a couple of different audio sources.


HFP and PBAP Protocols

HFP (Hands-Free Protocol) and PBAP (Phone Book Access Protocol) are implemented to support connections to cellular phones. They are related, in that most devices that support one will support both, but are not as closely related as AVRCP and A2DP.

HFP allows you to make and receive phone calls using a BC127 module. If an A2DP link is present as well as the HFP link, the HFP link will override the A2DP data stream when a call is received.

Here's a list of commands for accessing the HFP and PBAP functionality:

ANSWER accepts the incoming call on the HFP link.

CALL number initiates a call on the HFP link; if two BC127 modules are connected, this will create a bidirectional HFP link (i.e., walkie-talkie).

END hangs up an active call.

PULL_PBOOK option initiates retrieval of the phone's phonebook, call list or some subset thereof. If no option is passed, or the option PHONEBOOK is passed, the phonebook will be downloaded. For call lists, the options are OUTGOING, INCOMING, MISSED, or COMBINED. The response for each item in the list takes the following format:

PBAP_PB NAME: name
PBAP_PB TEL: number

Once all requested data has been delivered, the message PBAP_PB OK will be sent.

PULL_ABORT stops a phonebook download in progress; PBAP profile connections cannot be close during an active phonebook pull.

REJECT will cause an incoming call to be rejected.

TOGGLE_VR will activate voice recognition dialing on the phone.

TRANSFER_CALL sends audio back to the phone. This may cause the phone to drop the Bluetooth connection; that varies with phone model.

VOLUME flag allows for control of the speaker volume. Flag values UP and DOWN control speaker volume; HFP will return the current HFP speaker volume. MIC_MUTE reports the mute state of the microphone.

If the flag takes the form flag=value, the HFP volume can be adjusted independently of the A2DP stream, and MIC_MUTE=ON will mute the microphone.

There are some parameters controlling HFP and PBAP connections, as well:

ENABLE_HFP=value and ENABLE_PBAP=value can be ON or OFF to allow or disallow HFP and PBAP connections. WRITE RESET required after changes.

FORCE_ANALOG_MIC=value forces the use of the analog input in HFP even if a digital audio interface is selected. WRITE RESET required after changes.


Up Next: Usage Examples!

Now we're going to explore how to connect BC127 modules to various targets, including a cell phone, another BC127 module and a generic Bluetooth module as serial port.