Cant receive signals from Adapter (low-level dbus API)

"Fritz Code" <[email protected]> Tue, 20 May 2008 10:04:21 +0200
Newsgroups gmane.linux.bluez.user
Message-ID <[email protected]>
Hi,

attached is a small test application.
With this application I want to test the handling of signals from bluez
Adapter.
Therefore I always change the name of my bluetooth device, in order to make
the adapter to
generate a signal.
Unfortunately I cant receive this signal.
I can set and get the name correctly.
BUt it seems I receive at least some signals:
When I start the application I get a signal with value :1.20
If I remove my usb bluetooth dongle I get a signal with value off.




In another application I had the same problem (receiving not the expected
signals: e.g.: DiscoveryStarted() ... )
 when I sent signal DiscoverDevices.


thanks.

-- 
Regards,
--Codefritz

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft 
Defy all challenges. Microsoft(R) Visual Studio 2008. 
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users
test.c (text/x-csrc, 4.5 KB)
#include <stdio.h>
#include <stdlib.h>
#include <dbus/dbus.h>


int main(int argc, char **argv)
{
    DBusConnection *conn;
    DBusMessage *msg, *reply;
    DBusError err;
    DBusMessageIter args;
    const char *addr;
    const char *name;
    const char *manuf;
    void *sigvalue;

    char dispMsg[10];
    char *names[5] = { "test1", "test2", "test3", "test4", "test5"};

    int i=0;

    dbus_error_init(&err);

    printf("Connecting to the bus ...\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) { 
      exit(1);
    }
    
    

    // add a rule for which messages we want to see    
    dbus_bus_add_match(conn, 
 	 	       "type='signal',interface='org.bluez.Adapter'", 
	   	       &err); // see signals from the given interface
    dbus_connection_flush(conn);
    if (dbus_error_is_set(&err)) { 
      fprintf(stderr, "Match Error (%s)\n", err.message);
      exit(1); 
    }    
    
    i=0;

    while (1) {      
      dbus_connection_read_write(conn, 0);
      msg = dbus_connection_pop_message(conn);      

      if (NULL == msg) { 
	printf("GetName():");
	msg = dbus_message_new_method_call("org.bluez", "/org/bluez/hci0",
					   "org.bluez.Adapter", "GetName");
	reply = dbus_connection_send_with_reply_and_block(conn, msg, -1, NULL);
	dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
	printf(" %s\n", name);
	dbus_message_unref(msg);
	dbus_message_unref(reply); 

	if (i==4)
	  i=0;
	  
	printf("SetName(): %s\n", names[i++]);
	msg = dbus_message_new_method_call("org.bluez", "/org/bluez/hci0",
					   "org.bluez.Adapter", "SetName");
	if (!dbus_message_append_args(msg,
				      DBUS_TYPE_STRING, &names[i],				      
				      DBUS_TYPE_INVALID)) {
	  fprintf(stderr, "Ran out of memory while constructing args\n");
	  exit(EXIT_FAILURE);
	}
	dbus_message_set_no_reply(msg, TRUE);	
	if (!dbus_connection_send(conn, msg, NULL)) {
	  fprintf(stderr, "Ran out of memory while queueing message\n");
	  exit(EXIT_FAILURE);
	}
	
	dbus_connection_flush(conn);	
	dbus_message_unref(msg);	
	
	sleep(1);
	continue;
      }
      
      printf("!!got a message!!\n");
      // check if the message is a signal from the correct interface
      // and with the correct name
      //    if (dbus_message_is_signal(msg, "org.bluez.Adapter", "NameChanged")) {
	// read the parameters
	if (!dbus_message_iter_init(msg, &args))
	  fprintf(stderr, "Message has no arguments!\n"); 
	else if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&args)) 
	  fprintf(stderr, "Argument is not string!\n"); 
	else {
	  dbus_message_iter_get_basic(&args, &sigvalue);
	  printf("Got Signal with value %s\n", (char*)sigvalue);
	}
	//      }
      
      // free the message
      dbus_message_unref(msg);
    }      




      return 0;







    /*
    printf("DiscoverDevices():\n");
    msg = dbus_message_new_method_call("org.bluez", "/org/bluez/hci0",
				       "org.bluez.Adapter", "DiscoverDevices");
    if (!dbus_connection_send (conn, msg, NULL))
        fprintf (stderr, "error sending message\n");
    dbus_message_unref(msg);
    dbus_connection_flush(conn);
    */



      /*

    printf("GetAddress():");
    msg = dbus_message_new_method_call("org.bluez", "/org/bluez/hci0",
				       "org.bluez.Adapter", "GetAddress");
    reply = dbus_connection_send_with_reply_and_block(conn, msg, -1, NULL);
    dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &addr, DBUS_TYPE_INVALID);
    printf(" %s\n", addr);
    dbus_message_unref(msg);
    dbus_message_unref(reply); 



    printf("GetManufacturer():");
    msg = dbus_message_new_method_call("org.bluez", "/org/bluez/hci0",
				       "org.bluez.Adapter", "GetManufacturer");
    reply = dbus_connection_send_with_reply_and_block(conn, msg, -1, NULL);
    dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &manuf, DBUS_TYPE_INVALID);
    printf(" %s\n", manuf);
    dbus_message_unref(msg);
    dbus_message_unref(reply); 





    printf("GetName():");
    msg = dbus_message_new_method_call("org.bluez", "/org/bluez/hci0",
                                                   "org.bluez.Adapter", "GetName");
    reply = dbus_connection_send_with_reply_and_block(conn, msg, -1, NULL);
    dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
    printf(" %s\n", name);
    dbus_message_unref(msg);
    dbus_message_unref(reply); 




    dbus_connection_close(conn);

    return 0;*/
}