Error invoking method "Gadgeteer.Modules.GHIElectronics.Bluetooth" (check arguments to Program.BeginInvoke are correct)

Hi,

I dug out a project I worked on a year ago with Bluetooth using the drivers from Eduardo Velloso, I fired up the board and everything worked great. In that year though I upgraded my computer to Windows 8 and Visual Studio 2012. So installed the SDK and ensured that NetMF 4.2 was on the machine. The I updated the Firmware and bootloader on my Fez Cerberus board then I encountered problems.

System information:
Board: Fez Cerberus
TinyBooter: 4.2.6.1
TinyCLR: 4.2.6.1
Gadgeteer.dll: 2.42.0.0

Problems:

  1. Once I got all updated and compiled my solution, I get an error in the Bluetooth.cs file that suggested I may be missing a namespace as GTI.Serial was not recognized. I appears that Serial was removed from Gadgeteer.Interfaces. So I checked the developers guide for the Bluetooth module and noticed that I can now use the designer to add Bluetooth. I set out to change over my code.

  2. Now that my code was changed over I am getting the error: Error invoking method “Gadgeteer.Modules.GHIElectronics.Bluetooth” (check arguments to Program.BeginInvoke are correct) I have tried to work through this but it is not working.

Note: I have Bluetooth go into pairing mode only when a button is pushed but the device does not go into that mode (i.e. the red and blue leds do not alternate flashing, it only flashes blue.)

Code:


void ProgramStarted()
        {
            Debug.Print("Program Started");

            launchtimer = new GT.Timer(1000);
            launchtimer.Tick += new GT.Timer.TickEventHandler(launchtimer_Tick);

            bluetooth.SetDeviceName("Launcher");
            bluetooth.SetPinCode("1138");
            bluetooth.BluetoothStateChanged += bluetooth_BluetoothStateChanged;
            bluetooth.DataReceived += new Bluetooth.DataReceivedHandler(bluetooth_DataReceived);

            button.ButtonPressed += new Button.ButtonEventHandler(button_ButtonPressed);
            
        }


void bluetooth_BluetoothStateChanged(Bluetooth sender, Bluetooth.BluetoothState btState)
        {
            Debug.Print(btState.ToString());
        }


void button_ButtonPressed(Button sender, Button.ButtonState state)
        {
                bluetooth.ClientMode.EnterPairingMode();
        }


void bluetooth_DataReceived(Bluetooth sender, string data)
        {
            Debug.Print(Commands.ToString());
            Commands = data.Split(',');
            switch (Commands[0])
            {
                case "Channel":
                    DoStuff(Commands);
                    break;
                case "RGB":
                    SetStuff(Commands);           
                    break;
                default:
                    break;
            }

        }

Any ideas on how to get this resolved?

please show us the whole error message

This is everything that is posted in the Output window:

A first chance exception of type ‘System.NullReferenceException’ occurred in GadgeteerBlueToothTest.exe
Error invoking method “Gadgeteer.Modules.GHIElectronics.Bluetooth” (check arguments to Program.BeginInvoke are correct)

I suspect you have an old reference to your pre-compiled code still in existence.

Start simple. New Project, select the correct mainboard, drag the BT module in, put debug statements into handlers, and F5. If that works, then you know you have to clean up your old app

The old driver does not work with Cerberus and 4.2. Try this:
https://www.ghielectronics.com/community/codeshare/entry/763
Regards
Roland

So I tried Brett’s solution and created a brand new project. So I no longer get the first time exception or any of that but the problem remains that Bluetooth is not entering pairing mode. I had setup a button to enter pairing mode on button press and also set up a one time timer to do the same thing after 10 seconds. Nothing put it into pairing mode (red and blue led flashes).

I changed the device name in the new app form the older app and then checked Bluetooth devices on my phone and the old device name appeared (I had deleted it from the phone prior to testing).

Almost seems like the problem is the Bluetooth device, like it has been bricked or something. Is there a way to check for that?

@ RoSchmi - will try your suggestion shortly.

Because you’re interfacing over serial, any issue on serial lines or even timing issues can get in the road. Ro’s code is a good step to try… my personal thought is you just need to try the simplest setup (no name changes, nothing) and see if that works.

Ok I have made some progress using Brett’s method of starting simple. I found that my phone keeps the last device name used unless I turn the Bluetooth radio off and then back on. Also this resolves the issue with going into ClientModePairing.

The remaining issue is that the data collected by Bluetooth_DataReceived only seems to be half the data I sent it.

EventHandler signature:



Example of data sent:
Phone sends data as a string "RGB,XXX,XXX,XXX") where XXX == 0 -255
Gadgeteer board seems to receive half the data but does two reads for the full data, This problem did not exist previously.

I have just using Debug.Print(data); in the event handler for now. I will look @ RoSchmi's code tomorrow. In the meantime others have experienced this issue or know how to get past it that would be great.

I have confirmed that the phone app is sending the data all at one time.

Lets remember that “data received” events are NOT deterministic ways to get “full strings” over a serial connection. You always should be handling the data queuing in your own app and assemble data as you detect your own terminator.

Yes, this seems to have changed from NETMF 4.1 to 4.2, at least in the cerb family. Also the messages from the bluetooth circuit to the serial port of the mainboard in the module driver arrive in chunks so that they cannot be correctly parsed by the original driver code of Eduado Velloso and Brett. To relieve from this issue, I integrated a circular buffer in the driver, so that the Bluetooth module gets complete strings (delimiter Hex 0x0A) which then can be correctly parsed by the driver code and then are send as complete strings through the eventhandler to the application code.
Kind Regards
Roland

