Bluetooth "OnReceive" callback does not work ?

315 views
Skip to first unread message

Damien Lohier

unread,
Aug 9, 2015, 11:28:32 AM8/9/15
to AndroidScript
Hi !

I have a problem with bluetooth functions.
The write function is ok, but when I try to receive a data with bluetooth, the callback I called in bt.SetOnReceive is not called.
There is my code:


//Called when application is started.
function OnStart()
{
   
//Create a layout with objects vertically centered.
    lay
= app.CreateLayout( "linear", "VCenter,FillXY" );    

   
//Create a button 1/3 of screen width and 1/4 screen height.
    btn
= app.CreateButton( "Connect", 0.4, 0.15 );
    btn
.SetOnTouch( btn_OnTouch );
    lay
.AddChild( btn );
   
   
Send = app.CreateButton("send", 0.4, 0.15);
   
Send.SetOnTouch(send);
   
    app
.AddLayout( lay );
   
   
//Create Bluetooth serial object.
    bt
= app.CreateBluetoothSerial();
    bt
.SetOnConnect( bt_OnConnect )
    bt
.SetOnReceive( bt_OnReceive );
    bt
.SetSplitMode( "End", "\n" );
}

//Called when user touches the button.
function btn_OnTouch()
{
    bt
.Connect( "HC-06" );
}

//Called when we are connected.
function bt_OnConnect( ok )
{
   
if( ok )
   
{
        bt
.Write( "Connecté\n" );
        lay
.AddChild(Send);
   
}
   
else app.ShowPopup( "Failed to connect!" );
}

//Called when we get data from device.
function bt_OnReceive( data )
{
    txt
= app.CreateButton("hello");
    lay
.AddChild(txt);
}

function send()
{
   
if(bt.IsConnected())
   
{
        bt
.Write("abc");
   
}
}

When I run this code, the "Hello" button does not appear.
Can you try this code ? (I want to connect to a HC-06 module, not HC-05 ;))

I'm on Samsung Galaxy S3.

Thanks !

Dave Smart

unread,
Aug 10, 2015, 6:16:31 AM8/10/15
to AndroidScript
Hi Damien,

What happens if you comment out the line which says SetSplitMode ?  (That line tells DS to expect new line characters in the responses).

Regards
David

Ray Perkins

unread,
Sep 29, 2017, 8:13:18 AM9/29/17
to DroidScript
Did you ever get it working? I have the same issue with using an OTG cable... Arduino is sending data, but the phone never gets it. Using SetSplitMode did nothing. Sending data from phone to Arduino is fine.

Dave Smart

unread,
Sep 29, 2017, 8:38:11 AM9/29/17
to DroidScript
Hi Ray,

I use OnReceive all the time over USB/OTG, so I suspect you are doing something wrong.   I suggest you post your DS and arduino code here for us to check. (Also tell us what device you are using)

Did you try the "USB Arduino" example in the DroidScript samples (including the sketch)?

Ray Perkins

unread,
Sep 30, 2017, 9:03:15 AM9/30/17
to DroidScript

Hi Dave,

Thanks for a fast reply. I am using the "USB Arduino" example with just SetSplitMode added. The phone is an Alcatel Idol 4 with an OTG cable adapter.
Tried it on both an Arduino Uno clone, and a legit Duelimove, to no avail. You can see the Tx light on the Arduino flicker when it tries to send data, so I suspect the problem is with the phone. When connected to a PC, data from Arduino to PC works fine.

Code for Droidscript and Arduino follows...


// Example showing how to control an Arduino Uno via OTG cable.
//
// This demo requires the following sketch on your Uno:
// http://androidscript.org/demos/arduino/DS_USB_Uno.ino

function OnStart()
{
    // Create a layout with objects vertically centered


    lay = app.CreateLayout( "linear", "VCenter,FillXY" );   

    // Create a toggle button
    btn = app.CreateToggle( "LED On/Off", 0.4 );


    btn.SetOnTouch( btn_OnTouch );
    lay.AddChild( btn );

    // Add layout to app   
    app.AddLayout( lay );

    // Create USB serial object
    usb = app.CreateUSBSerial();
    usb.SetSplitMode( "End", "\n" );
    if( !usb )
    {
        app.ShowPopup( "Please connect your Arduino and restart" );
        return;
    }
    usb.SetOnReceive( usb_OnReceive );
    app.ShowPopup( "USB Connected" );
}

// Called when user touches our toggle button
function btn_OnTouch( isChecked  )
{
    if( !usb ) return; 
    //Send LED command to Uno
    if( isChecked ) usb.Write( "ledh");
    else usb.Write( "ledl");
}

// Called when we get data from Arduino
function usb_OnReceive( data )
{
    app.ShowPopup( "USB received" );
    console.log( data );
}


//------------------------------------------------------------
//
// Arduino Sketch for controlling Arduino devices from
// DroidScript via serial OTG/USB cable.
//
// This code can be tested using the Aurduino serial monitor.  
//
// Examples:
//   send 'ver' to get version of this program.
//   send 'ledh' to turn LED on.
//   send 'ledl' to turn LED off.
//
// Copyright: droidscript.org
// License: Creative Commons Attribution 3.0
//
//------------------------------------------------------------
 
//Global variables.
char g_version[] = "0.10\n";
int g_led = 13;

//Setup the hardware.
void setup()
{
  //Initialize the digital pin as an output.
  //(Pin 13 has an LED connected on most Arduino boards)
  pinMode( g_led, OUTPUT );    
 
  //Setup USB serial comms.
  Serial.begin( 115200 );
  Serial.setTimeout( 100 );
 
  //Send a message over USB serial.
  Serial.print( "USB Ready\n" );
}

//This function is called forever.
void loop()
{
  //Read serial commands.
  while( Serial.peek() != -1 )
  {
    //Read 3 character command.
    char cmd[4] = "---";
    Serial.readBytes( cmd, 3 );
     
    //Execute chosen command.
    if( strcmp( cmd, "led" )==0 ) SetLED();
    else if( strcmp( cmd, "ver" )==0 ) GetVersion();
    else Serial.print( "Unkown command!\n" );
  }
}

//Get software version.
void GetVersion()
{
  Serial.print( g_version );
}

//Control LED.
void SetLED()
{
  //Read hi/low parameter.
  char hiLo;
  Serial.readBytes( &hiLo, 1 );
 
  //Turn the LED on or off (using 'h' or 'l' character).
  if( hiLo=='h' ) digitalWrite( g_led, HIGH );   
  else digitalWrite( g_led, LOW ); 

Dave Smart

unread,
Oct 5, 2017, 7:50:52 PM10/5/17
to DroidScript
Hi Ray,

Do you see anything when you remove the SetSplitMode line?


Have you tried different baud rates... maybe your phone can't cope with faster rates:-

  usb = app.CreateUSBSerial( 115200,8,1,0 );


Ray Perkins

unread,
Oct 7, 2017, 3:19:15 PM10/7/17
to DroidScript
Thanks again, Dave, I finally managed to get it working. Baud rate was fine, and it works with/without the usb.SetSplitMode function.
I think I was just expecting a screen display with the console.log message, so I changed it to app.ShowPopup so I can see what's coming back, and it was fine. I'll mark it solved! 
Reply all
Reply to author
Forward
0 new messages