Tizen Native API
5.0
|
Bluetooth Socket API provides functions for managing connections to other devices and exchanging data.
#include <bluetooth.h>
This set of functions is used for exchanging data between two bluetooth devices, our device can have both roles as server (service provider) and client (service user). Depending on the role, there is difference in creating a connection. After that, processes of exchanging data and disconnection are same.
To start communication, you should first register for bt_socket_set_connection_state_changed_cb() and bt_socket_set_data_received_cb(). Next step depends on the role of your application.
If you want to be server role, application should create socket (bt_socket_create_rfcomm()), and start listening and accepting incoming connections (bt_socket_listen_and_accept_rfcomm()). If you want to connect to specific device and use it's services (client role), you have to start connection with bt_socket_connect_rfcomm() and wait for connection.
After connection has been established (information is passed to your program with bt_socket_connection_state_changed_cb() function call), you can exchange data by calling bt_socket_send_data().
If you receive data from remote device, bt_socket_data_received_cb() functions will be called. When you finish exchanging data, you should disconnect connection with bt_socket_disconnect_rfcomm() and unregister callback functions (with using bt_socket_unset_data_received_cb(), bt_socket_unset_connection_state_changed_cb()).
This API is related with the following features:
It is recommended to design feature related codes in your application for reliability.
You can check if a device supports the related features for this API by using System Information,thereby controlling the procedure of your application.
To ensure your application is only running on the device with specific features, please define the features in your manifest file using the manifest editor in the SDK.
More details on featuring your application can be found from Feature Element.
FUNCTION | CALLBACK | DESCRIPTION |
---|---|---|
bt_socket_listen_and_accept_rfcomm() bt_socket_connect_rfcomm() | bt_socket_connection_state_changed_cb() | Used to connect a device. |
REGISTER | UNREGISTER | CALLBACK | DESCRIPTION |
---|---|---|---|
bt_socket_set_data_received_cb() | bt_socket_unset_data_received_cb() | bt_socket_data_received_cb() | Used to be notified of received data. |
bt_socket_set_connection_state_changed_cb() | bt_socket_unset_connection_state_changed_cb() | bt_socket_connection_state_changed_cb() | Used to be notified when the state of connection changes. |
Please refer Bluetooth Tutorial if you want to get more detailed usages and information of this api.
Functions | |
int | bt_socket_create_rfcomm (const char *service_uuid, int *socket_fd) |
Registers a RFCOMM server socket with a specific UUID. | |
int | bt_socket_destroy_rfcomm (int socket_fd) |
Removes the RFCOMM server socket which was created using bt_socket_create_rfcomm(). | |
int | bt_socket_listen_and_accept_rfcomm (int socket_fd, int max_pending_connections) |
Starts listening on passed RFCOMM socket and accepts connection requests. | |
int | bt_socket_connect_rfcomm (const char *remote_address, const char *service_uuid) |
Connects to a specific RFCOMM based service on a remote Bluetooth device UUID, asynchronously. | |
int | bt_socket_disconnect_rfcomm (int socket_fd) |
Disconnects the RFCOMM connection with the given file descriptor of connected socket. | |
int | bt_socket_send_data (int socket_fd, const char *data, int length) |
Sends data to the connected device. | |
int | bt_socket_set_data_received_cb (bt_socket_data_received_cb callback, void *user_data) |
Registers a callback function that will be invoked when you receive data. | |
int | bt_socket_unset_data_received_cb (void) |
Unregisters the callback function. | |
int | bt_socket_set_connection_requested_cb (bt_socket_connection_requested_cb callback, void *user_data) |
Registers a callback function that will be invoked when a RFCOMM connection is requested. | |
int | bt_socket_unset_connection_requested_cb (void) |
Unregisters the callback function. | |
int | bt_socket_set_connection_state_changed_cb (bt_socket_connection_state_changed_cb callback, void *user_data) |
Registers a callback function that will be invoked when the connection state changes. | |
int | bt_socket_unset_connection_state_changed_cb (void) |
Unregisters the callback function. | |
Typedefs | |
typedef void(* | bt_socket_data_received_cb )(bt_socket_received_data_s *data, void *user_data) |
Called when you receive data. | |
typedef void(* | bt_socket_connection_state_changed_cb )(int result, bt_socket_connection_state_e connection_state, bt_socket_connection_s *connection, void *user_data) |
Called when the socket connection state changes. | |
typedef void(* | bt_socket_connection_requested_cb )(int socket_fd, const char *remote_address, void *user_data) |
Called when a RFCOMM connection is requested. |
typedef void(* bt_socket_connection_requested_cb)(int socket_fd, const char *remote_address, void *user_data) |
Called when a RFCOMM connection is requested.
[in] | socket_fd | The file descriptor of socket on which a connection is requested |
[in] | remote_address | The address of remote device |
[in] | user_data | The user data passed from the callback registration function |
typedef void(* bt_socket_connection_state_changed_cb)(int result, bt_socket_connection_state_e connection_state, bt_socket_connection_s *connection, void *user_data) |
Called when the socket connection state changes.
[in] | result | The result of connection state changing |
[in] | connection_state | The connection state |
[in] | connection | The connection information which is established or disconnected |
[in] | user_data | The user data passed from the callback registration function |
typedef void(* bt_socket_data_received_cb)(bt_socket_received_data_s *data, void *user_data) |
Called when you receive data.
[in] | data | The received data from the remote device |
[in] | user_data | The user data passed from the callback registration function |
enum bt_socket_role_e |
int bt_socket_connect_rfcomm | ( | const char * | remote_address, |
const char * | service_uuid | ||
) |
Connects to a specific RFCOMM based service on a remote Bluetooth device UUID, asynchronously.
[in] | remote_address | The address of the remote Bluetooth device |
[in] | service_uuid | The UUID of service provided by the remote Bluetooth device |
BT_ERROR_NONE | Successful |
BT_ERROR_NOT_INITIALIZED | Not initialized |
BT_ERROR_INVALID_PARAMETER | Invalid parameter |
BT_ERROR_NOT_ENABLED | Not enabled |
BT_ERROR_OPERATION_FAILED | Operation failed |
BT_ERROR_PERMISSION_DENIED | Permission denied |
BT_ERROR_NOT_SUPPORTED | Not supported |
int bt_socket_create_rfcomm | ( | const char * | service_uuid, |
int * | socket_fd | ||
) |
Registers a RFCOMM server socket with a specific UUID.
[in] | service_uuid | The UUID of service to provide |
[out] | socket_fd | The file descriptor of socket to listen |
BT_ERROR_NONE | Successful |
BT_ERROR_NOT_INITIALIZED | Not initialized |
BT_ERROR_INVALID_PARAMETER | Invalid parameter |
BT_ERROR_NOT_ENABLED | Not enabled |
BT_ERROR_OPERATION_FAILED | Operation failed |
BT_ERROR_PERMISSION_DENIED | Permission denied |
BT_ERROR_NOT_SUPPORTED | Not supported |
int bt_socket_destroy_rfcomm | ( | int | socket_fd | ) |
Removes the RFCOMM server socket which was created using bt_socket_create_rfcomm().
[in] | socket_fd | The file descriptor of socket (which was created using bt_socket_create_rfcomm()) to destroy |
BT_ERROR_NONE | Successful |
BT_ERROR_NOT_INITIALIZED | Not initialized |
BT_ERROR_INVALID_PARAMETER | Invalid parameter |
BT_ERROR_NOT_ENABLED | Not enabled |
BT_ERROR_OPERATION_FAILED | Operation failed |
BT_ERROR_PERMISSION_DENIED | Permission denied |
BT_ERROR_NOT_SUPPORTED | Not supported |
int bt_socket_disconnect_rfcomm | ( | int | socket_fd | ) |
Disconnects the RFCOMM connection with the given file descriptor of connected socket.
[in] | socket_fd | The file descriptor of socket to close which was received using bt_socket_connection_state_changed_cb(). |
BT_ERROR_NONE | Successful |
BT_ERROR_NOT_INITIALIZED | Not initialized |
BT_ERROR_NOT_ENABLED | Not enabled |
BT_ERROR_INVALID_PARAMETER | Invalid parameter |
BT_ERROR_PERMISSION_DENIED | Permission denied |
BT_ERROR_NOT_SUPPORTED | Not supported |
int bt_socket_listen_and_accept_rfcomm | ( | int | socket_fd, |
int | max_pending_connections | ||
) |
Starts listening on passed RFCOMM socket and accepts connection requests.
Pop-up is shown automatically when a RFCOMM connection is requested.
bt_socket_connection_state_changed_cb() will be called with
BT_SOCKET_CONNECTED if you click "yes" and connection is finished successfully.
[in] | socket_fd | The file descriptor of socket on which start to listen |
[in] | max_pending_connections | The maximum number of pending connections |
BT_ERROR_NONE | Successful |
BT_ERROR_NOT_INITIALIZED | Not initialized |
BT_ERROR_NOT_ENABLED | Not enabled |
BT_ERROR_INVALID_PARAMETER | Invalid parameter |
BT_ERROR_OPERATION_FAILED | Operation failed |
BT_ERROR_PERMISSION_DENIED | Permission denied |
BT_ERROR_NOT_SUPPORTED | Not supported |
int bt_socket_send_data | ( | int | socket_fd, |
const char * | data, | ||
int | length | ||
) |
Sends data to the connected device.
[in] | socket_fd | The file descriptor of connected socket which was received using bt_socket_connection_state_changed_cb() |
[in] | data | The data to be sent |
[in] | length | The length of data to be sent |
On | error, -1 is returned, and errno is set appropriately. See write 2 man page. |
BT_ERROR_NOT_INITIALIZED | Not initialized |
BT_ERROR_NOT_SUPPORTED | Not supported |
BT_ERROR_INVALID_PARAMETER | Invalid parameter |
BT_ERROR_PERMISSION_DENIED | Permission denied |
BT_ERROR_AGAIN | Resource temporarily unavailable |
int bt_socket_set_connection_requested_cb | ( | bt_socket_connection_requested_cb | callback, |
void * | user_data | ||
) |
Registers a callback function that will be invoked when a RFCOMM connection is requested.
[in] | callback | The callback function to register |
[in] | user_data | The user data to be passed to the callback function |
BT_ERROR_NONE | Successful |
BT_ERROR_NOT_INITIALIZED | Not initialized |
BT_ERROR_INVALID_PARAMETER | Invalid parameter |
BT_ERROR_NOT_SUPPORTED | Not supported |
int bt_socket_set_connection_state_changed_cb | ( | bt_socket_connection_state_changed_cb | callback, |
void * | user_data | ||
) |
Registers a callback function that will be invoked when the connection state changes.
[in] | callback | The callback function to register |
[in] | user_data | The user data to be passed to the callback function |
BT_ERROR_NONE | Successful |
BT_ERROR_NOT_INITIALIZED | Not initialized |
BT_ERROR_INVALID_PARAMETER | Invalid parameter |
BT_ERROR_NOT_SUPPORTED | Not supported |
int bt_socket_set_data_received_cb | ( | bt_socket_data_received_cb | callback, |
void * | user_data | ||
) |
Registers a callback function that will be invoked when you receive data.
[in] | callback | The callback function to register |
[in] | user_data | The user data to be passed to the callback function |
BT_ERROR_NONE | Successful |
BT_ERROR_NOT_INITIALIZED | Not initialized |
BT_ERROR_INVALID_PARAMETER | Invalid parameter |
BT_ERROR_NOT_SUPPORTED | Not supported |
int bt_socket_unset_connection_requested_cb | ( | void | ) |
Unregisters the callback function.
BT_ERROR_NONE | Successful |
BT_ERROR_NOT_INITIALIZED | Not initialized |
BT_ERROR_NOT_SUPPORTED | Not supported |
int bt_socket_unset_connection_state_changed_cb | ( | void | ) |
Unregisters the callback function.
BT_ERROR_NONE | Successful |
BT_ERROR_NOT_INITIALIZED | Not initialized |
BT_ERROR_NOT_SUPPORTED | Not supported |
int bt_socket_unset_data_received_cb | ( | void | ) |
Unregisters the callback function.
BT_ERROR_NONE | Successful |
BT_ERROR_NOT_INITIALIZED | Not initialized |
BT_ERROR_NOT_SUPPORTED | Not supported |