Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private byte[] writeSampleData() {
- ByteArrayOutputStream bo = new ByteArrayOutputStream();
- DataOutputStream os = new DataOutputStream(bo);
- os.writeInt(1); //operation code
- os.writeInt(1); //haul QTY
- os.writeInt(1); //haul count
- long time = new Date().getTime(); // time in ms since epoch
- time /= 86400000; // ms in a day
- byte timeA = (byte) (time >>> 8);
- byte timeB = (byte) (time);
- os.writeByte(timeA);
- os.writeByte(timeB);
- os.writeInt(32790); //latitude
- os.writeInt(1017409); //longitude
- os.writeInt(10); // fish count
- //loop
- os.writeInt(1234); // fish code
- os.writeInt(12345); // fish quantity
- os.writeInt(12345); // fish weight
- os.writeInt(12); // sample fork length
- os.writeInt(10); // sample weight
- buffer = bo.toByteArray();
- return buffer;
- }
- private class SendSampleData extends AsyncTask<Void, Void, Boolean>{
- public SendSampleData(Activity activity){
- }
- @Override
- protected Boolean doInBackground(Void... voids) {
- byte[] bytes = writeSampleData();
- // write buffer length
- StringBuilder stringBuilder = new StringBuilder();
- stringBuilder.append("AT+SBDWB=");
- stringBuilder.append(bytes.length);
- usbService.writeSync(stringBuilder.
- String writeLine = usbService.readLine(500);
- //waiting for READY
- if(!"".equals(writeLine) && writeLine.contains("READY")) {
- //generate byte with last 2 checksum
- byte[] prepareBytes = prepareByte(bytes);
- //write byte + 2 checksum
- usbService.writeSync(prepareBytes);
- String writeLine2 = usbService.readLine(3000);
- // attempt checking the session by running SBDIX
- boolean attemptStatus = usbService._attemptSession();
- return attemptStatus;
- }
- return false;
- }
- @Override
- protected void onPostExecute(Boolean aBoolean) {
- super.onPostExecute(aBoolean);
- String status = aBoolean ? "Success" : "Failed";
- Log.d("MY_APP", "Result: "+ status);
- }
- }
- private static byte[] prepareByte(byte[] bytes) {
- try {
- byte[] mByte = bytes;
- short sum = 0;
- ByteArrayOutputStream output = new ByteArrayOutputStream();
- for(int i = 0; i < mByte.length; i++) {
- output.write(mByte[i]);
- sum += (mByte[i] & 0x0000ffff);
- }
- int first = (sum >> 8);
- int second = (sum & 0xFF);
- output.write(first);
- output.write(second);
- byte[] out = output.toByteArray();
- return out;
- } catch (Exception e) {
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement