Tizen D.I.T

Bluetooth

D.I.T

#include "Commnucation/Bluetooth.h"
#include "dit.h"

void Bluetooth_FileSend ()
{
    Bluetooth bt = NewBluetooth ();
    // Bluetooth 모듈 생성
    if ( bt->isBluetoothAccessible (bt))
    {// Bluetooth 장비 확인
        if ( bt->onConnect (bt))
        {// Bluetooth 접속
            String sendfilepath = getSharedResourceFile ("music/Over the Horizon.mp3");
            // FilePath 설정
            bt->FileSend (bt, sendfilepath);
            // Bluetooth FileSend
            free (sendfilepath);
            // FilePath 삭제
        }
    }
    DestroyBluetooth (bt);
    // Bluetooth 모듈 삭제
}

void Bluetooth_FileRecv ()
{
    Bluetooth bt = NewBluetooth ();
    // Bluetooth 모듈 생성
    if ( bt->isBluetoothAccessible (bt))
    {// Bluetooth 장비 확인
        if ( bt->onConnect (bt))
        {// Bluetooth 접속
            String recvfilepath = NULL;
            // FilePath 설정
            bt->FileRecv (bt, &recvfilepath);
            // Bluetooth FileRecv
            free (recvfilepath);
            // FilePath 삭제
        }
    }
    DestroyBluetooth (bt);
    // Bluetooth 모듈 삭제
}

Native

#include <stdlib.h>

#include <system_info.h>
#include <bluetooth.h>
#include <app_control.h>
#include <glib.h>

#define DOWNLOADSFOLDERPATH "/opt/usr/media/Downloads"
char * remoteMACAddr;
bool accessible;

static bool adapter_bonded_device_cbx (bt_device_info_s * device_info, void * user_data);
static void __bt_opp_client_push_responded_cbx (int result, const char * remote_address, void * user_data);
static void __bt_opp_client_push_progress_cbx (const char * file, long long size, int percent, void * user_data);
static void __bt_opp_client_push_finished_cbx (int result, const char * remote_address, void * user_data);
static void adapter_state_changed_cbx (int result, bt_adapter_state_e adapter_state, void * user_data);
static void bt_opp_server_transfer_progress_cb_for_oppx (const char * file, long long size, int percent, void * user_data);
static void bt_opp_server_transfer_finished_cb_for_oppx (int result, const char * file, long long size, void * user_data);
static void connection_requested_cb_for_opp_serverx (const char * remote_address, void * user_data);

void Bluetooth_FileSend ()
{
    bool ret;
    system_info_get_platform_bool ("http://tizen.org/feature/network.bluetooth", &ret);

    if ( ret )
    {
        bt_adapter_state_e adapter_state;
        int                re = bt_adapter_get_state (&adapter_state);

        if ( re == BT_ERROR_NONE )
        {// Bluetooth 장비 확인
            accessible = true;

            bt_initialize ();
            bt_adapter_set_state_changed_cb (adapter_state_changed_cbx, NULL);
            bt_adapter_foreach_bonded_device (adapter_bonded_device_cbx, NULL);
            bt_adapter_start_device_discovery ();
            // Bluetooth 접속

            char * sendfilepath = getSharedResourceFile ("music/Over the Horizon.mp3");
            // FilePath 설정

            int ret = bt_opp_client_initialize ();
            if ( ret != BT_ERROR_NONE )
            {
                bt_opp_client_deinitialize ();
                return;
            }

            ret = bt_opp_client_add_file (sendfilepath);

            if ( ret != BT_ERROR_NONE )
            {
                bt_opp_client_clear_files ();
                bt_opp_client_deinitialize ();
                return;
            }

            ret = bt_opp_client_push_files (remoteMACAddr, __bt_opp_client_push_responded_cbx, __bt_opp_client_push_progress_cbx, __bt_opp_client_push_finished_cbx, NULL);

            if ( ret != BT_ERROR_NONE )
            {
                bt_opp_client_cancel_push ();
                bt_opp_client_clear_files ();
                bt_opp_client_deinitialize ();
                return;
            }

            ret = bt_opp_client_clear_files ();

            if ( ret != BT_ERROR_NONE )
            {
                bt_opp_client_deinitialize ();
                return;
            }
            bt_opp_client_deinitialize ();
            // Bluetooth FileSend

            free (sendfilepath);
            // FilePath 삭제
        }
    }

    bt_opp_server_deinitialize ();
    bt_adapter_stop_device_discovery ();
    bt_adapter_unset_state_changed_cb ();
    bt_adapter_unset_device_discovery_state_changed_cb ();
    bt_device_unset_service_searched_cb ();
    bt_socket_unset_data_received_cb ();
    bt_socket_unset_connection_state_changed_cb ();
    bt_adapter_unset_visibility_duration_changed_cb ();
    // Bluetooth 모듈 삭제
}

