private void handleMessage ( byte [ ] data ) { Buffer buffer = new Buffer ( ) ; buffer . write ( data ) ; int type = ( buffer . readShort ( ) & 0xffff ) & ~ ( APP_MSG_RESPONSE_BIT ) ; if ( type == BeanMessageID . SERIAL_DATA . getRawValue ( ) ) { beanListener . onSerialMessageReceived ( buffer . readByteArray ( ) ) ; } else if ( type == BeanMessageID . BT_GET_CONFIG . getRawValue ( ) ) { returnConfig ( buffer ) ; } else if ( type == BeanMessageID . CC_TEMP_READ . getRawValue ( ) ) { returnTemperature ( buffer ) ; } else if ( type == BeanMessageID . BL_GET_META . getRawValue ( ) ) { returnMetadata ( buffer ) ; } else if ( type == BeanMessageID . BT_GET_SCRATCH . getRawValue ( ) ) { returnScratchData ( buffer ) ; } else if ( type == BeanMessageID . CC_LED_READ_ALL . getRawValue ( ) ) { returnLed ( buffer ) ; } else if ( type == BeanMessageID . CC_ACCEL_READ . getRawValue ( ) ) { returnAcceleration ( buffer ) ; } else if ( type == BeanMessageID . CC_ACCEL_GET_RANGE . getRawValue ( ) ) { returnAccelerometerRange ( buffer ) ; // Ignore CC_LED_WRITE; it appears to be only an ack } else if ( type == BeanMessageID . CC_GET_AR_POWER . getRawValue ( ) ) { returnArduinoPowerState ( buffer ) ; } else if ( type == BeanMessageID . BL_STATUS . getRawValue ( ) ) { try { Status status = Status . fromPayload ( buffer ) ; handleStatus ( status ) ; } catch ( NoEnumFoundException e ) { Log . e ( TAG , "Unable to parse status from buffer: " + buffer . toString ( ) ) ; e . printStackTrace ( ) ; } } else { String fourDigitHex = Integer . toHexString ( type ) ; while ( fourDigitHex . length ( ) < 4 ) { fourDigitHex = "0" + fourDigitHex ; } Log . e ( TAG , "Received message of unknown type 0x" + fourDigitHex ) ; returnError ( BeanError . UNKNOWN_MESSAGE_ID ) ; } }
Handles incoming messages from the Bean and dispatches them to the proper handlers .