Android ketai Bluetooth read

edited December 2017 in Android Mode

Hi! I'm using the following code to communicate with arduino on Android, it works great, but can't figure out, how to read the stream until specified character received. Thanks in advance!


import android.content.Intent;

import android.os.Bundle;

import ketai.net.bluetooth.*;

import ketai.ui.*;

import ketai.net.*;


PFont fontMy;

boolean bReleased = true; //no permament sending when finger is tap

KetaiBluetooth bt;

boolean isConfiguring = true;

String info = "";
String asd = "";
KetaiList klist;

ArrayList devicesDiscovered = new ArrayList();


//********************************************************************

// The following code is required to enable bluetooth at startup.

//********************************************************************


void onCreate(Bundle savedInstanceState) {

 super.onCreate(savedInstanceState);

 bt = new KetaiBluetooth(this);

}


void onActivityResult(int requestCode, int resultCode, Intent data) {

 bt.onActivityResult(requestCode, resultCode, data);

}


void setup() {

 size(displayWidth, displayHeight);

 frameRate(10);

 orientation(PORTRAIT);

 background(0);

 

 //start listening for BT connections

 bt.start();

 //at app start select device…

 isConfiguring = true;

 //font size

 fontMy = createFont("SansSerif", 40);

 textFont(fontMy);

}


void draw() {

 //at app start select device

 if (isConfiguring)

 {

  ArrayList names;

  background(78, 93, 75);

  klist = new KetaiList(this, bt.getPairedDeviceNames());

  isConfiguring = false;

 }

 else

 {

  background(0,50,0);

  if((mousePressed) && (bReleased == true))

  {

 //send with BT

  byte[] data = {'s','w','i','t','c','h','\r'};

  bt.broadcast(data);

 //first tap off to send z next message

  bReleased = false;

  }

  if(mousePressed == false)

  {

  bReleased = true; //finger is up

  }

 //print received data

  fill(255);

  noStroke();

  textAlign(LEFT);

  text( info , 20, 104);

 }

}


void onKetaiListSelection(KetaiList klist) {

 String selection = klist.getSelection();

 bt.connectToDeviceByName(selection);

 //dispose of list for now

 klist = null;

}


//Call back method to manage data received

void onBluetoothDataEvent(String who, byte[] data) {

 if (isConfiguring)

 return;

 //received
 
 info = new String(data);

 //clean if string to long

 if(info.length() > 150)

 info = "";

}


// Arduino+Bluetooth+Processing 

// Arduino-Android Bluetooth communication

Answers

  • Now you can help me?

  • edited April 2018

    I know this is far too late but I will answer this for those who always search before making questions. The code below uses the Ketai library, which is kept updated. The part of the Processing sketch that treads the configuring and pairing I left out, because it can be done on the android configuring. Just pair your let ´s say, Arduino Bluetooth shield like HC-05 or whatever you named it. Change the "bt.connectToDeviceByName("HC-05");" line with "your name", and run the sketch.

    First the arduino part:

             #include <SoftwareSerial.h>
              SoftwareSerial mySerial(10,11);
    
             void setup() {
                mySerial.begin(9600);
             }
    
             void loop() {
                for (int i=-100; i<=100; i++){ 
                   String s = "^" + String(i);
                   mySerial.print(s);
                  //  String s = "^The quick brown fox jumped over the lazy dog.";
                  //  mySerial.write (32);
                  //  mySerial.write (65);
                  //  mySerial.write (64);
                    delay(300);
                }
             }
    

    The Processing code:

                import android.content.Intent;
                import android.os.Bundle;
                import ketai.net.bluetooth.*;
                import ketai.ui.*;
                import ketai.net.*;
    
                KetaiBluetooth bt;
                KetaiList klist;
                int val = 0;
                String stringInfo = "";
    
                void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  bt = new KetaiBluetooth(this);
                }
    
                void onActivityResult(int requestCode, int resultCode, Intent data) {
                  bt.onActivityResult(requestCode, resultCode, data);
                }
    
                void setup() {
                  fullScreen();
                  background(0);
                  fill(255);
                  textAlign(LEFT);
                  textSize(40);
                  bt.start();
                  bt.getPairedDeviceNames();
                  bt.connectToDeviceByName("HC-05");
                }
    
                void draw() {
                    background(0);
                    text(stringInfo , 20, 104);
                }
    
                void onBluetoothDataEvent(String who, byte[] data) {
                  if (data != null){
                    String str = new String(data);
                      if (str.charAt(0) != '^'){  // '^' = separator
                        stringInfo = str.substring(0, str.length());
                        val = Integer.valueOf(stringInfo);
                        println(val*2);
                      }
                   }
                }
    

    Large strings will be send in chunks of shorter data arrays so you have to put a delay in your ino file big enough to insure that the separator byte always be the end character in the data array, otherwise it will lose characters making it unreliable. For faster transfer, like file transfer you can copy the data arrays into a new array increasing the index number continuosly.

Sign In or Register to comment.