void Bluetooth_FileRecv ()
{
    bool ret;
    system_info_get_platform_bool ("http://tizen.org/feature/network.bluetooth", &ret);

    if ( ret )
    {
        bt_adapter_state_e adapter_state;
        int                re = bt_adapter_get_state (&adapter_state);

        if ( re == BT_ERROR_NONE )
        {// Bluetooth 장비 확인
            accessible = true;

            bt_initialize ();
            bt_adapter_set_state_changed_cb (adapter_state_changed_cbx, NULL);
            bt_adapter_foreach_bonded_device (adapter_bonded_device_cbx, NULL);
            bt_adapter_start_device_discovery ();
            // Bluetooth 접속

            char * recvBuffer = NULL;
            // FilePath 설정

            int ret = bt_opp_server_initialize_by_connection_request (DOWNLOADSFOLDERPATH, connection_requested_cb_for_opp_serverx, &recvBuffer);
            if ( ret != BT_ERROR_NONE )
            {
                return;
            }
            // Bluetooth FileSend

            free (recvBuffer);
            // FilePath 삭제
        }
    }

    bt_opp_server_deinitialize ();
    bt_adapter_stop_device_discovery ();
    bt_adapter_unset_state_changed_cb ();
    bt_adapter_unset_device_discovery_state_changed_cb ();
    bt_device_unset_service_searched_cb ();
    bt_socket_unset_data_received_cb ();
    bt_socket_unset_connection_state_changed_cb ();
    bt_adapter_unset_visibility_duration_changed_cb ();
    // Bluetooth 모듈 삭제
}

//페어링된 Device iter callback
static bool adapter_bonded_device_cbx (bt_device_info_s * device_info, void * user_data)
{
    if ( device_info == NULL )
    {
        return false;
    }

    if ( remoteMACAddr != NULL )
    {
        free (remoteMACAddr);
    }

    size_t size = strlen (device_info->remote_address);
    remoteMACAddr = (char *)malloc (size + 1);

    strcpy (remoteMACAddr, device_info->remote_address);
    dlog_print (DLOG_INFO, "DIT", remoteMACAddr);
    return false;
}

//Data Push
static void __bt_opp_client_push_responded_cbx (int result, const char * remote_address, void * user_data)
{
    dlog_print (DLOG_INFO, "DIT", "result: %s", BluetoothErrorCheck (result));
    dlog_print (DLOG_INFO, "DIT", "remote_address: %s", remote_address);
}

//Data Push
static void __bt_opp_client_push_progress_cbx (const char * file, long long size, int percent, void * user_data)
{
    dlog_print (DLOG_INFO, "DIT", "size: %lld", size);
    dlog_print (DLOG_INFO, "DIT", "percent: %d", percent);
    dlog_print (DLOG_INFO, "DIT", "file: %s", file);
}

//Data Push
static void __bt_opp_client_push_finished_cbx (int result, const char * remote_address, void * user_data)
{
    dlog_print (DLOG_INFO, "DIT", "result: %d", result);
    dlog_print (DLOG_INFO, "DIT", "remote_address: %s", remote_address);
}

//블루투스 연결 가능 체크용 callback
static void adapter_state_changed_cbx (int result, bt_adapter_state_e adapter_state, void * user_data)
{
    if ( result != BT_ERROR_NONE )
    {
        accessible = false;
        return;
    }
    if ( adapter_state == BT_ADAPTER_ENABLED )
    {
        bt_adapter_visibility_mode_e mode;

        int duration = 1;

        bt_adapter_get_visibility (&mode, &duration);

        switch (mode)
        {
        case BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE:
            accessible = false;
            break;
        case BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE:
            accessible = true;
            break;
        case BT_ADAPTER_VISIBILITY_MODE_LIMITED_DISCOVERABLE:
            accessible = true;
            break;
        }
    }
    else
    {
        accessible = false;
    }
}

//Data Recv
static void bt_opp_server_transfer_progress_cb_for_oppx (const char * file, long long size, int percent, void * user_data)
{
    dlog_print (DLOG_INFO, "DIT", "file: %s", file);
    dlog_print (DLOG_INFO, "DIT", "size: %lld", size);
    dlog_print (DLOG_INFO, "DIT", "percent: %d", percent);
}

//Data Recv
static void bt_opp_server_transfer_finished_cb_for_oppx (int result, const char * file, long long size, void * user_data)
{
    dlog_print (DLOG_INFO, "DIT", "result: %d", result);
    dlog_print (DLOG_INFO, "DIT", "file: %s", file);
    dlog_print (DLOG_INFO, "DIT", "size: %lld", size);

    char ** recvBuffer = (char **)user_data;

    size_t fileLen     = strlen (file);
    size_t downloadLen = strlen (DOWNLOADSFOLDERPATH);
    *recvBuffer = (char *)malloc (fileLen + downloadLen + 2);
    strcpy (*recvBuffer, DOWNLOADSFOLDERPATH);
    strcat (*recvBuffer, "/");
    strcat (*recvBuffer, file);
}

//Data Recv
static void connection_requested_cb_for_opp_serverx (const char * remote_address, void * user_data)
{
    bt_error_e ret;

    ret = bt_opp_server_accept (bt_opp_server_transfer_progress_cb_for_oppx, bt_opp_server_transfer_finished_cb_for_oppx, NULL, user_data, NULL);
}