Filter results by

Bluetooth

All ARTIK product families offer Bluetooth® / Bluetooth LE ("Bluetooth Smart®") capabilities.

  • ARTIK 5, 7, and 10 modules incorporate both Bluetooth and Bluetooth LE support. They typically act as a central (controller) device, exchanging data with Bluetooth and Bluetooth LE peripheral devices as well as with network-connected (IP) devices. All software is built-in as part of the Fedora® Linux® installation.

  • ARTIK 020 in the ARTIK 0 family supports Bluetooth LE. It typically implements a low-cost, low-power peripheral device. Its Bluetooth software stack is part of the Simplicity Studio® 4.0 development environment. Download and follow the ARTIK 020 Software User's Guide.

In this article, we first discuss configuring and using Bluetooth, and later describe the procedures for development of code for controller and peripheral device applications.

Bluetooth Pairing

Bluetooth and Bluetooth LE scanning and pairing operations are executed from the Linux command line using the bluetoothctl application. Once paired, connmanctl is used to create connections.

A typical pairing sequence looks like this on the screen.

  [root@localhost /]# bluetoothctl  
  [bluetoothctl]# pair _mac_address_of_XX-device_
  [bluetoothctl]# connect _mac_address_of_XX-device_
  [XX-device]# exit

The procedure varies slightly according to the Bluetooth "profile". Examples for some profiles are shown below. For brevity, the [bluetoothctl]# prompts are shown simply as # and the regular Linux prompts are omitted.

For detailed screen output examples, and for information on executing these operations programmatically, refer to the Programming section below.

As noted above, Bluetooth devices are paired through a series of commands and actions on each device. At its simplest, you just give each device the MAC address of the other.

If you already know the MAC addresses of the devices you want to pair, you may be able to skip the Discovery steps below.

Preparation

On the device you want to pair with an ARTIK central device:

  • Make the device visible to other Bluetooth devices. The process is device-specific – calling up a menu or holding a button to put the device into "discoverable" mode.

On the ARTIK controller board:

  • Start the control program to look for available devices.
    bluetoothctl

Discovery

  1. Enable an 'agent' to auto-negotiate a pairing passkey. You can skip this step if you do not need a passkey, but doing so may leave the connection vulnerable to hacking.

    # agent on
    # default-agent

  2. Discover nearby devices.
    # scan on

    The scan should show the device and its MAC address in the list. You can type scan off to stop the discovery once you have located your device.

Pairing

  1. Pair the ARTIK controller with your device.
    # pair _mac_address_of_device_

  2. Confirm passkeys. If you used an agent, the ARTIK controller and the peripheral device will ask you to confirm that the passkeys match. If you agree, the pairing process is complete.

  3. Quit control program.

    # exit

Once two devices are paired, the ARTIK module stores the information until a remove command is executed. That is, there is no need to repeat Discovery and Pairing once the devices are initially paired.

Note that if you remove a pairing from one paired device, you may need to remove from the other, and then re-pair the devices.


Connection

Pairing is established and remains present even through a reset or power cycle. However, you must still 'connect' each time you want to use the pairing.
bluetoothctl
# connect _mac_address_of_device_
# exit

Certain profiles do not connect through bluetoothctl but by another means, such as connmanctl, or as noted.

Personal Area Networking (PAN) Profile

This example shows how to pair a typical smart phone with the ARTIK board to allow the ARTIK module to use the phone data connection for Internet access. Run this test with Wi-Fi® disabled and Ethernet disconnected to make sure you are validating the data connection through your phone.

First time through (devices not yet paired)

On the phone:

  1. Make phone visible to other Bluetooth devices.
  2. Enable Bluetooth tethering to Internet (phone must support this feature).

On the ARTIK board:

  1. Follow the Discovery and Pairing procedures to pair the phone.

First time Internet connection (devices already paired)

