Rainbow cube Rainbowduino V3.0b Bluetooth Bee

Dear all,

I am trying to get a Bluetooth Bee (seeedstudio.com/depot/blueto … Path=19_21) working on a Rainbow Cube with Rainbowduino V3, but when trying to discover the Bluetooth Bee on my Android phone it is not visible.

I have put the Bluetooth Bee in place at the Bee headers of the Rainbow Cube motherboard, and I have soldered jumper cables in the wholes in the Rainbow Cube motherboard that connects to ground, Tx, and Rx at the Bee connector. The ground cable is then connected to ground at the Rainbowduino mail header, Tx at Rainbowduino is connected to Bee Rx, and Rx at RainbowDuino is connected to Bee Tx. Further, I have but the USB/Host switch in the Host position, after having programmed the RainbowDuino.

The Rainbowduino code is at the bottom.

Thanks in advance for any help.

Best regards, Marius


#include <Rainbowduino.h>

void setup() {
Rb.init();
Rb.blankDisplay();
setupBlueToothConnection();
}

char a;

void loop() {
if (Serial.available()) {
if (Serial.read() == ‘a’) {
Rb.setPixelZXY(3, 3, 3, 255, 255, 255);
}
while( (a = Serial.read()) != -1) {
//Wait until all other response chars are received
}
}
delay(100);
}

void setupBlueToothConnection()
{
Serial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400
delay(1000);
sendBlueToothCommand("\r\n+STWMOD=0\r\n");
sendBlueToothCommand("\r\n+STNA=LEDCube\r\n");
sendBlueToothCommand("\r\n+STAUTO=0\r\n");
sendBlueToothCommand("\r\n+STOAUT=1\r\n");
sendBlueToothCommand("\r\n+STPIN=5934\r\n");
delay(2000); // This delay is required.
sendBlueToothCommand("\r\n+INQ=1\r\n");
delay(2000); // This delay is required.
}

//Checks if the response “OK” is received
void CheckOK()
{
char a,b;
while(true) {
if(Serial.available()) {
a = Serial.read();

  if('O' == a) {
    // Wait for next character K. available() is required in some cases, as K is not immediately available.
    while(Serial.available()) {
      b = Serial.read();
      break;
    }
    if('K' == b) {
      break;
    }
  }
}

}

while( (a = Serial.read()) != -1)
{
//Wait until all other response chars are received
}
}

void sendBlueToothCommand(String command)
{
Serial.print(command);
CheckOK();
}

Looking more closely at the Eagle schematic files it seems I got it all wrong. I think I should rather connect the Bluetooth Bee to the female header Rx/Tx and put the switch in the Host position. This way the USB will be disconnected from the ATmega328 and the Bluetooth Bee will be connected to data pins 0 and 1.

Then using the SoftwareSerial library with pins 0 and 1, I should be able to communicate with the Bluetooth Bee.

Am I on the right path or am I about to mess up the Rainbow Cube?

Feedback will be appreciated.

Best regards,
Marius

I connected the Bluetooth Bee to the female header Rx/Tx (in addition to the ground connection - still connected to the male header ground - but this is also connected to the female header ground) and put the switch in the Host position. Further, I use the hardware serial on the ATmega328 data pins 0 and 1. The sketch then resembles the code in the first post, except that I don’t use the CheckOK function (this didn’t work for some reason) but have fixed delays of 3 seconds in the sendBlueToothCommand function.

The sendBluetoothCommand function now looks like this:

void sendBlueToothCommand(String command)
{
char a;
Serial.print(command);
delay(3000);
while(Serial.available()) {
a = Serial.read();
}
}

I can now discover the Bluetooth Bee using my Android phone and pair with it.
I can also send text to the Bluetooth Bee from my Android phone.

My main problem now is that it seems I can only connect once to the Bluetooth Bee after power-up of the Rainbow Cube. If I disconnect and try to reconnect the connect fails.

My Android phone is a LG Nexus 4.

Any ideas?

Best regards,
Marius

Hi all,

I have fixed the issue with the reconnection problem. It turned out to be a timing issue in the Android application, trying to write to the OutputStream (java.io.OutputStream) too fast after doing a BluetoothSocket connect. Then, after closing down the OutputStream and the BluetoothSocket, I was not able to connect again.

I now only (I believe) have one main issue left, and that is to get rid of the pairing dialogue by providing the pin by the Android application. I have this working with a bluetooth printer, but for some reason the pin dialogue pops up during the pairing process with the cube.

Anybody else had problems with this and solved it?

Best regards,
Marius

Hi all,

For anyone interested. The issue with the pairing pin dialog was due to my reliance on a function called createBond() in the class BluetoothDevice. This always returned false with the cube, indicating a failure to bond. I then tried to use the function getBondState() in BluetoothDevice to get the bond state so I would know if pairing was going to happen, but this function was unreliable. The solution was to always prepare for bonding (by registering a receiver for capturing the ACTION_PAIRING_REQUEST event and set the pin in the receiver). I also use an extension to the class NotificationListenerService (available from Android version 4.3 and on)
to cancel the pairing notification (it has the id “android.R.drawable.stat_sys_data_bluetooth”).

It now works :smiley:.

Best regards,
Marius

By the way, for some reason I had to reduce the baud rate at the Bluetooth Bee to make the communication between my Android phone and the Bluetooth Bee reliable. I now have it at 9600, and it works.

Best regards,
Marius