Main Site Documentation

Bluetooth is pairing sometimes and not other times


#1

Hi all,

I know there has been a lot of discussion around the bluetooth module but I think I’ve looked through everything that I could without this problem being reported anywhere else.

I am using the bluetooth.cs code that Velloso put up and have pretty much the same setup as: https://www.ghielectronics.com/community/forum/topic?id=7480

It seems to be working and I’ve managed to send and receive data using Tera Term. The problem I have is that sometimes my gadgeteer enters pairing mode (the red and blue flashing lights I take it?) and then my PC says “Oh I’ve found a paired device, tap to setup” and this is what fails.

I call EnterPairingMode() after a 15 second timer as suggested by other users as doing it straight from ProgramStarted() doesn’t work. At this point it enters the red/blue flashing state and connects or not, if it for some reason doesn’t manage to connect with my PC it goes to CONNECT:FAIL and to just flashing the blue light and then goes back into pairing mode (red/blue flashing).

My question is, is there anything I can do to get this working again. It seems to go back and forth between bluetooth statuses: disconnected->inquiring->connecting->CONNECT:FAIL->disconnected->inquiring etc. Every time it is inquiring my PC recognises it and asks me to tap to set it up and this always fails.

Any ideas how to solve this problem?

Thanks for your time,

Poncho


#2

Okay, now I’m getting nothing from the board. Everytime I run the code my PC says “Oh do you want to setup ‘Spider’?” but when I click that it says it’s failed and I’ve never managed to send/receive data using teraterm since. Even after removing all traces of the spider from my PC so it would see it as a new device to pair again.

Any help would be greatly appreciated.

Thanks,

Poncho


#3

Hi, things seem to have taken a backwards step since yesterday.

I am running exactly the same code (bar changing EnterPairingMode() from a timer to a button press event handler) but now it never goes into pairing mode. Here is a step-by-step of what happens:

  1. I run the program
  2. The module is doing the blue light double-flash
  3. I get “Reader Thread” in the console
  4. I get some crazy characters printed in the console
  5. I wait 15 seconds and press the button
  6. I get some more crazy characters in the console
  7. EnterPairingMode() is called
  8. The module continues to do the blue light double-flash and never does the red/blue alternate flashing it usually does for EnterPairingMode()

I have tried every standard baud rate to no avail.

Here is the output I get from running the program:

Using mainboard GHI Electronics FEZSpider version 1.0
Reader Thread
S\ÿ
S
è
è
è
Client Mode
Program Started
SRPÿ
Scª¬ÿ
è
The thread '<No Name>' (0x3) has exited with code 0 (0x0).
Enter Pairing Mode
SRPë
SRP+)ê

I am also printing to console on the BluetoothStateChanged event but this never seems to get hit like it used to.

Does anyone have any ideas of what else I can try? Or what I may be doing wrong?

Thank you again for your time,

Poncho

P. S. Here is my code:

public partial class Program
    {
        Bluetooth bluetooth;
        Bluetooth.Client client;

        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            bluetooth = new Bluetooth(9);
            bluetooth.SetDeviceName("rawr");
            bluetooth.SetDeviceBaud(115200);

            client = bluetooth.ClientMode;

            bluetooth.BluetoothStateChanged += new Bluetooth.BluetoothStateChangedHandler(bluetooth_BluetoothStateChanged);

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

            Debug.Print("Program Started");
        }

        void bluetooth_BluetoothStateChanged(Bluetooth sender, Bluetooth.BluetoothState btState)
        {
            if (btState == Bluetooth.BluetoothState.Connected)
                Debug.Print("State: Connected");
            else if (btState == Bluetooth.BluetoothState.Connecting)
                Debug.Print("State: Connecting");
            else if (btState == Bluetooth.BluetoothState.Disconnected)
                Debug.Print("State: Disconnected");
            else if (btState == Bluetooth.BluetoothState.Initializing)
                Debug.Print("State: Initializing");
            else if (btState == Bluetooth.BluetoothState.Inquiring)
                Debug.Print("State: Inquiring");
            else if (btState == Bluetooth.BluetoothState.Ready)
                Debug.Print("State: Ready");
            else
                Debug.Print("State: Unknown Change");
        }

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

#4

Hi,
I had similar problems too. I think it has to do with the serial ports on the PC. If the BT-connection is lost there seem to remain residuals from former Connections which interfere with a new connection. I’m now trying to work with 32feet.net library on the PC. That seems to work quite well. I hope to get it ready on the next weekend.
Regards
Roland


#5

Ah okay, thanks for the reply. The part I don’t understand is that the bluetooth module never goes into the red/blue flashing anymore, is that because it finds the PC it has paired with before and attempts to connect straight away?

If this is the case, what if I ever want it to connect to another PC in range? Is this not possible?

Thanks again for the response and please post back if you get the 32feet library working with it.

Cheers,

Poncho


#6

Hi,
I think you have - perhaps additionally - a baudrate mismatch from the serial port to the Bluetooth module. You get no reasonable answers from the BT-module that it understands your commands. Try to set back to standard baudrate 38.400.
Regards
Roland

P.S. See this thread: http://www.ghielectronics.com/community/forum/topic?id=9742


#7

Hi Roland,

Thanks for the help, I’ve just tried this (changing baud rate to 38400) but I still have the same problem.

The blue light does it’s double-flash but when I push the button to EnterPairingMode() it just sits there still doing its double-flash. It says “Enter Pairing Mode” in console but if I search for it with either my PC or android phone it doesn’t show up like it used to.

I’ll keep messing around but I’m close to giving up :frowning:


#8

