Bluetooth on android

Hello!

I am searching for a bluetooth library or any solution, that working on android. Already read ofSerial is enough when devices paired. But seems to me, on android it is not working. Try to list serial ports, but nothing listed.

My goal is make a serial connection with an arduino with bluetooth.

Hi Kiss and All,

I too am working on a piece of software that incorporates Arduino with Android via Bluetooth (working in Eclipse). I have downloaded several bluetooth apps on the android that are displaying my sensor values.

My difficulty, I believe, in finalizing this system, is in figuring out how to open the correct com port to read the bluetooth values into the openFrameworks program. What is the name of the Comm port for a bluetooth connection? Struggling with listing the ports in my code.

Any suggestions from someone who has successfully linked Arduino to Android via bluetooth would be very helpful. Thanks!

Here is what I have so far, just not sure what to call for the Com port:

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
// initialize the accelerometer
ofxAccelerometer.setup();

ofBackground(0);
    ofSetWindowTitle("St. Oswalds Kiosk");

	width = ofGetWidth();
	height = ofGetHeight();

    // read the directory for the images
    // we know that they are named in seq
    ofDirectory dir;

	count = 0;


	//load the images in the file - replace with sd card images

    int nFiles = dir.listDir("/storage/sdcard1/SD"); //here, the images are in a folder on the card called "SD"
	//int nFiles = dir.listDir("/storage/sdcard1"); //these are directly in the sd card, but they read extra files and create black spaces
    if(nFiles) {

        for(int i=0; i<dir.numFiles(); i++) {

            // add the image to the vector
            string filePath = dir.getPath(i);
            images.push_back(ofImage());
            images.back().loadImage(filePath);

        }

    }
    else printf("Could not find folder\n");


// initialize all of the Ball particles
for(int i=0; i<NUM_POINTS; i++) balls[i].init();

// Serial Stuff
//font.loadFont(“DIN.otf”, 64);

bSendSerialMessage = false;

serial.listDevices();
serial.enumerateDevices();

//vector <ofSerialDeviceInfo> deviceList = serial.getDeviceList();

string serialID;

vector<ofSerialDeviceInfo> deviceList = serial.getDeviceList();
	string deviceLine;
	for(int i=0; i<deviceList.size();i++){
		deviceLine = deviceList[i].getDeviceName().c_str();
		//if(deviceLine.substr(0,12) == "tty.usbmodem"){
			 serialID = "/dev/" +deviceLine;
			 cout<<"arduino serial = "<<serialID<<endl;
		//}
	}

// this should be set to whatever com port your serial device is connected to.
// (ie, COM4 on a pc, /dev/tty.... on linux, /dev/tty... on a mac)
// arduino users check in arduino app....
int baud = 9600;
serial.setup(0, baud); //open the first device
//serial.setup("COM4", baud); // windows example
//serial.setup("/dev/tty.usbserial-A4001JEC", baud); // mac osx example
//serial.setup("/dev/ttyUSB0", baud); //linux example

// nTimesRead = 0;
// nBytesRead = 0;
// readTime = 0;
// memset(bytesReadString, 0, 4);

}

//--------------------------------------------------------------
void ofApp::update() {
for(int i=0; i<NUM_POINTS; i++) balls[i].update();

/*
//SERIAL STUFF
if (bSendSerialMessage){

// (1) write the letter "a" to serial:
//serial.writeByte('a');

// (2) read
// now we try to read 3 bytes
// since we might not get them all the time 3 - but sometimes 0, 6, or something else,
// we will try to read three bytes, as much as we can
// otherwise, we may have a "lag" if we don't read fast enough
// or just read three every time. now, we will be sure to
// read as much as we can in groups of three...

nTimesRead = 0;
nBytesRead = 0;
int nRead  = 0;  // a temp variable to keep count per read

unsigned char bytesReturned[3];

memset(bytesReadString, 0, 4);
memset(bytesReturned, 0, 3);

while( (nRead = serial.readBytes( bytesReturned, 3)) > 0){
	nTimesRead++;
	nBytesRead = nRead;
};

memcpy(bytesReadString, bytesReturned, 3);

bSendSerialMessage = false;
readTime = ofGetElapsedTimef();
}

*/
}

