what causes the error "Unknown object" ? #272
|
I'm trying to work with Bluetooth LE with bluez, and for now start advertising. I have the following program : (I'm sorry, it's pretty big but I don't know what might help or not) // constants
namespace ble{
const std::string BLUEZ_SERVICE_NAME = "org.bluez";
const std::string ADAPTER_INTERFACE = BLUEZ_SERVICE_NAME + ".Adapter1";
const std::string ADVERTISING_MANAGER_INTERFACE = BLUEZ_SERVICE_NAME + ".LEAdvertisingManager1";
}<--! introspection xml to generate the adaptor (not all properties are here, but it works whith a python example) -->
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node name="/org/bluez/test/advertisement">
<interface name="org.bluez.LEAdvertisement1">
<method name="Release"/>
<property name="Type" type="s" access="read"/>
<property name="LocalName" type="s" access="read"/>
<property name="Discoverable" type="b" access="read"/>
</interface>
</node>// class implementing the adaptor
static const std::string PATH_BASE = "/org/bluez/test/advertisement";
class Advertisement : public sdbus::AdaptorInterfaces<org::bluez::LEAdvertisement1_adaptor>{
private:
std::string ad_type;
std::string local_name;
bool discoverable;
public:
Advertisement(sdbus::IConnection& bus, int index, std::string advertising_type)
: AdaptorInterfaces(bus, std::move(PATH_BASE + std::to_string(index)))
, ad_type(advertising_type)
, local_name("RPI 4B")
, discoverable(true){
registerAdaptor();
}
~Advertisement(){
unregisterAdaptor();
}
...
// methods// main
typedef std::unique_ptr<sdbus::IConnection> Bus;
typedef std::unique_ptr<sdbus::IProxy> Proxy;
Bus bus;
/**
* @brief find the path to an adapter implementing the org.bluez.LEAdvertisingManager1 interface, if any
*
* @return sdbus::ObjectPath the path to such an adapter if found, else an empty path
*/
sdbus::ObjectPath find_adapter(){
Proxy proxy = sdbus::createProxy(ble::BLUEZ_SERVICE_NAME, "/");
typedef std::map<sdbus::ObjectPath, std::map<std::string, std::map<std::string, sdbus::Variant>>> ManagedObjects;
ManagedObjects objects;
proxy->callMethod("GetManagedObjects").onInterface(ble::DBUS_OM_IFACE).storeResultsTo(objects);
for(ManagedObjects::iterator it = objects.begin(); it != objects.end(); it++){
if(it->second.find(ble::ADVERTISING_MANAGER_INTERFACE) != it->second.end()){
return it->first;
}
}
return sdbus::ObjectPath();
}
int main(){
bus = sdbus::createSystemBusConnection();
sdbus::ObjectPath adapter_path(find_adapter());
if(adapter_path.empty()){
std::cout<<ble::ADVERTISING_MANAGER_INTERFACE<<" interface not found"<<std::endl;
}
else{
Proxy proxy = sdbus::createProxy(ble::BLUEZ_SERVICE_NAME, adapter_path);
proxy->setProperty("Powered").onInterface(ble::ADAPTER_INTERFACE).toValue(sdbus::Variant(true));
Advertisement adv(*bus, 0, "peripheral");
sdbus::ObjectPath adv_path(adv.getObjectPath());
std::cout<<"Registering advertisement..."<<std::endl;
proxy->callMethod("RegisterAdvertisement").onInterface(ble::ADVERTISING_MANAGER_INTERFACE).withArguments(adv_path, std::map<std::string, sdbus::Variant>());
std::cout<<"Advertising forever..."<<std::endl;
bus->enterEventLoop();
proxy->callMethod("UnregisterAdvertisement").onInterface(ble::ADVERTISING_MANAGER_INTERFACE).withArguments(adv_path);
}
return 0;
}In the console I can see in dbus-monitor. What am I missing/doing wrong ? Edit : Also the services names are always :1.x, :1.x+2 and :1.x+3. |
Replies: 1 comment
|
Ok, I re-read the main page to understand sdbus-c++, I saw that I could register my proxies on the same connexion as my object, I didn't have the error "Unknown object" anymore, but a timeout... something I remembered I had 2 weeks ago. Then I though it might be because I am not in asyncronous mode and the method return is void, and I saw the I'm so glad I found the problem and I don't have to use a cursed c implementation of dbus like bluez did ! |
Ok, I re-read the main page to understand sdbus-c++, I saw that I could register my proxies on the same connexion as my object, I didn't have the error "Unknown object" anymore, but a timeout... something I remembered I had 2 weeks ago.
Then I though it might be because I am not in asyncronous mode and the method return is void, and I saw the
dontExpectReply(), used it, and it works !!I'm so glad I found the problem and I don't have to use a cursed c implementation of dbus like bluez did !