Discover and pair with a remote device

Device discovery, pairing, and device lookup are some of the basic tasks you may perform with Bluetooth. The following steps demonstrate how to use the C API functions to perform these tasks.

  1. Initialize the device by calling bt_device_init(). This function takes a callback as an argument. The callback is invoked whenever a Bluetooth event is triggered.
  2. Scan for Bluetooth enabled devices nearby by calling bt_disc_start_inquiry().
  3. Retrieve a list of all discovered devices by calling bt_disc_retrieve_devices().
  4. Retrieve information about a particular device by calling the API functions prefixed with bt_rdev_ in btdevice.h. For example, retrieve the device name by calling bt_rdev_get_friendly_name(); retrieve the device address by calling bt_rdev_get_address().
  5. Pair with a remote device by calling bt_rdev_pair().
  6. Free the resources allocated for the remote device array by calling bt_rdev_free_array().
  7. Free the resources used for retrieving a particular remote device by calling bt_rdev_free_services() and bt_rdev_free(), if applicable.

Not applicable

Coming soon

// Handle Bluetooth events
void my_callback(const int event, const char *bt_addr, const char *event_data)
{
    switch (event) {
        case BT_EVT_RADIO_INIT:
        	// Bluetooth radio started
        	break;
        case BT_EVT_PAIRING_COMPLETE:
        	// Pairing with another device completed.
        	// Handle event as needed.
        	// ...
        	break;
        // other cases
        // ...
        default:
        	// Handle other events or errors
        	// ...
    } 
}

// This function searches for Bluetooth devices nearby,
// pairs with the remote device needed, and
// retrieves remote device information.
discover_device() {
    bt_device_init(my_callback);

    // Scan for devices
    bt_disc_start_inquiry(BT_INQUIRY_GIAC);
    delay(5);
    bt_disc_cancel_inquiry();

	// Look through discovered devices
    bt_remote_device_t *next_remote_device = 0;	
    bt_remote_device_t **remote_device_array = 
            bt_disc_retrieve_devices(BT_DISCOVERY_CACHED, 0);
    if (remote_device_array) {
        for (int i = 0; (next_remote_device = 
                         remote_device_array[i]); ++i) {
            #define UNKNOWN "Unknown"
            char name[128] = UNKNOWN;
            const int bufferSize = sizeof(name);
            char addr[128];
            int device_class = -1;
            int device_type = -1;
            bool paired = false;
			
            // Retrieve remote device information
            bt_rdev_get_address(next_remote_device, addr);	     
            bt_rdev_get_friendly_name(next_remote_device, 
                                      name, bufferSize);
            device_class = 
                bt_rdev_get_device_class(next_remote_device, 
                                         BT_COD_DEVICECLASS);
            device_type = bt_rdev_get_type(next_remote_device);			
            // ...
			
            // Look for a device match
            // For example, check the UUID or the MAC address
            // ...
			
			if device_matches(next_remote_device) {
			
                // Assuming the device is found,
                // set up pairing if needed
                if(bt_rdev_is_paired(next_remote_device, 
                                     &paired) == 0) {			
                    if (!paired) {
                        if (bt_rdev_pair(next_remote_device) == 0) {
				            paired = true;
                        } else {
                            // Pairing failed; handle error
                        }
                    }
                }	
	
                // Retrieve services
                char **services_array = 
                         bt_rdev_get_services(next_remote_device);
                if (services_array != NULL) {
                    // Parse retrieved services as needed
                    // ...
                }
            
                bt_rdev_free_services(services_array);

                // We found and paired with the needed
                // remote device, so we exit the loop.
                break;
            }
		}

	    // Free resources
        bt_rdev_free(next_remote_device);
        bt_rdev_free_array(remote_device_array);
    }
}

Last modified: 2015-07-24



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

comments powered by Disqus