Bluetooth in Linux with Bluez and Mono

Topics: Bluetooth - Other
Jan 18, 2013 at 4:54 PM

Hi,

I've been using 32feet in windows for about a year, and am looking at doing the same thing in linux. I've seen the wiki/posts about patching mono, and I haven't tried that yet, because I wanted to make sure the basics worked before I went any further.

So far, I've got the DLL working within mono, but the command

BluetoothRadio[] radio = BluetoothRadio.AllRadios;
returns an empty array with the following coming up on the output widow:

System.DllNotFoundException: Irprops.cpl
System.Reflection.TargetInvocationException: '32feetWidcomm' status exception: System.EntryPointNotFoundException: GetModuleHandle
System.DllNotFoundException: BsSDK

and if I include any more involved stuff I get an error to the effect of 'could not find bluetooth stack'. So it looks like it's not seeing the BlueZ stack, but I've definitely got some kind of bluetooth stack running because I have the drop-down menu on my taskbar with bluetooth on and visible.

I'm wondering if this is because I have a different version of BlueZ, so what versions of BlueZ has InTheHand been tested with?

Alternatively, are there any setup steps necessary to set the right stack that I might have missed?

Thanks,

Rory

Developer
Jan 20, 2013 at 7:41 PM

I've just added the following to the BlueZ docs page. Sorry for not mentioning it there before.

This code is not included in the release versions but it included in DEBUG builds, 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).

Also see http://32feetnetdev.wordpress.com/2011/07/25/sample-bluez-logging/ for sample logging to compare against what you see.

Let me know what you find.


Jan 25, 2013 at 4:50 PM

That doesn't seem to have helped. I'm reasonably sure I've now built it properly, and within Mono it's perfectly happy to use the namespace InTheHand.Net.Bluetooth.BlueZ, so I assume it's now included.

BluetoothRadio.AllRadios still gives an empty array, and if I create an new BluetoothClient() I get the following:

Unhandled Exception: System.PlatformNotSupportedException: No supported Bluetooth protocol stack found.
  at InTheHand.Net.Bluetooth.Factory.BluetoothFactory.GetStacks_inLock () [0x00000] in <filename unknown>:0
  at InTheHand.Net.Bluetooth.Factory.BluetoothFactory.get_Factories () [0x00000] in <filename unknown>:0
  at InTheHand.Net.Bluetooth.Factory.BluetoothFactory.get_Factory () [0x00000] in <filename unknown>:0
  at InTheHand.Net.Sockets.BluetoothClient..ctor () [0x00000] in <filename unknown>:0
  at BT_blueZ.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.PlatformNotSupportedException: No supported Bluetooth protocol stack found.
  at InTheHand.Net.Bluetooth.Factory.BluetoothFactory.GetStacks_inLock () [0x00000] in <filename unknown>:0
  at InTheHand.Net.Bluetooth.Factory.BluetoothFactory.get_Factories () [0x00000] in <filename unknown>:0
  at InTheHand.Net.Bluetooth.Factory.BluetoothFactory.get_Factory () [0x00000] in <filename unknown>:0
  at InTheHand.Net.Sockets.BluetoothClient..ctor () [0x00000] in <filename unknown>:0
  at BT_blueZ.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0

 

What's the most basic BlueZ function I could use to test this, and what specific namespace(s) do I need to use? I feel like either I'm missing something really basic, I've built it wrong somehow, or I need an older version of BlueZ.

I'll keep trying, and have a closer look at the build options in case there's anything obvious there,

thanks,

Rory

Developer
Jan 28, 2013 at 3:57 PM

I'll get some to test this here soon.

Did you get logging on the console etc and does it look like that at http://32feetnetdev.wordpress.com/2011/07/25/sample-bluez-logging/

Jan 28, 2013 at 4:58 PM
Edited Jan 28, 2013 at 5:44 PM

I don't get any logging at all, but I think that's because I don't have mono set up to do anything with Debug.Assert or Debug.WriteLine - I need some sort of trace listener, which I haven't got working yet.

I put in a few judicious Console.WriteLine statements, and I see some stuff like the first bit of the first sample logging. It looks like the DLL goes through all the possible stacks and tries to set each one up in turn - and all of them have some sort of 'couldn't find DLL' type of error.

In fact the first error I get looks like exactly the same one as in the sample logging, which for the microsoft stack makes perfect sense - it doesn't exist. The next error is from looking for the BlueZ stack, and I get:

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

 

I'll try and get the trace listener working, if that is where the logging should be happening, as that looks like it'll give a lot more info.

EDIT: I've now got logging, and it doesn't seem to give much more than that - the relevant bit is still the type load exception as above. I can give you the whole lot though if you think it'd help.

Rory

Developer
Jan 28, 2013 at 10:25 PM

I've just set-up a clean Ubuntu box, and the short answer is that I needed to copy NDesk.DBus.dll into the same folder as the app and secondly ensure that a particular Bluetooth library was required.  So firstly the error you show there is fixed by supplying the NDesk.DBus.dll Assembly being required. Its a shame that using the managed DBus API fails in that way when the library is missing. I used the copy in the sources: "ExternalLibs/Mono etc/NDesk.DBus.dll".

The second thing I required was "libbluetooth". On ubuntu that was supplied by package "sudo apt-get install libbluetooth-dev".

After that getting the radio worked. And of course, since I was using a standard Mono, "new BluetoothClient()" failed with the message about socket family.

Hope that helps.

In the longer term we should get rid of the dependency on that -- presumably old-ish libray -- and go fully to the DBus API.

 

(The complete set of packages was the following -- the middle three presumably only required since I was building the library and apps on the ubuntu box.

mono-runtime
mono-xbuild
mono-devel
mono-gmcs
libbluetooth-dev

)

Jan 29, 2013 at 12:22 PM

Fantastic! It's seeing the BlueZ radio, now all I need to do is patch Mono. Thanks so much for your help,

 

Rory

Developer
Jan 30, 2013 at 12:04 PM

Cool. Let me know if the patch needs updated, it might do if the Mono native sockets code has changed.

Apr 11, 2015 at 11:40 PM
i need to know how to setup 32feet on monodevelop am using debian/ubuntu can someone help me plz ?
May 17, 2015 at 5:31 PM
I am having the same issue, I could solved System.DllNotFoundException: BsSDK using the version 3.7, compiled from library source, but I couldn't figured out how to patch Mono and make BluetoothClient() work.