我正在尝试使用Bluez Api的rssi参数与蓝牙设备保持距离。我从互联网上下载了代码并运行它。该错误告诉我缺少方法“DefaultAdapter”。但我不知道这种方法的签名。可能在那里我还有其他一些错误。我在下面粘贴我的代码: 任何其他更简单的代码链接都是好的。 我收到错误 无法获取托管对象:GDBus.Error:org.freedesktop.DBus.Error.UnknownMethod:接口“org.bluez.Manager”上带有签名“”的方法“DefaultAdapter”不存在,使用bluez apis获取rssi的距离< / p>
pthread_t threadId;
#define BLUEZ_BUS_NAME "org.bluez"
#define BLUEZ_INTF_ADAPTER "org.bluez.Adapter"
static GDBusConnection *dbus_conn = NULL;
static GMainLoop *main_loop;
struct handler_data {
const char *bt_address;
short rssi;
};
/*
* Callback function for the DeviceFound signal from the org.bluez.Adapter
* interface.
*/
static void
device_found_handler (GDBusConnection *connection,
const gchar *sender_name,
const gchar *object_path,
const gchar *interface_name,
const gchar *signal_name,
GVariant *parameters,
gpointer user_data)
{
char *device_address;
gboolean res;
short rssi;
GVariant *property_dict;
struct handler_data *data = (struct handler_data *)user_data;
/*
* Paramter format: sa{sv}
* Only interested in the RSSI so lookup that entry in the properties
* dictionary.
*/
g_variant_get(parameters, "(&s*)", &device_address, &property_dict);
if (strcmp(data->bt_address, device_address)) {
/* Not the device of interest */
return;
}
res = g_variant_lookup(property_dict, "RSSI", "n",
&rssi);
if (!res) {
printf("Unable to get device address from dbus\n");
g_main_loop_quit(main_loop);
return;
}
data->rssi = rssi;
g_main_loop_quit(main_loop);
}
/*
* Gets the RSSI for a given BT device address.
*/
static short
get_rssi(const char *bt_address)
{
GError *error = NULL;
GVariant *reply = NULL;
char *adapter_object = NULL;
struct handler_data data;
data.bt_address = bt_address;
main_loop = g_main_loop_new(NULL, FALSE);
if (!main_loop) {
printf("Error creating main loop\n");
return 0;
}
dbus_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
if (error) {
printf("Unable to get dbus connection\n");
return 0;
}
/* Get the default BT adapter. Needed to start device discovery */
reply = g_dbus_connection_call_sync(dbus_conn,
BLUEZ_BUS_NAME,
"/",
"org.bluez.Manager",
"DefaultAdapter",
NULL,
G_VARIANT_TYPE("(o)"),
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error);
if (error) {
printf("Unable to get managed objects: %s\n", error->message);
return 0;
}
g_variant_get(reply, "(&o)", &adapter_object);
/* Register a handler for DeviceFound signals to read the device RSSI */
g_dbus_connection_signal_subscribe(dbus_conn,
NULL,
"org.bluez.Adapter",
"DeviceFound",
NULL,
NULL,
/*0*/G_DBUS_SIGNAL_FLAGS_NONE,
device_found_handler,
&data,
NULL);
/* Start device discovery */
reply = g_dbus_connection_call_sync(dbus_conn,
BLUEZ_BUS_NAME,
adapter_object,
"org.bluez.Adapter",
"StartDiscovery",
NULL,
NULL,
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error);
if (error) {
printf("Unable to start discovery: %s\n", error->message);
return 0;
}
g_main_loop_run(main_loop);
return data.rssi;
}
void* getSetDeviceDetails(void * arg)
{
char* destinationAddress = new char[strlen((char*)arg)+1];
strcpy(destinationAddress, (char*)arg);
short rssi = 0;
rssi = get_rssi(destinationAddress);
cout<< "rssi=" << rssi<< endl;
}
void scanDevices(inquiry_info **ii, int * sock, int * num_rsp)
{
int max_rsp;
int dev_id, len, flags;
int i;
dev_id = hci_get_route(NULL);
*sock = hci_open_dev( dev_id );
if (dev_id < 0 || sock < 0) {
perror("opening socket");
exit(1);
}
len = 8;
max_rsp = 255;
flags = IREQ_CACHE_FLUSH;
*ii = (inquiry_info*)malloc(max_rsp * sizeof(inquiry_info));
*num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, ii, flags);
if( num_rsp < 0 ) perror("hci_inquiry");
printf("num devices = %d\n",*num_rsp);
}
int main()
{
inquiry_info *ii = NULL;
int num_rsp, i, sock;
char addr[19] = { 0 };
char name[248] = { 0 };
scanDevices(&ii, &sock, &num_rsp);
close( sock );
char destinationAddress[18];
ba2str(&(ii)->bdaddr, destinationAddress);
cout<<destinationAddress<<"print"<<endl;
pthread_create(&threadId,0,getSetDeviceDetails,(void*)destinationAddress);
pthread_join(threadId,0);
free( ii );
}
我从互联网上获得了另一个代码。它最初发布时遇到了一些问题。但是这个人写过他在ioctl之前没有连接到服务器。他纠正了它并且有效。 我下载了他的代码并遇到了同样的问题。 链接到那 https://communities.intel.com/thread/100186
有人可以提出任何建议。