【发布时间】:2019-12-12 18:12:56
【问题描述】:
我有一个简单的录音机应用程序,当我停止录音时,我希望立即通过蓝牙将录制的文件发送到我的手机。
但是我错过了一些东西,我不知道是什么。
它基于用 C 编写的示例原生 ui 应用程序,这是我目前所做的:
在_app_resume_cb():
我设置了以下回调:
int error_code = bt_adapter_foreach_bonded_device(bonded_device_cb, NULL);
dlog_print(DLOG_INFO, LOG_TAG, "bt_adapter_foreach_bonded_device. error code: %s", get_error_message(error_code));
error_code = bt_socket_set_connection_state_changed_cb(socket_connection_state_changed_cb,NULL);
dlog_print(DLOG_INFO, LOG_TAG, "registered bt_socket_set_connection_state_changed_cb. error code: %s", get_error_message(error_code));
然后在我的bonded_device_cb() 中,我阅读了remote_address 和service_uuid
static bool bonded_device_cb(bt_device_info_s *device_info, void *user_data)
{
dlog_print(DLOG_INFO, LOG_TAG, "got a bonded device! \n"
"remote_address: %s \n"
"remote_name: %s \n"
"service_uuid: %s \n"
"is_bonded: %d \n"
"is_connected: %d \n"
"is_authorized: %d \n",
device_info->remote_address,
device_info->remote_name,
*device_info->service_uuid,
device_info->is_bonded,
device_info->is_connected,
device_info->is_authorized);
remote_address = malloc((sizeof(device_info->remote_address)*strlen(device_info->remote_address))+1);
service_uuid = malloc((sizeof(*device_info->service_uuid)*strlen(*device_info->service_uuid))+1);
strcpy(remote_address, device_info->remote_address);
strcpy(service_uuid, *device_info->service_uuid);
dlog_print(DLOG_INFO, LOG_TAG, "copied!! remote_address: %s, service_uuid: %s", remote_address, service_uuid);
return true;
}
稍后在_app_resume_cb() 中使用它以使用bt_socket_connect_rfcomm():
dlog_print(DLOG_INFO, LOG_TAG, "remote_address: %s, service_uuid: %s", remote_address, service_uuid);
error_code = bt_socket_connect_rfcomm(remote_address, service_uuid);
dlog_print(DLOG_INFO, LOG_TAG, "bt_socket_connect_rfcomm. error code: %s", get_error_message(error_code));
所以连接error_code是成功的,在我的socket_connection_state_changed_cb()我读到了socket_fd:
static void socket_connection_state_changed_cb(int result, bt_socket_connection_state_e connection_state,
bt_socket_connection_s *connection, void *user_data)
{
dlog_print(DLOG_INFO, LOG_TAG, "socket connection state changed \n"
"result: %s "
"connection_state: %d "
"socket_fd: %d "
"server_fd: %d "
"remote_address: %s "
"local_role: %d "
"service_uuid: %s ",
get_error_message(result),
connection_state,
connection->socket_fd,
connection->server_fd,
connection->remote_address,
connection->local_role,
connection->service_uuid);
socket_fd = connection->socket_fd;
}
然后用于使用bt_socket_send_data() 发送数据。我按下停止记录按钮后发送数据:
...
/* Stop the recorder and save the recorded data to a file */
if (_recorder_expect_state(g_recorder, RECORDER_STATE_RECORDING)
|| _recorder_expect_state(g_recorder, RECORDER_STATE_PAUSED))
{
char *filez;
recorder_get_filename(g_recorder, &filez);
stop_recorder();
dlog_print(DLOG_INFO, LOG_TAG, "stopped recording");
Eina_Bool shared = EINA_FALSE;
Eina_File * ein_file = eina_file_open(filez, shared);
size_t fil_size = eina_file_size_get(ein_file);
const char * fil_name = eina_file_filename_get(ein_file);
dlog_print(DLOG_INFO, LOG_TAG, "****opened file?, filez: %s fil_size: %d, fil_name: %s", filez, fil_size, fil_name);
error_code = bt_socket_send_data(socket_fd, data, fil_size);
dlog_print(DLOG_INFO, LOG_TAG, "****sending file!!!!! number of bytes written: %d", error_code);
...
在日志中我可以看到正确打印的文件名和大小。
一些日志打印输出:
got a bonded device! remote_address: 34:2D:0D:12:BB:89 remote_name: Galaxy S8 service_uuid: 00001105-0000-1000-8000-00805F9B34FB is_bonded: 1 is_connected: 1 is_authorized: 0
socket connection state changed result: Successful connection_state: 0 socket_fd: 40 server_fd: -1 remote_address: 34:2D:0D:12:BB:89 local_role: 2 service_uuid: 00001105-0000-1000-8000-00805F9B34FB
...
remote_address: 34:2D:0D:12:BB:89, service_uuid: 00001105-0000-1000-8000-00805F9B34FB
bt_socket_connect_rfcomm. error code: Successful
...
****opened file?, filez: /opt/usr/media/Documents/AUDIO_2019_12_12_18_39_27.3gp fil_size: 835, fil_name: /opt/usr/media/Documents/AUDIO_2019_12_12_18_39_27.3gp
****sending file!!!!! number of bytes written: 835
在电话上我从来没有收到“接收文件”的请求,除非它是“默默地”完成的?我对此高度怀疑。我尝试手动搜索该文件,但无济于事。
请告诉我我错过了什么!
最好的问候,
赫尔班
【问题讨论】:
-
您找到解决方案了吗?我有完全相同的问题!
-
@hbdch 不幸的是没有,显然从日志来看,我尝试过的一切都返回正常或成功,但是手机上什么也没有发生,也没有收到文件。
标签: c bluetooth tizen tizen-wearable-sdk tizen-native-app