Use the SPP service

The Bluetooth library allows you to connect to a Serial Port Profile (SPP) service and exchange data with another Bluetooth enabled device. Your BlackBerry 10 device can act as either an SPP server or SPP client.

To use your device as an SPP server:
  1. Initialize the SPP service by calling bt_spp_init().
  2. To start listening for connection requests, call bt_spp_open_server().
  3. After a connection is established, exchange data with the client app.
  4. To close the connection and release resources, call bt_spp_close_server() or POSIX close().
  5. To free any resources not yet freed before closing the server app, call bt_spp_deinit().

    Calling bt_spp_deinit() may not be necessary if you have closed all connections. However, there may be situations in which a connection hasn't been cleaned up. In that case, calling bt_spp_deinit() frees all SPP resources your app uses.

To use your device as an SPP client:

  1. Initialize the SPP service by calling bt_spp_init(). If your app uses btdevice.h functions, you need to initialize the device by calling bt_device_init().
  2. Connect to the SPP service on the server device by calling bt_spp_open() or bt_spp_open_ex(). If you are using bt_spp_open(), the service_uuid argument passed into this function can be either the Universally Unique Identifier (UUID) or the name of the service to connect to. If you pass in the UUID, it must be the unique UUID defined for the server app, not the Service Class ID defined by the Bluetooth organization for SPP.
  3. Exchange data with the server device as needed using the POSIX read() and write() functions.
  4. To close the SPP connection, call bt_spp_close() or POSIX close().
  5. To free any resources not yet freed before closing your app, call bt_spp_deinit().
The following code sample demonstrates how to set up an SPP connection on the client side:
#define TEST_SERVICE_UUID "00001101-0000-1111-8888-123456ABCDEF"
	
bt_remote_device_t **remote_device_array;
bt_remote_device_t *next_remote_device = 0;
char addr[128];

// Initialize the device before calling btdevice functions.
bt_device_init(myCallback);

// Initialize the SPP service
bt_spp_init();
	
// Get information from a paired remote device
remote_device_array = 
        bt_disc_retrieve_devices(BT_DISCOVERY_PREKNOWN, 0);
if (remote_device_array) {
    // As an example, we'll use the first remote device.
    next_remote_device = remote_device_array[0];
    bt_rdev_get_address(next_remote_device, addr);

    // Open SPP connection to the remote device
    int fd = bt_spp_open(addr, (char *)TEST_SERVICE_UUID, false);
    if (fd >= 0) {
        // Perform desired tasks with the remote device.
        // You can exchange data with the remote device using 
        // the POSIX read() and write() functions.
        
    } else {
        // Handle connection error
        // ...
    
    }    
    // Close SPP connection
    bt_spp_close(fd);
}

// Free resources
bt_spp_deinit();
bt_device_deinit();