Use the SPP service

The Serial Port Profile (SPP) is often used for creating chat apps. You can connect to an SPP service and exchange messages with another Bluetooth enabled device. Your BlackBerry 10 device can act as either an SPP server or an SPP client.

The following steps describe how to set up an SPP connection using the C API.

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

    Calling bt_spp_deinit() may not be necessary if you have closed all connections. However, there may be situations in which a connection has not 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. Close the SPP connection by calling bt_spp_close() or POSIX close().
  5. Free any resources not yet freed by calling bt_spp_deinit() before closing your app.

The following code sample demonstrates how to set up an SPP connection on the client side:

Not applicable

Coming soon

#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();
        

Last modified: 2015-07-24



Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus