sending/receving binary data over bluetooth

664 views
Skip to first unread message

Dan Sulik

unread,
Oct 28, 2015, 3:40:50 PM10/28/15
to AndroidScript
Is there a way to send binary data via  bluetooth Write () function?
For example I have an array of bytes like:
d[0] = 0x55;
d[1] = 2;
d[2] = 0;
...

then I want to send the data as 3 bytes in binary format like 
bt.Write (d, 3);

Any help is appreciated.
Thnx
dan

Dave Smart

unread,
Oct 28, 2015, 5:28:41 PM10/28/15
to AndroidScript
Hi Dan,

Yes you can use the "Hex" mode of the Bluetooth serial component.  Here is an example class I wrote to control a "FlowPaw" Bluetooth module from DroidScript Apps:-


function FlowPaw()
{
    //Private variables.
    var onConnect, onButtons;
    var buttons = [false,false,false,false];
    var lastBtns  = 0;
   
    //Initialise the object (constructor).
    var bt = app.CreateBluetoothSerial("hex");
    bt.SetOnConnect( _Cb(this,"OnConnect") );
    bt.SetOnReceive( _Cb(this,"OnData") );
    
    //Use EOF+data header as delimeter because this is more 
    //reliable than using fixed data size if we get out of sync.
    bt.SetSplitMode( "End", "23452342" );
    //bt.SetSplitMode( "Size", 64+1 );
    
    //Callbacks setters.
    this.SetOnConnect = function( cb ) { onConnect = cb; }
    this.SetOnButtons = function( cb ) { onButtons = cb; }
    
    //Connect to BT module.
    this.Connect = function( btName )
    {
        bt.Connect( btName );
    }
    
    //Handle connection.
    this.OnConnect = function( ok )
    {
        if( ok )
        {
            //PI mode - only speak when spoken to.
            //bt.Write("23,51,23,45");
        
            //Setup buzzer on claw 1.
            bt.Write( "23,58,01,01,23,45" );
        }
        //Fire users callback if set.
        if( onConnect ) onConnect( ok );
    }
    
    //Handle data from device.
    this.OnData = function( data )
    {
        data = data.split(",");
        //console.log( data );
        
        //Read buttons states.
        var btns = parseInt("0x"+data[0]);
        buttons[0] = (btns & 1) > 0 ? 1 : 0;
        buttons[1] = (btns & 2) > 0 ? 1 : 0;
        buttons[2] = (btns & 4) > 0 ? 1 : 0;
        buttons[3] = (btns & 8) > 0 ? 1 : 0;
        
        //Fire button callback if required.
        if( onButtons && btns!=lastBtns ) onButtons( buttons );
        lastBtns = btns;
    }

    //Play tone of given freqency.
    this.PlayTone = function( Hz )
    {
        var hex = Hex( Hz ,4 );
        var hi = hex.substr(0,2);
        var lo = hex.substr(2,2);
        bt.Write( "23,43,01,01,"+hi+","+lo+",23,45" );
    }
    
    //Turn LEDs on or off.
    //paw.SetLEDs( [1,1,1,0] );
    this.SetLEDs = function( leds )
    {
        var byte = 0;
        if( leds[0] ) byte |= 1;
        if( leds[1] ) byte |= 2;
        if( leds[2] ) byte |= 4;
        if( leds[3] ) byte |= 8;
        var hex = Hex( byte, 2 );
        bt.Write( "23,4C,"+hex+",23,45" );
    }
    
    //Set a servos position (posn 0.0 - 1.0).
    this.SetServo = function( num, posn )
    {
        var hex = Hex( posn*999, 4 );
        var hi = hex.substr(0,2);
        var lo = hex.substr(2,2);
        var num = Hex( num, 4 );
        bt.Write( "23,53,"+num+","+hi+","+lo+",23,45" );
    }
    
    //Set global servo speed (0.0 - 1.0).
    this.SetServoSpeed = function( speed )
    {
        var hex = Hex( speed*99, 4 );
        var hi = hex.substr(0,2);
        var lo = hex.substr(2,2);
        bt.Write( "23,50,"+hi+","+lo+",23,45" );
    }
    
    //Convert number to hex string of given length.
    function Hex( num, len ) 
    {
        num = Math.round( num );
        var s = "000000000" + num.toString(16);
        return s.substr(s.length-len);
    }
}





Dan Sulik

unread,
Oct 28, 2015, 5:46:08 PM10/28/15
to AndroidScript
Hi Dave,

thank you for quick response. That's a great news to have a binary access.
Please could you put more light on CreateBluetoothList () function in my another post?

Thank you
Dan
Reply all
Reply to author
Forward
0 new messages