0

I want to set the current time as the major and minor values of a beacon. Lets suppose this is the current time 1465398279009. I want 9827 to be the value of the major and 9009 the value of the minor. I use a java code to call the shell script. this is my java code

Long millis = System.currentTimeMillis();
            String time=Long.toString(millis);
           // String major1=time.substring(...);
            String major1="99";
            String major2="99";
            String minor1="99";
            String minor2="99";
  ProcessBuilder pb2=new ProcessBuilder("/test.sh");
            pb2.environment().put("major1", major1);
            pb2.environment().put("major2", major2);
            pb2.environment().put("minor1", minor1);
            pb2.environment().put("minor2", minor2);
            Process script_exec = pb2.start();

and this is the content of test.sh

sudo hcitool -i hci0 cmd 0x08 0x0008 1e 02 01 1a 1a ff 4c 00 02 15 e2 c5 6d sd fh fb 33 j2 k9 j9 23 h2 n9 g7 v7 $param1 $major1 $major2 $minor1 $minor2 c5 00

In this example I just try to put both values to 9999 and 9999, but I get 39321 as result, I think the values are converted to big endian. I get confused and I don't understand well in which type and how shall I convert the String.

Celiiine
  • 19
  • 5
  • I know this is not the point of the question, but note that the test.sh file has illegal byte values like "j2" "j9" "n9" "g7 "v7". A hex value must contain only the characters 0-9 and a-f. The characters j, n, g and v are not legal and will cause problems. – davidgyoung Jun 08 '16 at 17:29

2 Answers2

0

Long.toString can take two parameters. The first parameter is the number and the second parameter is the radix of the conversion.

Using a radix of 16 (Long.toString(millis, 16)) would result in a standard hexstring using [1-9a-f].

Kiskae
  • 24,655
  • 2
  • 77
  • 74
0

Try this:

 String major1 = String.format("%02X", (millis >> 32) & 0xff);
 String major2 = String.format("%02X", (millis >> 16) & 0xff);
 String minor1 = String.format("%02X", (millis >> 8) & 0xff);
 String minor2 = String.format("%02X", millis & 0xff);

The code above accesses each byte out of the timestamp and assigns it to the proper field, then formats it as a two character (one byte) hex number. A two character hex number is what you want for the script.

davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • Thank you but it's not working properly,example: Time=1465471852025 , Hex=55 ED DD F9, Major=21997, Minor=63941. And I can't find the error in your code. – Celiiine Jun 09 '16 at 11:33
  • As I explained I want the last 8 numbers of the time 14654**71852025** to be the value of the major and minor. So the major should be 7185 and the minor 2025 when I detect them and not 21997and 63941. – Celiiine Jun 09 '16 at 13:28
  • is it possible to decode in the android device the value I get with your code "Major=21997, Minor=63941" to get the original timestamp ? – Celiiine Jun 09 '16 at 18:38
  • Understand there is a notational difference between numbers expressed in Hexadecimal (what you pass to the script) and Decimal (what we humans normally use and what we get back from checking the beacon major and minor). But they are equivalent. Separately, you cannot get the full original timestamp, because you only save the least significant four bytes into the major and minor. You *can* get this four byte subset of the timestamp value back with `(major*65536l)+minor`. This will be the same as `timestamp & 0xFFFFFFFFl` before encoding into major and minor. – davidgyoung Jun 11 '16 at 18:56