Bluetooth Shield V2.0

Hi guys!

I have a Bluetooth Shield V2.0 (http://wiki.seeedstudio.com/wiki/Bluetooth_Shield_V2.0) and I have managed to make contact with my mobile phone (Samsung Galaxy A5). But every time the connection will fail. so my question is: Why does it keep failing?

Is it because the Bluetooth of my phone is version 4.0 and of my shield version 2.0?

Well I hope you can help me.
I have the code I am using as an attachment :confused: here I have got the Serial output:


Power on!!
send: AT
recv: OK
send: AT+DEFAULT
recv: OK+DEFAULT
send: AT+ROLEM
recv: OK+Set:M
send: AT+IMME0
recv: OK+Set:0
send: AT+RESTART
recv: OK+RESTART
send: AT+DISC?
recv: OK+DISC:0
send: AT+CLEAR
recv: OK+CLEAR
send: AT+NOTI1
recv: OK+Set:1
send: AT+LNKE8B4C807955E
recv: OK+CONNS
recv: OK+CONNF
send: AT
recv: OK
Setup complete


(Here is code again)

/* Upload this sketch into Seeeduino and press reset*/

#include <SoftwareSerial.h> //Software Serial Port
#define RxD 7
#define TxD 6

#define DEBUG_ENABLED 1

#define MASTER 1 //change this macro to define the Bluetooth as Master or not

SoftwareSerial blueToothSerial(RxD,TxD);//the software serial port

char recv_str[100];

void setup()
{
Serial.begin(115200);
pinMode(RxD, INPUT);
pinMode(TxD, OUTPUT);
Serial.println("\r\nPower on!!");
setupBlueToothConnection();

// while(1)
// {
// if(recvMsg(1000) == 0)
// {
// if(strcmp((char *)recv_str, (char *)“OK+CONB”) == 0)
// {
// Serial.println(“connected\r\n”);
// break;
// }
// }
// delay(200);
// }
}

void loop()
{
recvMsg(10000);
Serial.print(“recv: “);
Serial.print((char *)recv_str);
Serial.println(””);
delay(2000);
sendBlueToothCommand(“AT+SCAN?”);

}

int strcmp(char *a, char *b)
{
unsigned int ptr = 0;
while(a[ptr] != ‘\0’)
{
if(a[ptr] != b[ptr]) return -1;
ptr++;
}
return 0;
}

void setupBlueToothConnection()
{
blueToothSerial.begin(9600);

delay(500);

sendBlueToothCommand(“AT”);

delay(500);

sendBlueToothCommand(“AT+DEFAULT”);

delay(3000);

// sendBlueToothCommand(“AT+PIN0000”);
sendBlueToothCommand(“AT+ROLEM”);
sendBlueToothCommand(“AT+IMME0”);
sendBlueToothCommand(“AT+RESTART”);

delay(5000);

sendBlueToothCommand(“AT+DISC?”);
sendBlueToothCommand(“AT+CLEAR”);
sendBlueToothCommand(“AT+NOTI1”);
// sendBlueToothCommand(“AT+SCAN?”);

delay(1000);

sendBlueToothCommand(“AT+LNKE8B4C807955E”);
recvMsg(11000);
Serial.print(“recv: “);
Serial.print(recv_str);
Serial.println(””);

// delay(1000);

// sendBlueToothCommand(“AT+ADDR?”);

// delay(500);

// sendBlueToothCommand(“AT+RESTART”);

delay(2000);

if(sendBlueToothCommand(“AT”) != 0) return -1;

Serial.print(“Setup complete!\r\n\r\n”);
}

int sendBlueToothCommand(char command)
{
Serial.print(“send: “);
Serial.print(command);
Serial.println(””);

blueToothSerial.print(command);
delay(200);

recvMsg(11000);

Serial.print(“recv: “);
Serial.print(recv_str);
Serial.println(””);
return 0;
}

int recvMsg(unsigned int timeout)
{

unsigned int time = 0;
unsigned char num;
unsigned char i;

i = 0;
while(1)
{
delay(50);
if(blueToothSerial.available())
{
recv_str = char(blueToothSerial.read());

  • i++;*

  • break;*

  • }*

  • time++;*

  • if(time > (timeout / 50)) return -1;*

  • }*

  • while(blueToothSerial.available() && (i < 100))*

  • { *
    recv_str = char(blueToothSerial.read());
    * i++;*
    * }*
    recv_str = ‘\0’;
    * return 0;*
    }
    BT_HRM.ino (2.95 KB)

Your recvMsg() function is fatally flawed. Until you fix that, it is hopeless to try to resolve your other issues.

You wait, using delay() in a while loop, until one character arrives. Once that one character arrives, you assume that the rest of the message has arrived (it will NOT have), so you read up to 100 characters (at most one or two more will have arrived).

The Serial.print() statement that says that setup is complete is not in setup(), so the message is wrong.

bastiaang:
Why does it keep failing?
Is it because the Bluetooth of my phone is version 4.0 and of my shield version 2.0?

No.
If your BT4 phone finds itself communicating with a BT Arduino, it simply reverts to BT2 itself and continues on as normal.

Your problem is as unclear as your intentions, your code looks like utter junk, and is not suitable for use with your phone.

Bluetooth has two modes of operation - communications mode and AT mode. It can only be in one mode at a time. When you are in AT mode you can only communicate with bluetooth by wires, the phone cannot be used. You need to be clear about what you really want to do.

The code looks like it is intended for configuration, i.e. AT mode, and I assume it is intended to put the module into AT mode when you press reset.

If you have had any connection at all with the phone using this code, i.e. actually communicating, not merely pairing, it is because you have actually failed to get bluetooth into AT mode, which I submit is a good thing, as there was never any need to use AT mode in the first place. Bluetooth should be able to enable your Arduino to communicate with your phone as it came out of the box- no fartarsing about with AT needed and, with a bit of luck, it still will.

As a test, you might try this code

#include <SoftwareSerial.h>   //Software Serial Port
#define RxD 7
#define TxD 6
SoftwareSerial blueToothSerial(RxD,TxD);//the software serial port

void setup()
{
    Serial.begin(9600);   
    pinMode(RxD, INPUT);   
    pinMode(TxD, OUTPUT);   
    blueToothSerial.begin(9600);   
}

void loop()
{
Serial.println("hello");
blueToothSerial.println("hello");
delay(1000);

That is all you need - I hope.
Note that the serial monitor must be set to the normal 9600, not the 115200 you (might have) been using. You probably have the phone and Bluetooth paired already, and you just need to run bluetooth terminal and make the connection.

You might find the following background notes useful

http://homepages.ihug.com.au/~npyner/Arduino/GUIDE_2BT.pdf
http://homepages.ihug.com.au/~npyner/Arduino/BT_2_WAY.ino

To use these notes, you will need to set your shield jumpers to hardware serial, not pins 6,7.