Hi,
I think that you have still a problem with the communication between e.g. Spider and the BT-Module (baudrate mismatch). If you have successful communication you receive OK Messages from the BT Module in the Output window (see Image). If you have changed the baudrate from the BT Module, I think you must use this last successful baudrate to change the Baudrate to the standard Baudrate of 38.400


#9

at times like this I always think it’s worth going back to “known” working bits. Plug the module into a working USB-UART adapter (via a extender or breakout module if you need) and issue commands that the driver would.


#10

Ah okay, over tera term or something?

I’m not really sure what sort of commands are issued, is there a list/guide somewhere?

Thanks


#11

Hi,
If you use the code of Brett at https://www.ghielectronics.com/community/forum/topic?id=7480 you can look in the Bluetooth.cs class which commmands are sent to the BT-module. More details you can see here: http://www.seeedstudio.com/wiki/Serial_port_bluetooth_module_(Master/Slave)
If you use the class of Brett, I can try to help you to get it running if you tell me the last baudrate that worked for you.
Regards
Roland


#12

Aah okay thanks,

I have no idea what the last baud rate was, it will either be 9600 or 115200, I think they are the two I tried the most.

Thanks


#13

In your code you use the constructor of the Bluetooth class with only one parameter (9). So the baudrate of spider UART always stays on the baudrate of 38.400 to communicate with the BT-Module. You then with bluetooth.SetDeviceBaud(115200) you set the Baudrate of the BT-Module to 115200. After this thy can no longer communicate. You should use the second constructor of the class as Brett suggested.
e.g. bluetooth = new Bluetooth(9, 38400);
this form of the constructor changes the baudrate of the spider UART too.
But you must hit the last baudrate you successfully sent to the BT-Module, otherwise the BT-does not understand the command and will not change its baudrate.

// Your code


void ProgramStarted()
        {
            bluetooth = new Bluetooth(9);
            bluetooth.SetDeviceName("rawr");
            bluetooth.SetDeviceBaud(115200);
 

//From the Bluetooth Class of Brett

 
 public Bluetooth(int socketNumber)
            {
                // This finds the Socket instance from the user-specified socket number.  
                // This will generate user-friendly error messages if the socket is invalid.
                // If there is more than one socket on this module, then instead of "null" for the last parameter, 
                // put text that identifies the socket to the user (e.g. "S" if there is a socket type S)
                Socket socket = Socket.GetSocket(socketNumber, true, this, null);

                this.reset = new GTI.DigitalOutput(socket, Socket.Pin.Six, false, this);
                this.statusInt = new GTI.InterruptInput(socket, Socket.Pin.Three, GTI.GlitchFilterMode.Off, GTI.ResistorMode.Disabled, GTI.InterruptMode.RisingAndFallingEdge, this);
                this.serialPort = new GTI.Serial(socket, 38400, GTI.Serial.SerialParity.None, GTI.Serial.SerialStopBits.One, 8, GTI.Serial.HardwareFlowControl.NotRequired, this);

                //this.statusInt.Interrupt += new GTI.InterruptInput.InterruptEventHandler(statusInt_Interrupt);
                this.serialPort.ReadTimeout = Timeout.Infinite;
                this.serialPort.Open();

                Thread.Sleep(5);
                this.reset.Write(true);

                readerThread = new Thread(new ThreadStart(runReaderThread));
                readerThread.Start();
                Thread.Sleep(500);
            }

            // Note: A constructor summary is auto-generated by the doc builder.
            /// <summary></summary>
            /// <param name="socketNumber">The socket that this module is plugged in to.</param>
            /// <param name="baud">The baud rate to communicate with the module with.</param>
            public Bluetooth(int socketNumber, long baud)
            {
                // This finds the Socket instance from the user-specified socket number.  
                // This will generate user-friendly error messages if the socket is invalid.
                // If there is more than one socket on this module, then instead of "null" for the last parameter, 
                // put text that identifies the socket to the user (e.g. "S" if there is a socket type S)
                Socket socket = Socket.GetSocket(socketNumber, true, this, null);

                this.reset = new GTI.DigitalOutput(socket, Socket.Pin.Six, false, this);
                this.statusInt = new GTI.InterruptInput(socket, Socket.Pin.Three, GTI.GlitchFilterMode.Off, GTI.ResistorMode.Disabled, GTI.InterruptMode.RisingAndFallingEdge, this);
                this.serialPort = new GTI.Serial(socket, 38400, GTI.Serial.SerialParity.None, GTI.Serial.SerialStopBits.One, 8, GTI.Serial.HardwareFlowControl.NotRequired, this);

                //this.statusInt.Interrupt += new GTI.InterruptInput.InterruptEventHandler(statusInt_Interrupt);
                this.serialPort.ReadTimeout = Timeout.Infinite;
                this.serialPort.Open();

                Thread.Sleep(5);
                this.reset.Write(true);

                // Poundy added:
                Thread.Sleep(5);
                this.SetDeviceBaud(baud);
                this.serialPort.Flush();
                this.serialPort.Close();
                this.serialPort.BaudRate = (int)baud;
                this.serialPort.Open();
                // Poundy 

                readerThread = new Thread(new ThreadStart(runReaderThread));
                readerThread.Start();
                Thread.Sleep(500);
            }

#14

You asked, with Tera Term or something…

Yes !

You just hook the device up to the TTL UART/USB converter and then open the port with Tera Term. In most cases you’ll either get garbage, or you’ll get plain text - garbage means you have it connected at the wrong BAUD rate.

But as Roland has explained, you can still try to un-do this on your Fez.


#15

@ Poncho - The Bluetooth Listener using 32feet.net library is now in the Code Share section. Give me some Feedback please, if it works for you.
Roland