Microbridge recieving data on Android gives problems

Hi all,

I’m using an Arduino with a USB hostshield (sparkfun) that is connected to a Android Phone. When i send data from the smartphone to the arduino then there is no problem. But when i send data from the arduino to the Android smartphone then the data is recieved ( i get a message that the handler is triggerd) but the app crashes after a few cycles. I use de Adb library form Microbridge.

The data that is send from the arduino is as uint16_t with the number 800, this happens in the following loop:

void setup()
{
  Serial.begin(57600);
 
  // Record time for sensor polling timer
  lastTime = millis();
 
  // Init the ADB subsystem.
  ADB::init();
 
  // Open an ADB stream to the phone's shell. Auto-reconnect. Use port number 4568
  connection = ADB::addConnection("tcp:4568", true, adbEventHandler);  
 
  Serial.println("Ready!");
}
 
 
void loop()
{
  //Check if sensor should be sampled.
  if ((millis() - lastTime) > 1000)
  {
  
   int number = 800;
    uint16_t data = number;
    Serial.println("Before send");
    connection->write(sizeof(data), (uint8_t*)&data);
    Serial.println("After send");
 
    // Update timer for sensor check
    lastTime = millis();
  }
 
  // Poll the ADB subsystem.
  ADB::poll();
}

The data is recieved on the Android in the handler as followd (it uses the lightwight server from Microbridge):

server.addListener(new AbstractServerListener() {
				

@Override
    public void onReceive(com.example.communicationmodulebase.Client client, byte[] data){
									
        Log.e(TAG, "In handler!");
       if (data.length < 1){
	  return;
      }
					
     TextView textRecvStatus = (TextView) findViewById(R.id.RecvStatusText);
				
     textRecvStatus.setText("In handler!");
			
    }
});

The message: “In handler!” is shown but after a few recieves the app chrases.

Does anyone have a idea why the app crashes? I can’t read the catlog since the Android smartphone is connected to the arduino shield. As far as i know there is no way to show the catlog data( i could be wrong?).

Any suggestions and tips are welcome!

Your Arduino question was?

My Arduino question is if my communciation on my Arduino side seems good? And if possible if someone has experience with Arduino and Android communicaiton.

My Arduino question is if my communciation on my Arduino side seems good?

That's hard to say. It's unnecessarily complicated. You don't need number and data. It won't even compile.

Thank you for your reply!

Do you mean the sending of the integer with the value 800? :roll_eyes:

Do you mean the sending of the integer with the value 800?

Yes. You assign 800 to an int, and then copy that to an unsigned it. You could have simply assigned the value to data, and skipped declaring and initializing the first variable.

@PaulS:

Thank you for your reply! I solved the problem. Now i have a aditional problem ( it has to do with this problem, so i posted it on this topic if nobody minds).

have an event handler function and in that function there is a call to a member function of a class. The event handler function is declared in the class cpp file but not part of the class, it's no a member function.

When i compile the code the compiler says that the function is note in the scope, because it's calling a member function within the global event handler function.

My question is as followed: is there a way to use a meber function in a global function? (The object is created first on runtime).

Below is the member function and global event handler:

Global event handler:

void adbEventHandler(Connection * connection, adb_eventType event, uint16_t length, uint8_t *     data)
{
     Serial.println("In data recieve handler");


    Serial.println("Data recieved: ");
    Serial.println(data[0]);
    Serial.println(data[1]);

    char a = data[0];
    char b = data[1];

   Serial.println(a);
   Serial.println(b);
   //uint16_t data2 = data;

   // Member function of USBCommunicator class
   SendBuffer(data, sizeof(data));

}

Member fucntion:

void CommunicationModuleUSB::SendBuffer(uint8_t * Buffer, int Size){

    connection->write(Size,(uint8_t*)&Buffer);
}

When i change te member function decleration to static like this:

  static void CommunicationModuleUSB::SendBuffer(uint8_t* Buffer, int Size);

And call the member function in the global event handler function like this:

CommunicationModuleUSB::SendBuffer(data, sizeof(data));

Then i get the following compiler error:

C:\Users\Gebruiker\Documents\Arduino\libraries\CommunicationModuleUSB/CommunicationModuleUSB.h:26: error: extra qualification 'CommunicationModuleUSB::' on member 'SendBuffer.

@PaulS (and any other helpful member) Is it possible to use member functions in a global function when the object is not yet declared (is declared on runtime).

Is it possible to use member functions in a global function when the object is not yet declared (is declared on runtime).

No. Think of it this way. Can you make Timmy whistle before Timmy is born?

class Person
{
   Person();
   void whistle();
};

Persom Timmy;

We'd need to see your complete code. You should be able to call a static method using the scope resolution operator. But, whether the scope resolution operator is needed, or not, depends on where you are calling the method.

Now, you are going to have a problem anyway, unless connection is also a static member of the class since static methods can not access instance variables. Which instance?

@PaulS:

Thank you for your help again, i have modefied the code but i still have a problem that the Connection variable is not declared in the scope. I have added the Connection variable in the class headerfile as followed:

	// Adb connection made this static....(is this right?
                   static Connection * connection;

And the static member functiion in the class ccp file as followed:

void CommunicationModuleUSB::SendBuffer(uint8_t* Buffer, int Size){

    connection->write(Size,(uint8_t*)&Buffer);
}

And the function is called in the global event handeler function as followed:

// Event handler for shell connection; called whenever data sent from Android to Microcontroller
void adbEventHandler(Connection * connection, adb_eventType event, uint16_t length, uint8_t * data)
{
   Serial.println("In data recieve handler");
   
   
   Serial.println("Data recieved: ");
   Serial.println(data[0]);
   Serial.println(data[1]);
   
   char a = data[0];
   char b = data[1];
   
   Serial.println(a);
   Serial.println(b);
   
   CommunicationModuleUSB::SendBuffer(data, sizeof(data));
     
}

Do you or anybody else have any suggestions? All feedback is welcome! I added the class header file and cpp file as a rar file to thist post.

CommunicationModuleUSB.rar (2.13 KB)

I added the class header file and cpp file as a rar file to thist post.

But, not the sketch.

@PaulS

I posted the sketch below:

#include <CommunicationModule.h>
#include <CommunicationModuleUSB.h>
 #include <SPI.h>
#include <Adb.h>


  CommunicationModuleUSB CMUSB; 
  


void setup()
{
  Serial.begin(57600);
  Serial.flush();
  
  Serial.println("Setting up....");
  CMUSB.Connect();
  Serial.println("Ready!");
}
 
 
void loop()
{
  while(1){
    //delay(1000);
    
    CMUSB.CheckConnection();
    
  };
}
#include <Adb.h>

Great. As soon as we correctly guess where you got this library, we’ll be in business. I’m not really into guessing games this morning.

  Serial.begin(57600);
  Serial.flush();

Open the serial port. Block until all pending serial data has been shuffled out. Why?

Unless you have a very good reason for calling flush() here, get rid of it. If you DO have a good reason, there needs to be a comment explaining that very good, but not at all obvious reason.