public byte[] toByteArray() {
        int size = index;
        if (bit > 0) size++;
        byte[] b = new byte[size];
        System.arraycopy(buf, 0, b, 0, size);
        return b;
    }
Gets an array containing all bits written thus far.
mesh/src/main/java/no/nordicsemi/android/mesh/utils/BitWriter.java