@ RoSchmi - Your code got me back in business. So frustrating that the switch from NetMF 4.1 to 4.2 broke what I had already working. You saved me a bunch of more hours trying to figure out the issue.

@ Brett thank you for your suggestions as well. I was nearly there with them except for the whole byte array and needing to pass a terminator value.

great to hear you’re on your way.

I always look at serial data as a gift. If you get everything you need in an interrupt-handler method, you’ve got a great gift, if you get it in dribs and drabs, well at least it arrived. (seriously, you were lucky if your previous experiences delivered it in chunks you wanted, by design a data received event needs to just present what it has)

@ ChicagoWay -
Hi,
Im happy that it worked for you. Please tell me if you have any issues with the driver or if you have suggestions for improvements.
Roland

OK, guys. This has been great, but I’m in the same boat as everyone else seems to be when it comes to Bluetooth. Out of the box example code doesn’t work. When I finally get it to pair (by trying over and over and over) it only stays connected for about 6 seconds and the Bluetooth object never shows a connected state.

I am trying to use your driver, RoSchmi and I don’t have exactly the same configuration. I download and it compiles just fine. But I can’t really test it.

BUT… I have a Cobra II WiFi and the Bluetooth module.

I noticed that the Bluetooth_Pairing_Test is using a Cerberus (which I have ordered) and a Serial module (which I did not).

I know this is going to sound really dumb, but I thought all I needed was a Bluetooth module to do this communication. Isn’t it providing it’s own UART and serial capabilities?

I have my Windows 8 desktop configured with COM3 assigned to the Bluetooth adapter and Tera Term opened on COM3.

I figured the Bluetooth module would connect, then accept the data I was sending it, forward it on to the desktop Bluetooth adapter and Tera Term would show me the results.

In the long term, I’m trying to hook this up to my Windows phone Bluetooth to get data from the NETMF board (whichever one I end up using).

What am I missing here?

Also, I might actually be better served with NFC as the communications link. Would that be easier than Bluetooth?

Thanks in advance, guys.

@ willy -
Hi Willy,
you do not need an rs232 module to use my driver. It was only included for tests to send the messages received over bluetooth back to the PC where they came from. Delete the rs232 module in the designer and outcomment the code segements concerning rs232.
For Cobra II set: MySocketNumber = 5
Do not include the bluetooth module in the designer but connect it physically to socket 5 of the Cobra II.
Cheers
Roland

Edit: On the PC side it works with teraterm but I suggest to use the 32feet.net library.
How it works you can see on my other Code Share entry
PC/ Bluetooth listener App https://www.ghielectronics.com/community/codeshare/entry/729
(has some minor bugs)
and FEZ/PC Bluetooth File Transfer Server/Client
https://www.ghielectronics.com/community/codeshare/entry/823
or this Project:
Controlling a Gadgeteer device using Bluetooth Module and 32feet.NET library | Integral Design

here is a link to a Windows phone App

The other thing to know is that often the serial port on the pc side only becomes active after you have paired, so the process should be pair them, check the device manager com port number, connect to any Bluetooth related ones and see if you get data coming back. If not, try them all before giving up.

Thanks!

I’m on it, first thing tomorrow and I’ll let you know.

1 Like

[edit] The latest update to WP8 doesn’t include a standard API for SPP, which is one of the things that is making this harder than it should be. There is sample code (which I will try) that claims to make it work, but so far I haven’t had any luck.)
[/edit]

I thought I would post a little something here, just in case it’s correct.

I am trying to hook Cerberus/BT module to a WP8 app. One of the connection properties allows for a “service class” guid to be specified.

I can’t find any reference to what the GHI BT module would be using, so I looked around and found this:

{00001101-0000-1000-8000-00805f9b34fb}

As it turns out, you can find that information here: https://www.bluetooth.org/en-us/specification/assigned-numbers/service-discovery

It shows the base UUID as: 00000000-0000-1000-8000-00805F9B34FB

And then later in the document is shows SPP as 0x1101.

So I guess that’s how that service class is being generated. I see references around the net for folks using the GUID above and having some success with it. I’m still not. :frowning:

Well, I have at least established some consistency…

I thought pairing was like establishing a connection in wifi. Once the network connection was made, you were finished.

But I now believe that Bluetooth “pairing” really isn’t completed when you simply enter the PIN code. At that point the module’s red and blue lights are still going even though you’ve typed in your PIN and the phone says “connected”. The Bluetooth module doesn’t change it’s state and doesn’t fire a state change event.

But, if you launch your app and attempt to connect to the device - and quickly, too - the connected event fires and now the little guys know each other.

When that happens, the pairing seems to be complete and after that, everything seems to work as advertised.

From that point on, I can drop the connection and the Bluetooth status reflects it and then reconnect without attempting to pair again.

Wish I had known that little fact about 4 days ago. :slight_smile:

Thanks for all the info, guys.
:clap:

Hi Willy,
which driver did work for you on the cerberus side? Did you get the connection to a Windows PC working?
Did you try the aproach of this link

on the Windows Phone side? I don’t have a Windows Phone but I thinks this aproach is the most promising.
Cheers
Roland