1
votes

I am trying to connect a wearable device to processing IDE using Bluetooth connection. Before anything, I have to say that I have made it work on Windows OS, but I am struggling to do it with Linux Mint.Trying to make it work I have made things worse, so I am going to try to explain myself as clear as possible.

FIRST PROBLEM

At the beginning, I was able to connect from processing to the rfcomm0 port but it only sent me bytes with 255 content. For example, if I sent a command such as myPort.write(0x03) to get the streaming sample of my wearable device, it didnt do it, it just sent me a byte with 255. In the same program but running in windows I got all the data that I asked to the device, so it has to be a problem with the rfcomm0 port. My /etc/bluetooth looked like this:

rfcomm0 {
    # Automatically bind the device at startup
    bind yes;

    # Bluetooth address of the device
    device 00:06:66:42:10:44;

    # RFCOMM channel for the connection
    channel 1;

    # Description of the connection
    comment "Example Bluetooth device";
}

The device was already paired, so doing rfcomm bind 0 00:06:66:42:10:44 and running processing as administrator I was able to connect to the port (although not receiving the right data as I said). I am really new on serial ports but my guess is that maybe the channel number 1 was not the right one. How can I know which channel is the one that I need to use?

SECOND PROBLEM

Trying to fix it, I used rfcomm release rfcomm0, changed the rfcomm0 file setting channel 0 (to test a different one) and after binding again (rfcomm bind 0 00:06:66:42:10:44 0, to make sure it connects to other channel) and running processing I keep getting this error when I try to connect:

Error opening serial port /dev/rfcomm0: Port not found

The file for this port it actually exists. I have set the permission mask of the file to /dev/rfcomm0 to crw-rw-rw- 1 root dialout 216, 0 Apr 30 17:04 /dev/rfcomm0 just in case that was the problem but no luck.

When I use the command rfcomm it gives me this:

rfcomm0: 00:06:66:42:10:44 channel 0 clean

So...any help?

Best regards

EDIT

The second problem occurs only when I connect the rfcomm to the channel0. So:

-Channel 0 -> Port not found
-Channel 1 -> Bytes with 255.
-Channel X -> Nothing.

Just to remember, the same code in Windows works fine.

Edit 2

Ok, It seems I am getting something...the wearable device only send me information bytes when I send messages from the method DRAW() buuuut this kind of sucks because I need to do it from the setup method (sending a message to StartStreaming from the draw method does not make sense, it would be sending this message all the time).

Edit 3

It seems to be some time issues...if I do this on the setup function:

  for(int i = 0; i < 50; i++){
    delay(200);
    getSampleRate();
  }

I just get 4-5 times answers package...I really have no idea whats happening.

This is the whole code:

    void setup() 
{
  size(windowW, windowH);
  smooth();  

  // List all the available serial ports:
  println(Serial.list());

  try {
    myPort = new Serial(this, Serial.list()[0], 115200);

  } catch (Exception e) {

      println(e.getMessage());
      connectionError = true;
  } 

  delay(2000);

  /*
  for(int i = 0; i < 50000; i++){
    delay(200);
    getSampleRate();
  }
  */
}

void draw(){

  background(255);

}

// Called whenever there is something available to read
void serialEvent(Serial port) {

  int bytesAvailable = myPort.available();
  for(int i = 0; i < bytesAvailable; i++){
    println("Byte:", port.read());
  } 
}

FINAL EDIT

Well, after researching a lot and testing I think the problem is just about the port connection using bluetooth in Linux. I have tried the same code in Windows and MAC and everything runs great...

1

1 Answers

1
votes

I ran into something similar to this earlier, when binding with /etc/bluetooth.

I don't know why this happens, but this is how I got mine to work.

I left the /etc/bluetooth empty.

I did all the connections in code.

After plugging the USB bluetooth in you will need to use this:

bzero(temp, 256);
sprintf(temp, "sudo hciconfig hci0 reset");
system((char*)temp);

The above only needs to be done when the USB first gets plugged in, not every time you connect. Some times EMI will cause the USB to "unplug and plug back in", you will need to run the above again in that case.

This connects to the device:

bzero(temp, 256);
sprinttf(temp, "sudo rfcomm -r connect 00:06:66:42:10:44 1");
system((char*)temp);

I then used this to check if the connection was made:

for(timeout = 0;; timeout++){
    access_status = access("/dev/rfcomm1", F_OK);
    if(access_status != -1) break; /* file exist */
    if(timeout >= 10) {
        printf("Failed to connect.");
        return;
    }
    sleep(1);
}

To close the connection I used this:

bzero(temp, 256);
sprintf(temp, "sudo rfcomm release /dev/rfcomm1");
system((char*)temp);

It goes without saying that your sudo will need to be configed so no password is required to run rfcomm and hciconfig.