//--------------------------------------------------------------
void ofApp::draw() {

   // we need some images if not return
    if((int)images.size() <= 0) {
        ofSetColor(255);
        ofDrawBitmapString("No Images...", 150, ofGetHeight()/2);
        return;
    }

    //my attempt at righting vertical images, (not working)
    if (images[count].height < images[count].width){
    	images[count].rotate90(1);
    }

    // make images fullscreen, correct aspect ratio, centred
   	int newWidth = ofGetWidth();
   	float scaleFactor = (float)ofGetWidth() / (float)images[count].width;
   	int newHeight = (int)(images[count].height * scaleFactor);
   	int destX = (width - newWidth) / 2;
   	int destY = (height - newHeight) / 2;

    ofSetColor(255);

    //This draws the current image (of the count state) to the screen
     images[count].draw(destX, destY, newWidth, newHeight);
    //images[count].draw(0, 0);

    // info to print to screen
    ofSetColor(200);
    string info;
    info += "tap the screen to advance the images";

    ofDrawBitmapString(info, 15, 20);

    for(int i=0; i<NUM_POINTS; i++) balls[i].draw();

/*
//Serial Stuff
if (nBytesRead > 0 && ((ofGetElapsedTimef() - readTime) < 0.5f)){
ofSetColor(0);
} else {
ofSetColor(220);
}
string msg;
msg += “click to test serial:\n”;
msg += "nBytes read " + ofToString(nBytesRead) + “\n”;
msg += "nTimes read " + ofToString(nTimesRead) + “\n”;
msg += "read: " + ofToString(bytesReadString) + “\n”;
msg += "(at time " + ofToString(readTime, 3) + “)”;
//font.drawString(msg, 50, 100);
*/
}

Ok, I believe the serial setup should be this:

serial.setup("/dev/tty.AdafruitEZ-Link1c6e-SPP", baud);

Though this works locally in XCode, I am not seeing any bytes read on the Android app. I have the android paired with my Bluefruit EZ-Link and bytes are read in other Bluetooth apps. I need to read and use the values sent from Arduino via the bluetooth module into this oF program.

I still have not able to connect the android with the bluefruit + Arduino Uno. I have been hung up on this for a couple of weeks now…

I have tried:

  1. “Arduino Uno Communicator” App with the Arduino plugged directly into the tablet. This App recognises the Arduino and reads the sensory input.

  2. “Bluetooth SPP Pro” App. This also reads and displays the Arduino sensory values.

  3. Running my openFrameworks programme in Xcode with the bluefruit paired with my macbook. This works! The serial values are read and display in the programme window as in this image: .

I have tried these ofSerial.setup() values to try to find either the bluefruit or the Uno connected via usb:

serial.setup(0, baud); //open the first device
serial.setup("COM4", baud); // windows example
serial.setup("/dev/cu.AdafruitEZ-Link1c6e-SPP", baud); 
serial.setup("/dev/tty.AdafruitEZ-Link1c6e-SPP", baud); // Works on xcode on mac, but not android 

I had hopes that one of these might work, but they did not:

    serial.setup("/dev/cuS0.AdafruitEZ-Link1c6e-SPP", baud); 
serial.setup("/dev/ttyS0.AdafruitEZ-Link1c6e-SPP", baud); 

I also recently changed my manifest file to add these lines, not sure if that is helpful:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

Any suggestions to get these values reading into my oF programme would be very welcome!

I am using a Sony Xperia Tablet Z android tablet and MacOSX 10.7.5, working in Eclipse.

Thanks!!

Victoria

I wasn’t able to make a BT shield and an Android oF app talk, too, but if you only need to read data from arduino I found this workaround:

“Bluetooth Terminal” app (free on Google Play) connects to Arduino and produces a log file on the sd card; into the oF app you can open that text file and extract the data you want… it’s not a very elegant solution, but for my purposes it worked! :wink:

Thank you for your reply and advice ofCinzio!

When you used this method, did it store and read the data in real time? What kind of sensory data were you using? For example, if there were a proximity sensor, would the results of someone moving closer be displayed by the oF app during their action or did you have a lag?

I think you should first define a serial port on the android device.
make a wrapper for this library:

in JNI could be a solution.

Hi!
I used this method to log instant data coming from 10 current sensors; the Arduino sent the 10 values every 100ms and I could see the actual value displayed in my oF app without any perceptible delay!

It worked well for my purposes, but the solution proposed by kashim is definitely better!

Did anyone on this thread have any luck opening serial ports on android inside OF?