private fun handleMessage(data: ByteArray) { val buffer = Buffer() buffer.write(data) val type: Int = buffer.readShort() and 0xffff and APP_MSG_RESPONSE_BIT.inv() 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) } else if (type == BeanMessageID.CC_GET_AR_POWER.getRawValue()) { returnArduinoPowerState(buffer) } else if (type == BeanMessageID.BL_STATUS.getRawValue()) { try { val status: Status = Status.fromPayload(buffer) handleStatus(status) } catch (e: NoEnumFoundException) { Log.e(TAG, "Unable to parse status from buffer: " + buffer.toString()) e.printStackTrace() } } else { var 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 .