Hardware Reference
In-Depth Information
Last, the draw() method reads the
buttons, then sets their previous
states once it's read them. This sketch
uses a modified version of the Button
class from the Processing RFID writer
sketch in Chapter 9. Because the
cursor on a touchscreen (namely,
your finger) can disappear, there's
no equivalent to the mousePressed
and mouseReleased events, so you
have to make one up. You're doing
that in this case by having a method
in the Button class called isPressed() ,
a variable called pressedLastTime ,
and two methods that let you get and
set this variable. With these, you can
check the current state of the button
( isPressed() ), save it when you're done
( setLastState() ), and get the state last
time you checked it ( getLastState() ).
8
Continued from previous page.
lastSendTime = getTime(); // a String to print on the screen
lastSend = minute(); // an int for further comparison
}
// if the read button changed from not pressed to pressed,
// set updateNow, to force an update next time through the
// loop. Do the same for the send button and sendNow, right below:
if (readButton.isPressed() && !readButton.getLastState()) {
updateNow = true;
}
//save the state of the button for next check:
readButton.setLastState(readButton.isPressed());
if (sendButton.isPressed() && !sendButton.getLastState()) {
sendNow = true;
}
//save the state of the button for next check:
sendButton.setLastState(sendButton.isPressed());
}
When the sketch pauses, the
pause() method is called. This
method sends any current readings
to the server, then disconnects the
Bluetooth serial connection so you
can open it again on resume. There is
no resume() method, since setup()
gets called by default every time an
Android activity resumes (remember,
a Processing sketch for Android is an
activity).
8
void pause() {
// if you have any readings, send them:
if (!currentReadings.equals("")) {
sendData(currentReadings);
}
// stop the Bluetooth connection so you can start it again:
if (bt != null && bt.isConnected()) {
bt.disconnect();
}
}
The connect() method tries to
make a Bluetooth connection.
First, it makes sure there's a valid
instance of the BtSerial library and
that it's not already connected to the
remote device (the Bluetooth Mate
in this case). Then it gets a list of all
paired devices on your phone and tries
to connect to the first one.
String connect() {
String result = "Bluetooth not initialized yet...";
if (bt !=null) {
// if you are connnected, get data:
if (!bt.isConnected() ) {
// get the list of paired devices:
String[] pairedDevices = bt.list();
8
if (pairedDevices.length > 0) {
println(pairedDevices);
// open a connection to the first one:
bt.connect( pairedDevices[0] );
result = "Connected to \n" + bt.getName();
}
Before you run the sketch, go to the
Bluetooth Settings on your phone
(under Settings Wireless Settings),
and pair with your Bluetooth Mate. If
»
 
Search WWH ::




Custom Search