how to find rssi and distance using bluez api in l

2019-08-13 23:14发布

问题:

I am trying to get distance from a bluetooth device using rssi parameters of Bluez Api. I have downloaded a code from internet and running it. The error uggests me that method "DefaultAdapter" is missing. But I do not know the signature of this method. May be there i some other error alo. I am pasting my code below: Any other simpler code link is good. I get error Unable to get managed objects: GDBus.Error:org.freedesktop.DBus.Error.UnknownMethod: Method "DefaultAdapter" with signature "" on interface "org.bluez.Manager" doesn't exist, getting distance through rssi using bluez apis

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 );
}

I got another code from internet. It had some problem as was originally posted. But the person had written that he was not making connection to server before ioctl. He corrected it and it worked. I downloaded his code and getting the same problem. the link to that is https://communities.intel.com/thread/100186

can someone suggest anything.