Portrait of Edd Dumbill, taken by Giles Turnbull

Subscribe to updates

Feed icon Atom or RSS

or get email updates

What I make

a conference management web application

XTech Conference
a European web technology conference

Mono Bluetooth bindings

I took it into my head tonight to start writing bindings for Mono for libbtctl, which is the base C support for Bluetooth control in GNOME Bluetooth.

I originally planned to do it in 30 minutes, but it took about 90 before I had it going properly. I took the GTK# sources as a starting point. The basic method is that you use some Perl scripts to parse your C source and header files. They look for the common idioms of writing GObject-based code and generate an XML file describing your library. Then you run a code generator that turns the XML into C# source. This source is compiled into the final library.

These Perl scripts turned out to be very sensitive to the exact style the C code was written in. I had to clean up several parts of my code to look more like the GTK source. No bad thing in itself, but I got some very curious errors before I figured out this was the problem.

I've not had time to test the bindings out thoroughly. I've certainly not had time to write the ".metadata" files that can be used to clean up what gets exposed in the Mono class library. There's a lot of cruft that's sneaked through right now. However you should now be able to access all libbtctl functionality: device discovery, SDP queries, establish RFCOMM connections to Bluetooth devices.

I'm working hard on the OBEX support in libbtctl, detaching it from the higher application level where it used to be in GNOME Bluetooth. This will be in the next release, along with the Mono/C# and Python bindings and the start of HTML API documentation.

Here's the test program I wrote. It uses the not-very-useful synchronous interface for device discovery, but it illustrates the point well enough:

  namespace Btctl.Test {
     using System;
     using System.Runtime.InteropServices;
     using Btctl;
     using BtctlSharp;

     public class TestApp {
        static extern void g_type_init ();

        public static void Main (string[] args)
            g_type_init ();

            Controller ctl = new Controller ();

            ctl.AddDevice += new AddDeviceHandler (Add_Device);
            ctl.DiscoverDevices ();

        static void Add_Device (object obj, AddDeviceArgs args)
            Console.WriteLine ("Device {0} Class {1}", 
               args.Addr, args.Clsid);

blog comments powered by Disqus

You are reading the weblog of Edd Dumbill, writer, programmer, entrepreneur and free software advocate.
Copyright © 2000-2012 Edd Dumbill