• 0
  • If you guys are familiar with the BLE firmware, you might have noticed that packets are often enough corrupted on the receiving end (Android, iOS, etc). To combat this issue, one can implement a simple one byte checksum into the transmission so the receiving end can generate the same checksum and compare it with the checksum byte received from the CBT.

    For example: on the CBT

    byte checksum(byte *payload, const byte length){
    	byte chk;
    	for(byte i = 0; i < length; i++){
    	   chk += payload[i];    //value wraps around once it reaches > 255
    	}
    	return ~chk;
    }
    
    byte bluetoothOut[5];
    bluetoothOut[0] = speedMPH;
    bluetoothOut[1] = (byte)engineRPM << 8;
    bluetoothOut[2] = (byte)engineRPM;
    bluetoothOut[3] = (byte)throttlePosition;
    bluetoothOut[4] = checksum(bluetoothOut, 4); //5th byte is checksum for bytes 1-4
    

    And for android:

    private boolean validCheck(final byte[] bluetooth_rx_data){
        int checksum = 0;
        int cmp = (bluetooth_rx_data[bluetooth_rx_data.length - 1] & 0xFF);
        for(int i = 0; i <bluetooth_rx_data.length - 1; i++){
            int value = (bluetooth_rx_data[i] & 0xFF);
            checksum += value;
        }
        checksum = ~checksum & 0xFF;
        return (checksum == cmp);
    }