Android Question send timeticks (long from datetime) over BLE

jimseng

Active Member
Licensed User
Longtime User
Hello
I clearly don't understand how to use byteconvert. I have a long derived from a datetime dialog (yesterday's post). I now need to convert it into a byte array (manager.WriteData(data() as byte)
I am confused. So my result from my date time picker for example is: 1714156680 (0x662BF488)
How do I convert that into something I can send over bluetooth. In my simple mind I would loop through each character and send it as a byte and re-construct it at the other end but surely there is "the correct" way to do it.
 

emexes

Expert
Licensed User
a long ... convert it into a byte array

Maybe something like:
To Bytes and Back:
Dim T As Long = DateTime.Now
Log(T)
Log(Bit.ToHexStringLong(T))

Dim bc As ByteConverter
Dim B() As Byte = bc.LongsToBytes(Array As Long(T))

Log(B.As(List))    'works in B4J, might work in B4A

'Bytes are signed ie -128 to 127, use Bit.And if you need them unsigned 0..255 (you don't yet, but you might later)
Log(B.Length)
For I = 0 To B.Length - 1
    Log(I & TAB & B(I) & TAB & Bit.And(B(I), 0xFF))
Next

Dim ReceivedT As Long = bc.LongsFromBytes(B)(0)    'note the (0) at the end, to "convert" the single element Long Array to a Long, by selecting element 0

Log(ReceivedT)
Log(Bit.ToHexStringLong(ReceivedT))
Log output:
Waiting for debugger to connect...
Program started.
1714165429229
18f1c38b3ed
(ArrayList) [0, 0, 1, -113, 28, 56, -77, -19]
8
0    0      0
1    0      0
2    1      1
3    -113   143
4    28     28
5    56     56
6    -77    179
7    -19    237
1714165429229
18f1c38b3ed
 
Last edited:
Upvote 0

jimseng

Active Member
Licensed User
Longtime User
Thanks. I divide my DateTime by 1000 which I believe converts it to seconds (i'm receiving it with micropython). Why does my array start with the first four elements as zeros?
1714169040 (0x662C24D0)
(ArrayList) [0, 0, 0, 0, 102, 44, 38, 116]
Apart from that , this is very helpful.
 
Upvote 0

emexes

Expert
Licensed User
I divide my DateTime by 1000 which I believe converts it to seconds

Correct. Ticks are in milliseconds.

Unix time was originally a (signed) 32-bit integer of seconds since 01/01/1970. Maximum value of 31 bits is 2^31-1 ie tad over 2 billion. Divide by 3600 seconds/hour and 24 hours/day and 365 days/year, and you get maximum value of 68 years. 1970 + 68 = 2038 ie the Unix equivalent of Y2K.

B4X time is 64-bit integer of milliseconds since 01/01/1970. With similar math, we get maximum value of 292 million years, so we should be ok for a while. Plus it covers the entire period of human history backwards, whereas (signed 32-bit) Unix time only goes back to ~1902 (ie 1970 = 68) and thus my grandmother born in 1901 would be reborn in 2037.

Why does my array start with the first four elements as zeros?

Because dividing by 1000 shifts the number to the right (by 3 decimal digits = approximately 10 binary digits = approximately 1.25 bytes) and so the bits (value) of byte array elements 2 and 3 have moved down to elements 4 and 5. and the zero values of elements 0 and 1 have moved down to elements 2 and 3.

If you're thinking that because the first four elements are zero, that you can ignore them and just send the lower four bytes as a 32-bit Int rather than all eight bytes as a 64-bit Long, then you're back at the Unix 2038 problem. Although, yes, you could treat the four bytes as an UNSIGNED 32-bit value, which would double your 68 year maximum and push the rollover problem to your great-great-grandchildren.
 
Last edited:
Upvote 0

jimseng

Active Member
Licensed User
Longtime User
Thanks again for the explanation. I'm guessing that by 2038 I'll simply ask my fridge to program my microcontroller for me based a discussion I have with my heat-pump kettle. And I sensibly decided not to have children so I think I'll lop the first four bytes off.
I am getting the correct value via bluetooth on my Pico now. This has made me happy and I can now go to bed.
 
Upvote 0
Top