Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. package com.auexpress.scan.controller;
  2.  
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.IntentFilter;
  7. import android.posapi.PosApi;
  8.  
  9. import com.auexpress.scan.utils.ScanCallback;
  10. import com.auexpress.scan.model.ScanResult;
  11.  
  12. /**
  13. * Created by auexpress on 2017-11-17.
  14. */
  15. public class DeviceIOController extends BroadcastReceiver{
  16.  
  17. //statics
  18. private static byte mGpioPower = 0x1E;// PB14
  19. private static byte mGpioTrig = 0x29;// PC9
  20.  
  21. private static int mCurSerialNo = 3; // usart3
  22. private static int mBaudrate = 4; // 9600
  23.  
  24. //
  25. private static DeviceIOController controller;
  26.  
  27. public static DeviceIOController getInstance(){
  28. if(controller == null){
  29. controller = new DeviceIOController();
  30. }
  31. return controller;
  32. }
  33.  
  34.  
  35. //current callback activity
  36. private ScanCallback callback;
  37. private PosApi api;
  38.  
  39. public void init(Context applicationContext){
  40. api = PosApi.getInstance(applicationContext);
  41. api.initPosDev(PosApi.PRODUCT_MODEL_IMA80M01);
  42. IntentFilter mFilter = new IntentFilter();
  43. mFilter.addAction(PosApi.ACTION_POS_COMM_STATUS);
  44. applicationContext.registerReceiver(this,mFilter);
  45. }
  46.  
  47. public void register(ScanCallback callback){
  48. this.callback = callback;
  49. }
  50.  
  51. public void unregister(ScanCallback callback){
  52. if(this.callback == callback){
  53. this.callback = null;
  54. }
  55. }
  56.  
  57. void onScanReceived(String info){
  58. callback.scanCallback(new ScanResult(info));
  59. }
  60.  
  61. @Override
  62. public void onReceive(Context context, Intent intent) {
  63. String action = intent.getAction();
  64. System.err.println("received! " + action );
  65. if(action.equalsIgnoreCase(PosApi.ACTION_POS_COMM_STATUS)){
  66. int cmdFlag = intent.getIntExtra(PosApi.KEY_CMD_FLAG, -1);
  67. //int status = intent.getIntExtra(PosApi.KEY_CMD_STATUS , -1);
  68. int bufferLen = intent.getIntExtra(PosApi.KEY_CMD_DATA_LENGTH, 0);
  69. byte [] buffer = intent.getByteArrayExtra(PosApi.KEY_CMD_DATA_BUFFER);
  70. switch(cmdFlag){
  71. case PosApi.POS_EXPAND_SERIAL3:
  72. if(buffer == null ) return;
  73. if(bufferLen < 12 ) return; //ignore noise
  74.  
  75. StringBuffer sb = new StringBuffer();
  76. for(int i = 0;i<buffer.length;i++){
  77. if(buffer[i]==0x0D){
  78. sb.append("\n");
  79. }else{
  80. sb.append((char)buffer[i]);
  81. }
  82. }
  83. onScanReceived(sb.toString());
  84. break;
  85. }
  86. }
  87.  
  88. }
  89.  
  90.  
  91. private void openDevice() {
  92. // open power
  93. api.gpioControl(mGpioPower, 0, 1);
  94.  
  95. api.extendSerialInit(mCurSerialNo, mBaudrate, 1, 1, 1, 1);
  96.  
  97. }
  98.  
  99. //call by scan button
  100. public void scan() {
  101. openDevice();
  102. }
  103.  
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement