I don’t have the Seeed oledDisplay so I commented out that part of the code. My first goal was just to get a message from the PC that a Bluetooth device was attempting to connect.
However, when I run my code I get the following error:
The type or namespace name ‘Serial’ does not exist in the namespace ‘Gadgeteer.Interfaces’ (are you missing an assembly reference?)
The error would seem to imply that I am missing one of my ‘libraries? I am currently running the GHI NETMF v4.2 and Gadgeteer Package July 23rd and I have installed the 32feet.NET [b]3.5.
I have in[/b]cluded my code below. Thank you to all for taking a look at my issue.
using Syst
em;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using Gadgeteer.Modules.Velloso;
namespace GadgeteerBluetooth
{
public partial class Program
{
private Bluetooth bluetooth;
private Bluetooth.Client client;
// This method is run when the mainboard is powered up or reset.
void ProgramStarted()
{
// oledDisplay.SimpleGraphics.Clear();
bluetooth = new Bluetooth(2);
client = bluetooth.ClientMode;
bluetooth.SetDeviceName("Gadgeteer");
bluetooth.SetPinCode("1234");
bluetooth.DataReceived += new Bluetooth.DataReceivedHandler(bluetooth_DataReceived);
client.EnterPairingMode();
// Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
Debug.Print("Program Started");
}
private void bluetooth_DataReceived(Bluetooth sender, string data)
{
/*
oledDisplay.SimpleGraphics.ClearNoRedraw();
oledDisplay.SimpleGraphics.DisplayText(data, Resources.GetFont(Resources.FontResources.NinaB), GT.Color.Yellow, 0, 60);
*/
}
}
}
I suspect that Bluetooth module driver needs to be recompiled for 4.2. See if you can do it yourself if not then leave a comment at the original blog post for Marco to help you out.
In fact, it might be worth just grabbing my CS file and add it to your project - if there are any unresolved references before you start you should be able to unravel that reasonably simply.
One thing I did note was that the module takes a long time to stabilise and be ready to initiate pairing, so I had to move the client.enterpairingmode() function out of ProgramStarted and I decided to put it in a joystick button pressed handler. Once the module stopped spitting out comms after it initialised, I press the button and then I entered pairing mode (blue/red LED flashes), and then I could connect to it from my PC.
In 4.2, SerialPort was moved to Microsoft.SPOT.Hardware.SerialPort. Make sure you have that reference. I have a 4.2 program working just fine based on Michael’s example. It’s still in progress but the basic communications are working and you can check it out here:
I have looked closer at the 4.2 Gadgeteer code and it looks like the Serial class (which wraps NetMF SerialPort internally ) is completely gone from Gadgeteer 4.2.
That Serial class is used by Bluetooth.cs.
If you copy it from Codeplex as is and add it to your solution (preserve the name space), then you should be fine.
Thank you for both taking so much time to share your experience and offer guidance to help me find a solution to my problem with the Bluetooth.
I must admit that the world of coding has intimidated me for some time, I’m more of a tactile person but you can only get so much satisfaction from building an artefact before you want to breathe life into it (give it a brain and a heart).
I have implemented the last suggestion by Architect and it has worked. Thank you for also explaining to me why it was necessary to change the software reference and the change in the SDK that caused it.
I am currently building and debugging the code. So far I have not been able to detect the module (I have verified that my PC’s dongle is working by connecting to other devices). The information shown in the Output window is included below:
Gentlemen did any of you encounter a similar output from your system during development?
The thread ‘’ (0x2) has exited with code 0 (0x0).
Using mainboard GHI Electronics FEZCerberus version 1.1
Reader Thread
Client Mode
Enter Pairing Mode
Program Started
WORK:SLAVER
+BTSTATE:0
+STWMOD=0
+STNA=Gadgeteer
+STPIN=1234
+INQ=1
OK
OK
OK
OK
+BTSTATE:1
+BTSTATE:3
WORK:SLAVER
+BTSTATE:0
+BTSTATE:1
+BTSTATE:3
CONNECT:FAIL
+BTSTATE:1
The thread ‘’ (0x3) has exited with code 0 (0x0).
The program ‘[2] Micro Framework application: Managed’ has exited with code 0 (0x0).
Glad you have sorted it out. Did you see Brett’s suggestion on moving pairing code to a button press instead of trying to do it right away. See if this helps. If it does then may be put a delay and let bluetooth module initialize fully before trying to pair.
If you look at my sample app that I linked to previously you will see that it uses Brett’s pairing technique and is already running on 4.2. Any issues you run into have probably already been resolved there.
My apologies for not responding earlier, my wireless was down and I am only now seeing your post.
Thank you for pointing the resource out to me, I should have reread the previous post with greater care!
I have managed to get Brett’s modified code to build and I am currently debugging (the present code is included below). From the Microsoft Visual Studio’s (MSVS) Output window I am seeing that the module has entered ‘Pairing Mode’.
Now, from what I have read (http://support.verizonwireless.com/faqs/Bluetooth/faq.html) the Pairing Mode is the state the BT device enters when it is ready to be discovered and establish a connection. However, I am unable to ‘see’ the BT module using either my PC or cellphone. Does anyone have any suggestions as to what I can try to further troubleshoot?
Output window:
Using mainboard GHI Electronics FEZCerberus version 1.1
Program Started
Reader Thread
Client Mode
Enter Pairing Mode
The thread ‘’ (0x3) has exited with code 0 (0x0).
usingSystem;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using Gadgeteer.Modules.Velloso;
namespace Bluetooth_32feet_V2
{
public partial class Program
{
private Bluetooth bluetooth;
private Bluetooth.Client client;
// This method is run when the mainboard is powered up or reset.
void ProgramStarted()
{
// Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
Debug.Print("Program Started");
// Joystick press event handler
joystick.JoystickPressed += new Joystick.JoystickEventHandler(joystick_JoystickPressed);
// set up a timer to fire every second
GT.Timer timer = new GT.Timer(1000);
timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
timer.Start();
// define a bluetooth module running at 460800 baud on socket 6
bluetooth = new Bluetooth(6, 460800);
// define a client mode connection to the bluetooth module
client = bluetooth.ClientMode;
// set up bluetooth module connection parameters
bluetooth.SetDeviceName("FezCerberus"); // change this to whatever name you want
bluetooth.SetPinCode("9999"); //likewise, set whatever PIN you want.
// need a handler for state changes and data recieved.
bluetooth.BluetoothStateChanged += new Bluetooth.BluetoothStateChangedHandler(bluetooth_BluetoothStateChanged);
bluetooth.DataReceived += new Bluetooth.DataReceivedHandler(bluetooth_DataReceived);
// put the device in pairing mode as part of a normal execution
client.EnterPairingMode();
}
void joystick_JoystickPressed(Joystick sender, Joystick.JoystickState state)
{
// check to see that the bluetooth module is connected, and if so send the joystick pressed notification
if (bluetooth.IsConnected)
{
client.Send("You pressed the Joystick\r\n");
}
// otherwise, just go into first-time pairing mode.
else
{
client.EnterPairingMode();
}
}
void timer_Tick(GT.Timer timer)
{
// check to see that the bluetooth module is connected, and if so get then send the joystick position over the connection
if (bluetooth.IsConnected)
{
GTM.GHIElectronics.Joystick.Position joyPosn = joystick.GetJoystickPosition();
client.Send("Joystick in position X=" + joyPosn.X + " Y=" + joyPosn.Y + "\r\n");
}
}
private void bluetooth_DataReceived(Bluetooth sender, string data)
{
// For sample purposes, we'll just debug print what we get
Debug.Print("Recieved: " + data);
}
void bluetooth_BluetoothStateChanged(Bluetooth sender, Bluetooth.BluetoothState btState)
{
// here the bluetooth module's state has changed. First, just debug print the value so we know what is happening
Debug.Print("New state:" + btState.ToString());
// If the state is now "connected", we can do stuff over the link.
if (btState == Bluetooth.BluetoothState.Connected)
{
Debug.Print("Connected");
Thread.Sleep(2000); // do this to wait for BT module to connect; 2000 may be too long, but a quick trial and error seemed to show this was ok.
// if we don't have this pause, then the BT module will take the data we send it and loop it back as input.
client.Send("Connected to Fez\r\n");
}
// if the state is now "disconnected", you might need to stop other processes but for this example we'll just confirm that in the debug output window
if (btState == Bluetooth.BluetoothState.Disconnected)
{
Debug.Print("Disconnected");
}
}
}
}
Make sure you’re set to WORK:SLAVE and have a BTSTATE:1 before sending +INQ=1
You’ll also need to pair your PC w/ the device. It WILL timeout and fail the first time while drivers are being installed and serial ports setup. After that connections should run smoothly as long as you keep the module in slave mode.
Commented out the client.EnterPairingMode(); in ProgramStarted and reran the code. The outpur generated was as follows:
The thread ‘’ (0x2) has exited with code 0 (0x0).
Using mainboard GHI Electronics FEZCerberus version 1.1
Program Started
Reader Thread
Client Mode
The thread ‘’ (0x3) has exited with code 0 (0x0).
Waited for just over a minute then pressed the joystick the following addtional output was generated:
Enter Pairing Mode
Attempted to connect to device, but still unsuccessful.
Architect, do you think that I am making some fundamental error? Several of the guys on the forum have been able to get this to work, I’m really wondering what I’m doing wrong :s
Dear Skewworks,
Please forgive I am not totally versed in the area of programming. The settings which you described in your email are wrt my PC or changes that I need to implement to the syntax of my code?
What Skewworks is telling about is a side effect of calling EnterPairingMode too early. You have removed it from ProgramStarted which is good and should help. If you look at your bluetooth trace you should press joystick right after you see +BTSTATE:1
Can you please show all the bluetooth trace messages as well?