class blueReader {

    public static byte[] decodeblueReaderPacket(byte[] buffer, int len) {
        int cmdFound = 0;
        long timestamp = new Date().getTime();
        String bufferstring;
        //Log.w(TAG, "Packet: " + bufferstring);
        if (buffer == null) {
            Log.e(TAG, "null buffer passed to decodeblueReaderPacket");
            return null;
        } else {
            bufferstring=new String(buffer);
        }
        if (bufferstring.startsWith("not ready for") ) { //delete the trans_failed, because its normal only if the bluereader could not read the sensor.
            counterHibernated++;
            Log.e(TAG, "Found blueReader in a ugly State (" + counterHibernated + "/3), send hibernate to reset! If this does not help in the next 5 Minutes, then turn the bluereader manually off and on!");
            if (counterHibernated > 2) {
                Log.wtf(TAG, "Ugly state not resolveable. Bluereader will be shut down! Please restart it!");
                Home.toaststatic("BlueReader ugly state not resolveable, bluereader will be shut down. Please restart it!");
                if (!Pref.getBooleanDefaultFalse("blueReader_suppressuglystatemsg")) {
                    Notifications.RiseDropAlert(xdrip.getAppContext(),true,"BlueReader Alarm", xdrip.getAppContext().getString(R.string.bluereaderuglystate),1);
                }
                return shutdown;
            } else {
                Home.toaststatic("Found blueReader in a ugly State, send hibernate to reset!");
                return goHybernate; //send hard hibernate, because blueReader is in a ugly state
            }
        } else if (bufferstring.startsWith("IDR")){
            Log.i(TAG, bufferstring);
            PersistentStore.setString("blueReaderFirmware", bufferstring );
            tempVers=Pattern.compile(".*\\|blue(.*)-.*").matcher(bufferstring);
            tempVers.find();
            PersistentStore.setDouble("blueReaderFirmwareValue",Double.parseDouble(tempVers.group(1)));
            Log.i(TAG, "bluereader-Firmware-Version: " + tempVers);
            if (BgReading.last() == null || BgReading.last().timestamp + (4 * 60 * 1000) < System.currentTimeMillis()) {
                return requestValue;
            } else {
                return null;
            }
        } else if (bufferstring.startsWith("WAKE")) {
            Log.d (TAG, "blueReader was set to wakeup-mode manually...");
            return null;
        } else if (bufferstring.startsWith("ECHO")) {
            Log.d (TAG, "blueReader was set to Echo-Mode manually...");
            return null;
        } else if (bufferstring.startsWith("NFC READY")) {
            Log.d (TAG, "blueReader notice that NFC is active...");
            return null;
        } else if (bufferstring.startsWith("NFC_DISABLED")) {
            Log.d (TAG, "blueReader notice that NFC is now hibernated...");
            return null;
        } else if (bufferstring.startsWith("HYBERNATE SUCCESS")) {
            Log.i (TAG, "blueReader notice that NFC is now really hibernated...");
            if (counterHibernated > 0) {
                Log.w (TAG,"Found hibernation after wrong read. Resend read-command...");
                return requestValue;
            } else {
                return null;
            }
        } else if (bufferstring.startsWith("-r 0:")) {
            Log.d (TAG, "blueReader sends an unknown reaction: '" + bufferstring + "'");
            return null;
        } else if (bufferstring.startsWith("TRANS_FAILED")) {
            Log.w (TAG, "Attention: check position of blueReader on the sensor, as it was not able to read!");
            Home.toaststatic(xdrip.getAppContext().getString(R.string.bluereader_position));
            return null;
        } else if (bufferstring.startsWith("battery: ")) {
            if (BgReading.last() == null || BgReading.last().timestamp + (4 * 60 * 1000) < System.currentTimeMillis()) {
                return requestValue;
            }
        } else {
            counterHibernated = 0;
            processNewTransmitterData(TransmitterData.create(buffer, len, timestamp), timestamp);
            // check for shutdown blueReader if Battery is too low
            if (Pref.getBooleanDefaultFalse("blueReader_turn_off")) {
                if (Pref.getInt("blueReader_turn_off_value",5) > Pref.getInt("bridge_battery",100)) {
                    Log.w (TAG, "blueReader will be turn off, as the battery is lower then " + Pref.getInt("blueReader_turn_off_value",5) +"%");
                    Home.toaststatic(xdrip.getAppContext().getString(R.string.bluereaderoff) + Pref.getInt("blueReader_turn_off_value",5) +"%");
                    return shutdown;
                }
            }
        }

        return null;
    }

}