BC118 BLE Mate 2 Hookup Guide
While you can just skip over this page and go straight to the code example and library documentation, it's probably a good idea to at least skim it so you have some idea what's going on under the hood.
The BC118 realizes a custom BLE profile, so you'll have to take that into account when developing an app for it. It also means you won't be able to easily connect it to any other BLE modules other than another BC118.
Generally, when you change settings on the BC118, you need to write the settings to the onboard non-volatile memory and reset the module before they will take effect. When in doubt, write/reset--it's the only way to be sure.
When the module boots in Central mode, it will immediately begin scanning and will scan until it reaches the timeout period (which may be never). To connect to a Peripheral devices, the BC118 must be in Central mode, and scanning. This makes programmatically detecting a successful connection difficult, as the acknowledgment of the connection is buried in a stream of detected devices. The BLE Mate 2 library takes care of that for you.
Communication With the Module
From the factory, the BC118 comes programmed to accept and transmit via the UART at 9600bps. There are two types of transactions the user can initiate with the module: Commands and setting/getting parameters.
The BC118 expects a carriage return ('\r') at the end of a command string; if you send a newline ('\n')(which is frequently a standard practice in serial communications; the Arduino println() function sends "\n\r" at the end of the transmission), that will cause a receive error.
All responses from the BC118 will be ended with "\n\r", however, which makes that a good string pattern to recognize for detecting responses from the module. If the module was unable to parse the string between the last two carriage returns into a command, it will respond with "ERR\n\r". At that point, the buffer is clear, and that fact can be a useful way to get to a known state for re-synchronizing user code with the buffer in the BC118.
Finally, regarding "transparent" mode: it's possible to put the module into transparent mode, where it forwards data presented on the UART directly to the remote device, and forwards data received from the remote device directly to the UART. Unfortunately, once transparent mode has been activated, there's no escape sequence that can be sent across the UART to return to data mode. GPIO8 and AIO1 can be used to exit transparent mode, but only if that function has been enabled (by setting the parameters GPIO and ACFG to ON, respectively). If those parameters haven't been set, and transparent mode is entered, the only way out is to power cycle the module.
Here's a list of useful commands. For more information, you can refer to the Melody Smart User Manual on the BlueCreation website.
Commands will be presented with the command first, then parameters to be passed afterwards in parentheses after the command, but the parentheses are not part of the command. Optional parameters will be in brackets. A pipe ('|') will be used to separate "choose one of these" parameters.
- ADV (ON|OFF) - Turn advertising on or off. While this can be toggled without error in Central mode, it will have no effect.
- AIO (0|1|2) - Report the analog voltage, in mV, on the respective analog input pin.
- CON [(BT ADDRESS) (TYPE)] **- **Device must be in scan mode for this command to work! If parameter ACON is '1', no parameter needs to be passed to this command and the module will connect to the first target that supports the Melody Smart protocol (i.e., another BC118 or an app you've written on a target device). Otherwise, BT ADDRESS should be the full 12-character hex address of the device to connect to, and type should be 0 for a public address (most common) or 1 for a private address.
- DCN - Disconnect from remote device. Works in either peripheral or central mode.
- HIB (timeout) - The "timeout" parameter should be an integer value between 1050 and 429496795, and represents the number of 1.024ms periods to elapse before the device emerges from hibernation. Restarting from hibernation is a clean reboot to stored settings.
- GET (parameter) - Returns the value of (parameter), which can be any of the configuration parameters listed below.
- RST - Resets the chip, loading all settings from non-volatile memory.
- RTR - Restores all settings to factory defaults.
- SCN (ON|OFF) - Enable or disable scanning.
- SET (parameter)=(value) - No spaces around the equal sign! Set (parameter) to (value). Settings changed in this way will not persist through a reset or power cycle unless the WRT command is issued (see below).
- SND (data) - Send a stream of binary data. At most, 20 characters can be sent when in Central mode and 125 in Peripheral mode, and the data must not contain a newline ('\r' or 0x0D) as that will trigger transmission.
- STS - Return the status of the module.
- VER - Return the version of Melody Smart on the current module, and the module's 12-character Bluetooth address.
- WRT - Save the current settings to non-volatile memory, causing them to persist after a reset or power cycle. Note that many settings require a WRT/RST cycle to actually take effect!
In addition to the commands listed above, there are some parameters governing the behavior of the module which can be set or checked via the SET and GET commands mentioned above. Here are some of the most important ones.
- ACFG=(ON|OFF) - When OFF, AIO pins are available as analog inputs. When ON, they can be used for control and reporting (see the "Hardware Connection" page for details).
- ACON=(ON|OFF) - When ON, the module will connect to the first discovered compatible device. This may not be the device you want to connect to!
- ADVC=(ON|OFF) - When ON, the device advertises constantly if not connected.
- ADVP=(SLOW|FAST) - Advertising rate. Using SLOW will consume less power but make connecting to the module slower.
- ADVT=(timeout) - How long the device will advertise after advertising begins (either at boot or due to the ADV command). Values range from 0 (forever) to 4260, and are integer representations of seconds.
- CENT=(ON|OFF) - Enable or disable Central mode. Must WRT/RST to make this take effect!
- CCON=(ON|OFF) - Should the device scan/advertise upon disconnection?
- CONP=(max_conn_int) (min_conn_int) (latency) (timeout) - Settings this device will send to a Central connecting to it. (max_conn_int) must be greater than (min_conn_int), and both must be between 6 and 3200 and represent the connection interval in units of 1.25ms each. They represent the longest and shortest intervals between which the Central device will ask for data. (latency) is in terms of connection intervals and represents the connection slave latency, which is the number of periods the Peripheral will ignore the Central if it has no new data. Finally (timeout) is the connection supervision timeout, the longest period the Central device should wait before declaring the link lost. It's an integer, in units of 10ms.