On the ARTIK board:

  1. Start Connection Manager.
    connmanctl

  2. List available services.
    connmanctl> services

    The screen should show paired MAC address key #s, like this:

      bluetooth_F8042EECD81F_F8CFC5E3B172  

    where the first number is the MAC address of the ARTIK module and the second is that of the phone.

  3. Connect using the Bluetooth service key #s (copy and paste), then exit.
    connmanctl> connect bluetooth_######_######
    connmanctl> exit

  4. Ping phone to test the Internet connection.
    ping 8.8.8.8

You should now see successful packet send and receive responses on the terminal.

On subsequent Internet connection

Once you know the MAC addresses, you can just execute a single command line to make the connection.

connmanctl connect bluetooth_######_######

Human Interface Device (HID) Profile

This example refers to the ZAGG pocket Bluetooth keyboard, but also applies to other devices with this profile.

On the ARTIK board:

  1. Follow the Discovery and Pairing procedures to pair the ZAGG pocket Bluetooth keyboard.

  2. After each reset, connect the keyboard.
    bluetoothctl
    # connect _mac_address_of_keyboard_
    # exit

You can start using the keyboard for the display attached to the ARTIK controller.

Obex FTP – BT File Transfer

On the Samsung phone:

  1. Download the Astro Bluetooth app and make sure the following settings are selected:
    – BT
    – Discoverable
    – FTP

On the ARTIK board (acting as peripheral device):

  1. Pair with phone, making sure ARTIK is in discoverable mode for Bluetooth.
    bluetoothctl
    # discoverable on
    # pair _mac_address_of_phone_
    # exit

  2. List folders on phone
    obexftp -b _mac_address_of_phone_ -l
  3. Send file from phone to ARTIK
    obexftp -b _mac_address_of_phone_ -g _path_and_filename_
  4. Send file from ARTIK to phone
    obexftp -b _mac_address_of_phone_ -p _path_and_filename_

After the last two steps, you should see the files in the destination folder.

Advanced Audio Distribution Profile (A2DP)

This example was tested with a Samsung EO-SG900 LEVEL Box Mini Portable Bluetooth speaker as well as a GearHead B3500 speaker. It also applies to other speakers with the a2dp profile.

First time through (devices not yet paired)

On the Bluetooth speaker:
* Power it up to make it ready for pairing according to manufacturer instructions.

On the ARTIK board:

After each board reset (devices paired)

  1. Connect the speaker.
    bluetoothctl
    # connect _mac_address_of_speaker_
    # exit

  2. List available Pulse Audio Control cards.
    pactl list cards
    If the Bluetooth speaker is not listed then you may not be paired properly.

  3. Select the card # (shown as #1 here) and the profile (in this case 'a2dp_sink') from the list.
    pactl set-card-profile 1 a2dp_sink

  4. Use the mplayer audio player program to play an audio file of your choice.
    mplayer test.wav

You should hear the audio via the Bluetooth speaker.

Bluetooth LE Pairing

Bluetooth LE follows GATT protocols. It uses the same command line procedures as Bluetooth to pair devices.

Prerequisites
Download the latest Bluetooth APIs from www.bluez.org to /etc/Bluetooth/test/

For example:

cd /etc/bluetooth/test/
wget http://www.kernel.org/pub/linux/bluetooth/bluez-5.38.tar.xz

Untar the file after the download completes. Under folder test you should find test-heartrate and test-proximity.

Proximity Profile (PXP)

This example refers to the HTC Fetch proximity sensor device but also applies to other devices using this profile.

On the HTC Fetch device:

  1. Reset the device by holding the button for a few seconds.

On the ARTIK board:

  1. Follow the Discovery and Pairing procedures to pair the HTC Fetch proximity sensor.

  2. Test and make note of options
    cd /etc/bluetooth/test
    ./test-proximity

  3. Set configuration
    ./test-proximity -b _mac_address_of_HTC_Fetch_ ImmediateAlertLevel high

Walking away with HTC will cause it to beep after the device goes out of range.

Heart Rate Profile (HRP)

This example refers to the Polar heart rate monitor but also applies to other devices using this profile.

On the ARTIK board:

  1. Follow the Discovery and Pairing procedures to pair the Polar heart rate sensor.

  2. Test the connection.

./test-heartrate -b _mac_address_of_sensor_

