Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. private byte[] writeSampleData() {
  2.  
  3. ByteArrayOutputStream bo = new ByteArrayOutputStream();
  4. DataOutputStream os = new DataOutputStream(bo);
  5.  
  6. os.writeInt(1); //operation code
  7. os.writeInt(1); //haul QTY
  8. os.writeInt(1); //haul count
  9.  
  10. long time = new Date().getTime(); // time in ms since epoch
  11. time /= 86400000; // ms in a day
  12. byte timeA = (byte) (time >>> 8);
  13. byte timeB = (byte) (time);
  14.  
  15. os.writeByte(timeA);
  16. os.writeByte(timeB);
  17.  
  18. os.writeInt(32790); //latitude
  19. os.writeInt(1017409); //longitude
  20.  
  21.  
  22. os.writeInt(10); // fish count
  23.  
  24. //loop
  25. os.writeInt(1234); // fish code
  26. os.writeInt(12345); // fish quantity
  27. os.writeInt(12345); // fish weight
  28.  
  29. os.writeInt(12); // sample fork length
  30. os.writeInt(10); // sample weight
  31.  
  32. buffer = bo.toByteArray();
  33. return buffer;
  34. }
  35.  
  36. private class SendSampleData extends AsyncTask<Void, Void, Boolean>{
  37.  
  38. public SendSampleData(Activity activity){
  39. }
  40.  
  41. @Override
  42. protected Boolean doInBackground(Void... voids) {
  43. byte[] bytes = writeSampleData();
  44.  
  45.  
  46. // write buffer length
  47. StringBuilder stringBuilder = new StringBuilder();
  48. stringBuilder.append("AT+SBDWB=");
  49. stringBuilder.append(bytes.length);
  50. usbService.writeSync(stringBuilder.
  51.  
  52. String writeLine = usbService.readLine(500);
  53. //waiting for READY
  54. if(!"".equals(writeLine) && writeLine.contains("READY")) {
  55.  
  56. //generate byte with last 2 checksum
  57. byte[] prepareBytes = prepareByte(bytes);
  58.  
  59. //write byte + 2 checksum
  60. usbService.writeSync(prepareBytes);
  61. String writeLine2 = usbService.readLine(3000);
  62.  
  63. // attempt checking the session by running SBDIX
  64. boolean attemptStatus = usbService._attemptSession();
  65. return attemptStatus;
  66. }
  67. return false;
  68. }
  69.  
  70. @Override
  71. protected void onPostExecute(Boolean aBoolean) {
  72. super.onPostExecute(aBoolean);
  73. String status = aBoolean ? "Success" : "Failed";
  74. Log.d("MY_APP", "Result: "+ status);
  75. }
  76. }
  77.  
  78.  
  79. private static byte[] prepareByte(byte[] bytes) {
  80. try {
  81. byte[] mByte = bytes;
  82. short sum = 0;
  83.  
  84. ByteArrayOutputStream output = new ByteArrayOutputStream();
  85.  
  86. for(int i = 0; i < mByte.length; i++) {
  87. output.write(mByte[i]);
  88. sum += (mByte[i] & 0x0000ffff);
  89. }
  90.  
  91. int first = (sum >> 8);
  92. int second = (sum & 0xFF);
  93.  
  94. output.write(first);
  95. output.write(second);
  96.  
  97. byte[] out = output.toByteArray();
  98.  
  99. return out;
  100. } catch (Exception e) {
  101. return null;
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement