用Dbus与android的bluz通信
来源:互联网 发布:安卓蹭网软件哪个最好 编辑:程序博客网 时间:2024/06/06 15:37
android的bluetooth架构是建立在bluez之上的,bluz通过dbus暴露了底层蓝牙设备的服务。先用android内置的命令bttest enable,开启bluedroid进程。这样bluz就可以接收dbus请求了。先来看段简单dbus代码:
char*
send_method_call_with_string_reply(char* objectPath,
char* type, char* name)
{
DBusMessage* msg;
DBusMessage* reply;
DBusConnection* conn;
DBusError err;
const char *reply_path;
char *path;
// initialise the error value
dbus_error_init(&err);
printf("get dbus connection:\n");
conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
if (dbus_error_is_set(&err))
{
fprintf(stderr, "Connection Error (%s)\n", err.message);
dbus_error_free(&err);
}
if (NULL == conn)
{
printf("conn is null\n");
goto done;
}
msg = dbus_message_new_method_call(“org.bluez”, // target for the method call
objectPath, // object to call on
type, // interface to call on
name); // method name
if (NULL == msg)
{
fprintf(stderr, "Message Null\n");
goto done;
}
reply = dbus_connection_send_with_reply_and_block(conn, msg, -1, &err);
if (!reply)
{
fprintf(stderr, "Can't get reply\n");
if (dbus_error_is_set(&err))
{
fprintf(stderr, "%s\n", err.message);
dbus_error_free(&err);
}
goto done;
}
if (!dbus_message_get_args(reply, &err, DBUS_TYPE_OBJECT_PATH, &reply_path,
DBUS_TYPE_INVALID))
{
fprintf(stderr, "Can't get reply arguments\n");
if (dbus_error_is_set(&err))
{
fprintf(stderr, "%s\n", err.message);
dbus_error_free(&err);
}
goto done;
}
path = strdup(reply_path);
dbus_connection_flush(conn);
printf("method call Sent\n");
done:
// free the message
if(msg)
dbus_message_unref(msg);
if(reply)
dbus_message_unref(reply);
return path;
}测试程序如下
#include <dbus/dbus.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char **argv) {
printf("----------------------test dbus start----------------------\n");
char* ss = send_method_call_with_string_reply("/","org.bluez.Manager","DefaultAdapter");
printf("reteun value:%s\n",ss);
free(ss);
printf("-----------------------test dbus end-----------------------\n");
return 1;
}在android.mk中添加LOCAL_SHARED_LIBRARIES := libbluedroid libdbus,再加上dbus头文件就能编过。
- 用Dbus与android的bluz通信
- DBus 进程间的通信
- 基于DBus的进程间通信(IPC)
- DBUS与QDBUS的应用
- Bluedroid与BluZ,蓝牙测试方法的变动(基于bludroid和BlueZ的对比)
- Bluedroid与BluZ,蓝牙测试方法的变动(基于bludroid和BlueZ的对比) .
- Bluedroid与BluZ,蓝牙测试方法的变动(基于bludroid和BlueZ的对比)
- dbus基础--创建dbus客户端与服务端的实例参考
- dbus基础--创建dbus客户端与服务端的实例参考
- 进程通信-dbus(1)
- dbus 通信分析
- DBUS 的 C 编程接口与配置
- dbus-daemon的启动与使用
- 再探DBUS-GLIB Binding,GLIB事件与DBUS事件是如何关联的
- DBUS-GLIB Binding,GLIB事件与DBUS事件是如何关联的
- DBus 入门与应用 -- DBus 的 C 编程接口
- DBus 入门与应用 -- DBus 的 C 编程接口
- 用DBUS实现Startup notification的构想
- 从OLE到SOA,SOA其实很简单!
- Spring bean之scope属性概述
- Ubuntu实践(7):更换内核
- //加载驱动
- 【转】MFC 利用控制台输出调试信息
- 用Dbus与android的bluz通信
- <meta-data>说明
- 【转】VC 对话框添加工具栏 以及RepositionBar 函数说明
- Java中获取当前系统时间
- //开始服务//停止服务
- ext里面包含了iframe 我怎么才能获得iframe里面的form对象呢
- 3D打印立起来
- Windows8降临是福是祸?
- java编程思想_009吸血鬼数字