Connecting to other Bluetooth module

Besides than scanning functions, the Bluetooth API contains some functions to open a transparent connection to other Bluetooth module.

CreateConnection (MAC)

This function is used to open a transparent connection with other Bluetooth module using the Serial Port Profile. The MAC address of the remote Bluetooth device must be specified.

{
  if (BT_Pro.createConnection(mac) == 1)
    {
      USB.println(F(\"Connected.\"));
    }
   else
    {
    USB.println(F(\"Not conencted\"));
    }
 }

SendData (data)

This function sends data directly to other Bluetooth module using a previously created connection.

{
    if (BT_Pro.sendData(\"Hello, This is Bluetooth module.\") == 1)
    {
        USB.println(F(\"Data sent\"));
    }
}

RemoveConnection()

This function removes a transparent connection with other Bluetooth module.

{
    if (BT_Pro.removeConnection() == 1)
    {
        USB.println(F(\"Connection removed\"));
    }
    else
    {
        USB.println(F(\"Not removed\"));
    }
}

In the Waspmote Development section you can find a complete example about using these functions:

https://development.libelium.com/bt-pro-10-creating-a-transparent-connection/

Pairing with devices

If the user wants to connect one device with security, both devices have to be paired before. A pin code can be used also to avoid man-in-the-middle intrusions. There are three functions to manage paired devices saved by the Bluetooth module.

Pair(macAddress, pincode)

This function pairs the Bluetooth module with the specified Bluetooth device. If the pin code is not specified, default code will be "123456".

{
    if (BT_Pro.pair("00:1a:70:90:b3:28") == 1)
    {
        USB.println(F(\"Paired OK\"));
    }
    else
    {
        USB.println(F(\"Not paired\"));
    }
}

IsPaired(deviceMac)

This function is used to check if a device is already paired.

{
    if (BT_Pro.isPaired("00:1a:70:90:b3:28") == 1)
    {
        USB.println(F(\"Already paired\"));
    }
    else
    {
        USB.println(F(\"Not paired\"));
    }
}

removePairedDevices()

This function removes all paired devices from the list.

{
    BT_Pro.removePairedDevices();
}

In the Waspmote Development section you can find a complete example about using these functions:

https://development.libelium.com/bt-pro-12-pairing-example/

Using WaspFrame class to create sensor data frames

WaspFrame is a class that allows the user to create data frames with a specified format. It is a very useful tool to build the packet in the same way as other Waspmote examples. It is recommended to read the Waspmote Programming Guide available here:

http://www.libelium.com/development/waspmote/documentation/programming

Last updated