Tizen蓝牙SPP在发送

时间:2016-03-31 20:27:45

标签: android c bluetooth tizen tizen-wearable-sdk

我正在使用Native Tizen开发三星Gear S2,我有一个Android应用程序,使用SPP连接作为客户端到我的Gear应用程序,Android应用程序有一个按钮,用于将简单文本发送到我的Gear,一切运行良好,但是当我想从我的Gear使用SPP向我的android发送数据时,我收到了BT_ERROR_PERMISSION_DENIED,这里是我的代码汇总:

static void socket_connection_state_changed(int result,
        bt_socket_connection_state_e connection_state,
        bt_socket_connection_s *connection, void *user_data) {
    appdata_s *ad = (appdata_s *) user_data;

    if (result != BT_ERROR_NONE) {
        dlog_print(DLOG_ERROR, LOG_TAG,
                "[socket_connection_state_changed_cb] Failed. result =%d.",
                result);

        return;
    }

    if (connection_state == BT_SOCKET_CONNECTED) {
        dlog_print(DLOG_INFO, LOG_TAG, "Callback: Connected.");
        if (connection != NULL) {
            dlog_print(DLOG_INFO, LOG_TAG,
                    "Callback: Socket of connection - %d.",
                    connection->socket_fd);
            dlog_print(DLOG_INFO, LOG_TAG, "Callback: Role of connection - %d.",
                    connection->local_role);
            dlog_print(DLOG_INFO, LOG_TAG,
                    "Callback: Address of connection - %s.",
                    connection->remote_address);
            // socket_fd is used for sending data and disconnecting a device
            ad->client_socket_fd = connection->socket_fd;
        } else {
            dlog_print(DLOG_INFO, LOG_TAG, "Callback: No connection data");
        }
    } else {
        dlog_print(DLOG_INFO, LOG_TAG, "Callback: Disconnected.");
        if (connection != NULL) {
            dlog_print(DLOG_INFO, LOG_TAG,
                    "Callback: Socket of disconnection - %d.",
                    connection->socket_fd);
            dlog_print(DLOG_INFO, LOG_TAG,
                    "Callback: Address of connection - %s.",
                    connection->remote_address);
        } else {
            dlog_print(DLOG_INFO, LOG_TAG, "Callback: No connection data");
        }
    }
}

static void bt_socket_data_received_callback(bt_socket_received_data_s* data,
        void* user_data) {
    if (data == NULL) {
        dlog_print(DLOG_INFO, LOG_TAG, "No received data!");

        return;
    }
    dlog_print(DLOG_INFO, LOG_TAG, "Socket fd: %d", data->socket_fd);
    dlog_print(DLOG_INFO, LOG_TAG, "Data: %s", data->data);
    dlog_print(DLOG_INFO, LOG_TAG, "Size: %d", data->data_size);
}

static void start_spp_server(appdata_s *ad) {
    int server_socket_fd = -1;
    const char* my_uuid = "00001101-0000-1000-8000-00805F9B34FB";
    bt_error_e ret;

    ret = bt_initialize();
    if (ret != BT_ERROR_NONE) {
        dlog_print(DLOG_ERROR, LOG_TAG, "[bt_initialize] Failed.");

        return;
    } else {
        dlog_print(DLOG_INFO, LOG_TAG, "[bt_initialize] Success.");
    }

    bt_adapter_state_e adapter_state;

    // Check whether the Bluetooth adapter is enabled
    ret = bt_adapter_get_state(&adapter_state);
    if (ret != BT_ERROR_NONE) {
        dlog_print(DLOG_ERROR, LOG_TAG, "[bt_adapter_get_state] Failed");

        return;
    }
    // If the Bluetooth adapter is not enabled
    if (adapter_state == BT_ADAPTER_DISABLED) {
        dlog_print(DLOG_ERROR, LOG_TAG,
                "Bluetooth adapter is not enabled. You should enable Bluetooth!!");
    }

    ret = bt_socket_set_connection_state_changed_cb(
            socket_connection_state_changed, ad);
    if (ret != BT_ERROR_NONE) {
        dlog_print(DLOG_ERROR, LOG_TAG,
                "[bt_socket_set_connection_state_changed_cb] failed.");

        return;
    }

    ret = bt_socket_set_data_received_cb(bt_socket_data_received_callback,
            NULL);
    if (ret != BT_ERROR_NONE) {
        dlog_print(DLOG_ERROR, LOG_TAG,
                "[bt_socket_data_received_cb] regist to fail.");
    }

    ret = bt_socket_create_rfcomm(my_uuid, &server_socket_fd);
    if (ret != BT_ERROR_NONE) {
        dlog_print(DLOG_ERROR, LOG_TAG, "[bt_socket_create_rfcomm()] failed.");
        log_bt_errors(ret);
    }

    ret = bt_socket_listen_and_accept_rfcomm(server_socket_fd, 5);
    if (ret != BT_ERROR_NONE) {
        dlog_print(DLOG_ERROR, LOG_TAG,
                "[bt_socket_listen_and_accept_rfcomm] failed.");

        return;
    } else {
        dlog_print(DLOG_INFO, LOG_TAG,
                "[bt_socket_listen_and_accept_rfcomm] Succeeded. bt_socket_connection_state_changed_cb will be called.");
        // Waiting for incoming connections
        ad->server_socket_fd = server_socket_fd;
    }
}

最后,我使用一个按钮测试从我的Gear S2发送,我使用回调按钮点击此代码:

static void clicked_cb(void *data, Evas_Object *obj, void *event_info) {
    dlog_print(DLOG_INFO, LOG_TAG, "Button clicked\n");
    appdata_s *ad = (appdata_s *) data;

    bt_error_e ret;
    char datatosend[] = "Sending test";

    ret = bt_socket_send_data(ad->client_socket_fd, datatosend,
            sizeof(datatosend));
    if (ret != BT_ERROR_NONE) {
        dlog_print(DLOG_ERROR, LOG_TAG, "[bt_socket_send_data] failed.");
        log_bt_errors(ret);
        dlog_print(DLOG_ERROR, LOG_TAG, "las error code = %d", get_last_result());
    }
}

使用调试我意识到我的发送失败并出现该错误,我已经在我的清单中设置了蓝牙权限,可能是什么?

1 个答案:

答案 0 :(得分:0)

看起来您没有在tizen-manifest.xml文件中添加权限。 所需的权限是: http://tizen.org/privilege/bluetooth 和所需功能: http://tizen.org/feature/network.bluetooth