This project has moved. For the latest updates, please go here.

BlueZ on Linux

We have support for the BlueZ Bluetooth protocol stack on Linux. We support running on the Mono CLI there, but require a patch to the Mono runtime as it blocks Bluetooth Sockets by default. Our BlueZ support is not included in our release versions but is included in a DEBUG build, so you need to build the library yourself to use this (and for instance you can add a definition of BlueZ in the project file if you want to include it in a release build).

Currently we have the following support:
  • Radio: Well supported, for instance we list the values properly (Address, Name, CoD, Mode and HardwareStatus), and do also allow setting the Mode. We will list all the installed radios, but see below for multi-radio usage.
  • Client.Connect: Supported. No support for Authenticate/Encrypt/SetPin etc.
  • Data transfer: BlueZ uses Sockets for data transfer so everything should be fine.
  • Listener: Supported, including advertising SDP record. Again no support for Auth/Encrypt/SetPin.
  • Client.DiscoverDevices: Good support for discovering both in-range and remembered devices, ‘live’ discovery is also supported. Note that the discovery support on BlueZ lists in-range devices only when first discovered; which is differenct from other stacks. More work is needed to use the arrival and departure events to provide the same behaviour as on other stacks
  • Security: RemoveDevice supported. PairRequest requires us to run a D-Bus Agent and that wouldn't work for me.
As noted above BluetoothClient.Connect and BluetoothListener use Sockets. However current versions of Mono (2.8) do not support opening or using Bluetooth sockets. My old pal Andy Hume and I have created a patch to Mono to enable this support, and have posted it as Mono/Novell bug id bug 655153. Use the second patch and not the first. LATER: See also new bug 262

Apply that patch to your Mono source code, rebuild, test and let me have your comments… Other dependencies required include managed assembly NDesk.DBus.dll and native library libbluetooth. For the latter install the necessary package, for instance on Ubuntu that's package libbluetooth-dev. For the former there's a copy in our sources at "ExternalLibs/Mono etc/NDesk.DBus.dll". There's presumably also a package for that.

Also see http://32feetnetdev.wordpress.com/2011/07/25/sample-bluez-logging/ for sample logging to compare against what you see. To see debug logging on Mono use the following command first: export MONO_TRACE_LISTENER=Console.Error

As a mega hack, if you want to test device discovery before you've patched Mono's sockets support you can enable the HACK_FAKE_SOCKET_TO_ALLOW_DISCOVERDEVICES_TEST hack at the top of BluezClient.cs.

Multi-Radio

As noted above, unlike apparently every stack on Windows, BlueZ does support multiple radios. We do list all the radios and BluetoothClient and BluetoothListener will bind to a particular local address if requested to do so. However the other functionality does not currently ensure it runs on a particular radio, DiscoverDevices and GetServiceRecords (and as used internally by Connect) will use the primary radio.

APIs used

We using the original BlueZ ‘hci’ API for some things and the D-Bus API for the rest.

Since the original BlueZ ‘hci’ API seems deprecated -- it is included in that 'dev' package -- we should look at changing to only using the D-Bus API.

Note that if the D-Bus managed assembly is missing, then very unhelpful error message shown below occurs. Note that it doesn't mention any assembly/library name. I'll have a check to see if there's anything we as a consumer of that library can do.
Exception creating factory 'InTheHand.Net.Bluetooth.BlueZ.BluezFactory, ex: System.TypeLoadException: A type load exception has occurred.
  at (wrapper managed-to-native) System.Reflection.MonoCMethod:InternalInvoke (System.Reflection.MonoCMethod,object,object[],System.Exception&)
  at System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0

Last edited Mar 30, 2013 at 5:40 PM by alanjmcf, version 17

Comments

MichaelLep Sep 21, 2013 at 10:22 AM 
Thanks for the info but it really needs some work to be of use to anyone.

1. Use the second patch and not the first? Please link to the exact patch file..
2. How do you install this patch file in Linux? Detailed instructions would be good.
3. Is this patch still required with Mono 3?
4. Debug Build? Link to the exact build please.

r0otsharp Jul 17, 2011 at 10:12 AM 
hi .

plz put in here sample for work bluez on linux with c# and mono .

thanks.