dbus 0.0.0-dev.20

Dart native
Flutter Android iOS

A native Dart implementation of the D-Bus message bus client. This package allows Dart applications to directly access services on the Linux desktop.

Pub Package

A native Dart client implementation of D-Bus.

Accessing a remote object using dart-dbus #

The easist way to get started is to use dart-dbus to generate Dart objects to access existing D-Bus services. Start with a D-Bus interface definition:

<node name="/org/freedesktop/hostname1">
  <interface name="org.freedesktop.hostname1">
    <property name="Hostname" type="s" access="read"/>
  </interface>
</node>

The dart-dbus tool processes this interface to generate a Dart source file:

$ dart-dbus generate-remote-object hostname1.xml -o hostname1.dart

You can then use the generated hostname1.dart to access that remote object:

import 'package:dbus/dbus.dart';
import 'hostname1.dart';

var client = DBusClient.system();
var hostname1 = OrgFreeDesktopHostname1(client, 'org.freedesktop.hostname1');
var hostname = await hostname1.getHostname();
print('hostname: ${hostname}')
await client.close();

The code generated by dart-dbus may not be the cleanest API you want to expose for your service. It is recommended that you use the generated code as a starting point and then modifiy it as necessary.

Accessing a remote object manually #

You can access remote objects without using dart-dbus if you want. This requires you to handle error cases yourself. The equivalent of the above example is:

import 'package:dbus/dbus.dart';

var client = DBusClient.system();
var object = DBusRemoteObject(client, 'org.freedesktop.hostname1', DBusObjectPath('/org/freedesktop/hostname1'));
var hostname = await object.getProperty('org.freedesktop.hostname1', 'Hostname');
print('hostname: ${hostname.toNative()}');
await client.close();

Exporting an object on the bus #

import 'package:dbus/dbus.dart';

class TestObject extends DBusObject {
  @override
  DBusObjectPath get path {
    return DBusObjectPath('/com/example/Test');
  }

  @override
  Future<MethodResponse> handleMethodCall(String sender, String interface, String member, List<DBusValue> values) async {
    if (interface == 'com.example.Test') {
      if (member == 'Test') {
        return DBusMethodSuccessResponse([DBusString('Hello World!')]);
      } else {
        return DBusMethodErrorResponse.unknownMethod();
      }
    } else {
      return DBusMethodErrorResponse.unknownInterface();
    }
  }
}

var client = DBusClient.session();
client.registerObject(TestObject());
5
likes
100
pub points
61%
popularity

A native Dart implementation of the D-Bus message bus client. This package allows Dart applications to directly access services on the Linux desktop.

Repository (GitHub)
View/report issues

Documentation

API reference

Uploaders

marcus.tomlinson@canonical.com
ken.vandine@canonical.com
robert.ancell@canonical.com

License

LGPL 3.0 (LICENSE)

Dependencies

args, xml

More

Packages that depend on dbus