You should see heart rate readings showing up on the terminal.

Programming Controller Device

The command output below demonstrates what you will see when enabling Bluetooth on an ARTIK board acting as controller, and then pairing the module with a device.

$ bluetoothctl

[NEW] Controller 43:50:C0:00:00:00 localhost.localdomain [default]

[bluetooth]# agent on

Agent registered

[bluetooth]# default-agent

Default agent request successful

[bluetooth]# scan on

Discovery started
[CHG] Controller 43:50:C0:00:00:00 Discovering: yes
[NEW] Device B8:09:8A:6D:4A:74 XXXXXXX

[bluetooth]# pair B8:09:8A:6D:4A:74

Attempting to pair with B8:09:8A:6D:4A:74
[CHG] Device B8:09:8A:6D:4A:74 Connected: yes
Request confirmation
[agent] Confirm passkey 622643 (yes/no): yes
[CHG] Device B8:09:8A:6D:4A:74 Modalias: bluetooth:v004Cp6D03d0830 
[CHG] Device B8:09:8A:6D:4A:74 UUIDs: 
00000000-deca-fade-deca-deafdecacafe 
00001000-0000-1000-8000-00805f9b34fb 
0000110a-0000-1000-8000-00805f9b34fb 
0000110c-0000-1000-8000-00805f9b34fb 
0000110e-0000-1000-8000-00805f9b34fb 
00001116-0000-1000-8000-00805f9b34fb 
00001200-0000-1000-8000-00805f9b34fb
[CHG] Device B8:09:8A:6D:4A:74 Paired: yes
Pairing successful

Coding with C++

Before using the provided code, you will need to install the following software.

# dnf install bluez-libs-devel

The code below demonstrates how you might retrieve Bluetooth device information using C++.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>

int main(int argc, char **argv)
{
    inquiry_info *ii = NULL;
    int max_rsp, num_rsp;
    int dev_id, sock, len, flags;
    int i;
    char addr[19] = { 0 };
    char name[248] = { 0 };

    dev_id = hci_get_route(NULL);
    sock = hci_open_dev( dev_id );
    if (dev_id < 0 || sock < 0) {
        perror("opening socket");
        exit(1);
    }

    len  = 8;
    max_rsp = 255;
    flags = IREQ_CACHE_FLUSH;
    ii = (inquiry_info*)malloc(max_rsp * sizeof(inquiry_info));
    
    num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags);
    if( num_rsp < 0 ) perror("hci_inquiry");

    for (i = 0; i < num_rsp; i++) {
        ba2str(&(ii+i)->bdaddr, addr);
        memset(name, 0, sizeof(name));
        if (hci_read_remote_name(sock, &(ii+i)->bdaddr, sizeof(name), 
            name, 0) < 0)
        strcpy(name, "[unknown]");
        printf("%s  %s\n", addr, name);
    }

    free( ii );
    close( sock );
    return 0;
}

Save the code as scan.c, and compile it using the following command line.

# gcc scan.c -o scan -l bluetooth

Running the program returns information like the following.

[root@localhost home]# ./scan
F8:04:2E:ED:40:13  ARTIK5

Coding with Arduino

Once you have paired devices using command line or other techniques, you can perform repetitive tasks like generating the connmanctl connect ... statement using Arduino.

To execute Linux command line operations within an Arduino sketch, use code like the following. Refer to online Arduino documentation for additional details.

1
2
Process p;
p.runShellCommand("_Linux_command_line_to_execute_");

Programming Peripheral Device

The ARTIK 5, 7, and 10 devices are typically operated as central (controller) devices, but can also act as peripheral devices. They can be dynamically reprogrammed through standard Linux programming procedures as noted above.

By contrast, the ARTIK 020 module is typically used to implement a standalone Bluetooth LE peripheral device. The code is defined, and the module is programmed with the generated binary file, using the Simplicity Studio 4 development environment. The procedure is similar to that used for the ARTIK 030 when developing for a Thread or ZigBee® application.

Download and follow the ARTIK 020 Software User's Guide for full details.

Last updated on: