/* Filename:    J024_settingFlag.js
 * Description: Processing a command within a command by setting a flag
 * Dropping a serial connection after data is sent, connecting with Bluetooth the reverting back to serial connection once data is transmitted
 * Copyright:   2006 The Code Corporation. Please see http://www.codecorp.com/jsLicensing.html for information regarding copyright and grant of license.
 * $Revision: 118 $
 * $Date: 2006-12-05 16:26:51 -0700 (Tue, 05 Dec 2006) $
 */
 
include(".crx.js");

var btdata = ""; //setting flag
var lastCommand = ""; //setting flag

reader.onCommandOld = reader.onCommand; //override .crx function
reader.onCommandFinishOld = reader.onCommandFinish; //override .crx function


//data that needs to be sent via bluetooth is preceeded by the "|" character
reader.onCommand = function (cmdType, cmdData)
{
    if(cmdType == '|') 
    {
        print("cmd pipe ");
        lastCommand = cmdType;
        btdata = cmdData;
    }
    
    return reader.onCommandOld(cmdType,cmdData);
    
}

//Command must finish before comm mode can change (see reader.onIdle())
reader.onCommandFinish = function (cmdSuccess, respType, respData)
{
        if(lastCommand == '|') 
        {
            lastCommand = "";
        }
        
        return reader.onCommandFinishOld(cmdSuccess,respType,respData);
}


reader.onIdleOld = reader.onIdle;


//once all events have cleared
reader.onIdle = function() 
{
var rc;
    if(btdata!="") 
    {
        rc = reader.processCommand('P', "\x08"+"2");  // chg rdr packet mode  
        rc = reader.processCommand('P', "\x1b"+"4");  // set BT comm mode 
        rc = reader.processCommand('P', "\x42"+"1");  // expect response
        rc = reader.processCommand(':', "\x07\x00\x0b\xef\x00\x14\xc3");  // bt modem addr

        rc = comm.sendPacket('z', btdata);     
        rc = reader.processCommand('P', "\x08"+"1");  // raw mode 
        rc = reader.processCommand('P', "\x1b"+"1");  // set rs232 comm mode 
        rc = reader.processCommand('P', "\x42"+"0");  // no response
        btdata = "";
    }

    reader.onIdleOld(); //return to .crx onIdle
}


