We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpElectronics,  Serial Library › Connect to a Bluetooth device only when in range
Page Index Toggle Pages: 1
Connect to a Bluetooth device only when in range (Read 1540 times)
Connect to a Bluetooth device only when in range
Dec 10th, 2009, 1:45pm
 
Hello,

I've created some vibration patterns I pass to a bluetooth device through the serial port. The problem is that I assume that the device is there, and connect to it in the setup() phase of the code. If I have the device out of range I get an error.

I'm fairly new to Processing, but I was wondering if there's a way of checking if the bluetooth device is in range, and only then connecting to it. Maybe trying to connect until the device comes in range, and avoid the errors if there's no device in sight?

Hope someone can help me with this :/ I was thinking of something in the lines of this:

Code:
import processing.serial.*;

Serial myPort;

void setup() {
 myPort = new Serial(this, "COM4", 230400);
}

void draw() {
 if (myPort.available() == 0) {
   myPort = new Serial(this, "COM4", 230400);
   println("Nothing");
 } else {
   int inByte = myPort.read();
   println(inByte);
 }
}


But I get an error on this line: (the one inside the draw())

myPort = new Serial(this, "COM4", 230400);

Error inside Serial.<init>()
Re: Connect to a Bluetooth device only when in range
Reply #1 - Dec 11th, 2009, 1:25am
 
I've managed to stop this error from happening by placing a myPort.stop() right before the line that was giving me an error.

My code is now:

Code:
import processing.serial.*;

Serial myPort;

void setup() {
 myPort = new Serial(this, "COM4", 230400);
}

void draw() {
 if (myPort.available() == 0) {
myPort.stop();
   myPort = new Serial(this, "COM4", 230400);
   println("Nothing");
 } else {
   int inByte = myPort.read();
   println(inByte);
 }
}


But now the problem is that if the device comes in range, he never connects, and if the device is in range at the beginning of the execution and then I turn it off and on, it doesn't reconnect :/

Anyone know what can I do?
Re: Connect to a Bluetooth device only when in range
Reply #2 - Dec 12th, 2009, 4:11am
 
Starting to wonder if this forum is dead. I'll continue to post my progress, in case someone else might need it.

I can now connect to a bluetooth device when it comes in range, and Processing continues to execute the code if I turn it off or it comes out of range.

The problem is that I can't keep the connection on enough to transmit enough data, because of the way I use the available() function.

I know the connection is gonna happen on COM4, so I start it by seeing if the device is available already. If it isn't, the execution just lags a bit, no problem here.

Code:
import processing.serial.*;

Serial myPort;

// SHAKE information
ShakeParser shake;
vibProfile vib_pattern;

void setup() {
 size(300, 300);
 background(255);

 // SHAKE begin
 shake = new ShakeParser();
 
 myPort = new Serial(this, "COM4", 230400);


Now, this is the trouble area. In order to have a loop here, I need to check if there's data available on the serial port. If there is, it means that the device is in range, and I upload the vibration pattern to it.

The problem is that if I don't close() or clear() the port at the end of the loop, even if the device isn't in range, the available() function will show data on the port.

That way I have to close() the port at the end the draw() phase, and attempt to connect to the port again, in order to have a valid available() test at the beginning of the next loop.

Code:

void draw() {
 if (myPort.available() == 0) {
   println("SHAKE not in range");
 } else {
   // SHAKE: Activates de motors and the capacitive sensors
   shakeOn();
   
   // SHAKE: Stop vibration if the Capacitive Sensor detects touch
   if ((int)shake.data.cap0.x > 150)
vibPatStop();
   else
vibPatThree();

   // SHAKE: upload the vibration pattern  
   fireVibration();
   
   println("SHAKE in range");
 }
 
 myPort.stop();
 myPort = new Serial(this, "COM4", 230400);
}


The problem is that I can't get the data to the device quick enough before the close() statement.

Any ideas?
Page Index Toggle Pages: 1