| 
 | 1 | +import {  | 
 | 2 | +  registerBroadcastReceiver,  | 
 | 3 | +  sendBroadcastWithExtras,  | 
 | 4 | +} from 'react-native-datawedge-intents';  | 
 | 5 | + | 
 | 6 | +const config_scanner = {  | 
 | 7 | +  name: 'Example',  | 
 | 8 | +  package: 'datawedgeintents.example',  | 
 | 9 | +};  | 
 | 10 | + | 
 | 11 | +let ean8checked = true;  | 
 | 12 | +let ean13checked = true;  | 
 | 13 | +let code39checked = true;  | 
 | 14 | +let code128checked = true;  | 
 | 15 | +let isSendResult = false;  | 
 | 16 | + | 
 | 17 | +export function ScannerInit() {  | 
 | 18 | +  registerBroadcastReceiver({  | 
 | 19 | +    filterActions: [  | 
 | 20 | +      'com.zebra.reactnativedemo.ACTION',  | 
 | 21 | +      'com.symbol.datawedge.api.RESULT_ACTION',  | 
 | 22 | +    ],  | 
 | 23 | +    filterCategories: ['android.intent.category.DEFAULT'],  | 
 | 24 | +  });  | 
 | 25 | +  sendCommand('com.symbol.datawedge.api.GET_VERSION_INFO', '');  | 
 | 26 | +  ScannerDecoder();  | 
 | 27 | +}  | 
 | 28 | + | 
 | 29 | +export function ScannerTrigger() {  | 
 | 30 | +  sendCommand('com.symbol.datawedge.api.SOFT_SCAN_TRIGGER', 'TOGGLE_SCANNING');  | 
 | 31 | +}  | 
 | 32 | + | 
 | 33 | +export function ScannerDecoder() {  | 
 | 34 | +  // NOTE: Set the new configuration  | 
 | 35 | +  const body = {  | 
 | 36 | +    PROFILE_NAME: config_scanner.name,  | 
 | 37 | +    PROFILE_ENABLED: 'true',  | 
 | 38 | +    CONFIG_MODE: 'UPDATE',  | 
 | 39 | +    PLUGIN_CONFIG: {  | 
 | 40 | +      PLUGIN_NAME: 'BARCODE',  | 
 | 41 | +      PARAM_LIST: {  | 
 | 42 | +        scanner_selection: 'auto',  | 
 | 43 | +        decoder_ean8: '' + ean8checked,  | 
 | 44 | +        decoder_ean13: '' + ean13checked,  | 
 | 45 | +        decoder_code128: '' + code128checked,  | 
 | 46 | +        decoder_code39: '' + code39checked,  | 
 | 47 | +      },  | 
 | 48 | +    },  | 
 | 49 | +  };  | 
 | 50 | +  sendCommand('com.symbol.datawedge.api.SET_CONFIG', body);  | 
 | 51 | +}  | 
 | 52 | + | 
 | 53 | +function sendCommand(extraName: string, extraValue: any) {  | 
 | 54 | +  console.log(  | 
 | 55 | +    'Sending Command: ' + extraName + ', ' + JSON.stringify(extraValue)  | 
 | 56 | +  );  | 
 | 57 | +  var broadcastExtras: any = {};  | 
 | 58 | +  broadcastExtras[extraName] = extraValue;  | 
 | 59 | +  broadcastExtras.SEND_RESULT = '' + isSendResult;  | 
 | 60 | +  sendBroadcastWithExtras({  | 
 | 61 | +    action: 'com.symbol.datawedge.api.ACTION',  | 
 | 62 | +    extras: broadcastExtras,  | 
 | 63 | +  });  | 
 | 64 | +}  | 
 | 65 | + | 
 | 66 | +export function ScannerReceiver(intent: any): any {  | 
 | 67 | +  console.log('Received Intent: ' + JSON.stringify(intent));  | 
 | 68 | +  if (  | 
 | 69 | +    intent.hasOwnProperty('com.symbol.datawedge.api.RESULT_GET_VERSION_INFO')  | 
 | 70 | +  ) {  | 
 | 71 | +    // NOTE: The version has been returned (DW 6.3 or higher).  Includes the DW version along with other subsystem versions e.g MX  | 
 | 72 | +    const versionInfo =  | 
 | 73 | +      intent['com.symbol.datawedge.api.RESULT_GET_VERSION_INFO'];  | 
 | 74 | +    console.log('Version Info: ' + JSON.stringify(versionInfo));  | 
 | 75 | +    const datawedgeVersion = versionInfo.DATAWEDGE;  | 
 | 76 | +    console.log('Datawedge version: ' + datawedgeVersion);  | 
 | 77 | + | 
 | 78 | +    // NOTE: Fire events sequentially so the application can gracefully degrade the functionality available on earlier DW versions  | 
 | 79 | +    if (datawedgeVersion >= '06.3') {  | 
 | 80 | +      datawedge63();  | 
 | 81 | +    }  | 
 | 82 | +    if (datawedgeVersion >= '06.4') {  | 
 | 83 | +      datawedge64();  | 
 | 84 | +    }  | 
 | 85 | +    if (datawedgeVersion >= '06.5') {  | 
 | 86 | +      datawedge65();  | 
 | 87 | +    }  | 
 | 88 | +  } else if (  | 
 | 89 | +    intent.hasOwnProperty('com.symbol.datawedge.api.RESULT_ENUMERATE_SCANNERS')  | 
 | 90 | +  ) {  | 
 | 91 | +    // NOTE: Return from our request to enumerate the available scanners  | 
 | 92 | +    const enumeratedScannersObj =  | 
 | 93 | +      intent['com.symbol.datawedge.api.RESULT_ENUMERATE_SCANNERS'];  | 
 | 94 | +    enumerateScanners(enumeratedScannersObj);  | 
 | 95 | +  } else if (!intent.hasOwnProperty('RESULT_INFO')) {  | 
 | 96 | +    // NOTE: A barcode has been scanned  | 
 | 97 | +    const scannedData = intent['com.symbol.datawedge.data_string'];  | 
 | 98 | +    const scannedType = intent['com.symbol.datawedge.label_type'];  | 
 | 99 | +    if (scannedData) {  | 
 | 100 | +      return {  | 
 | 101 | +        data: scannedData,  | 
 | 102 | +        decoder: scannedType,  | 
 | 103 | +        timeAtDecode: new Date().toISOString(),  | 
 | 104 | +      };  | 
 | 105 | +    } else {  | 
 | 106 | +      return null;  | 
 | 107 | +    }  | 
 | 108 | +  }  | 
 | 109 | +}  | 
 | 110 | + | 
 | 111 | +function datawedge63() {  | 
 | 112 | +  sendCommand('com.symbol.datawedge.api.CREATE_PROFILE', config_scanner.name);  | 
 | 113 | +  sendCommand('com.symbol.datawedge.api.GET_ACTIVE_PROFILE', ''); // NOTE: Although we created the profile we can only configure it with DW 6.4.  | 
 | 114 | +  sendCommand('com.symbol.datawedge.api.ENUMERATE_SCANNERS', ''); // NOTE: Enumerate the available scanners on the device  | 
 | 115 | +}  | 
 | 116 | + | 
 | 117 | +function datawedge64() {  | 
 | 118 | +  // NOTE: Configure the created profile (associated app and keyboard plugin)  | 
 | 119 | +  const bodyAssociate = {  | 
 | 120 | +    PROFILE_NAME: config_scanner.name,  | 
 | 121 | +    PROFILE_ENABLED: 'true',  | 
 | 122 | +    CONFIG_MODE: 'UPDATE',  | 
 | 123 | +    PLUGIN_CONFIG: {  | 
 | 124 | +      PLUGIN_NAME: 'BARCODE',  | 
 | 125 | +      RESET_CONFIG: 'true',  | 
 | 126 | +      PARAM_LIST: {},  | 
 | 127 | +    },  | 
 | 128 | +    APP_LIST: [  | 
 | 129 | +      {  | 
 | 130 | +        PACKAGE_NAME: config_scanner.package,  | 
 | 131 | +        ACTIVITY_LIST: ['*'],  | 
 | 132 | +      },  | 
 | 133 | +    ],  | 
 | 134 | +  };  | 
 | 135 | +  sendCommand('com.symbol.datawedge.api.SET_CONFIG', bodyAssociate);  | 
 | 136 | + | 
 | 137 | +  // NOTE: Configure the created profile (intent plugin)  | 
 | 138 | +  const bodyIntent = {  | 
 | 139 | +    PROFILE_NAME: config_scanner.name,  | 
 | 140 | +    PROFILE_ENABLED: 'true',  | 
 | 141 | +    CONFIG_MODE: 'UPDATE',  | 
 | 142 | +    PLUGIN_CONFIG: {  | 
 | 143 | +      PLUGIN_NAME: 'INTENT',  | 
 | 144 | +      RESET_CONFIG: 'true',  | 
 | 145 | +      PARAM_LIST: {  | 
 | 146 | +        intent_output_enabled: 'true',  | 
 | 147 | +        intent_action: 'com.zebra.reactnativedemo.ACTION',  | 
 | 148 | +        intent_delivery: '2',  | 
 | 149 | +      },  | 
 | 150 | +    },  | 
 | 151 | +  };  | 
 | 152 | +  sendCommand('com.symbol.datawedge.api.SET_CONFIG', bodyIntent);  | 
 | 153 | + | 
 | 154 | +  // NOTE: Give some time for the profile to settle then query its value  | 
 | 155 | +  setTimeout(() => {  | 
 | 156 | +    sendCommand('com.symbol.datawedge.api.GET_ACTIVE_PROFILE', '');  | 
 | 157 | +  }, 1000);  | 
 | 158 | +}  | 
 | 159 | + | 
 | 160 | +function datawedge65() {  | 
 | 161 | +  // NOTE: Instruct the API to send  | 
 | 162 | +  isSendResult = true;  | 
 | 163 | +}  | 
 | 164 | + | 
 | 165 | +function enumerateScanners(enumeratedScanners: any) {  | 
 | 166 | +  let humanReadableScannerList = '';  | 
 | 167 | +  for (let i = 0; i < enumeratedScanners.length; i++) {  | 
 | 168 | +    console.log(  | 
 | 169 | +      'Scanner found: name= ' +  | 
 | 170 | +        enumeratedScanners[i].SCANNER_NAME +  | 
 | 171 | +        ', id=' +  | 
 | 172 | +        enumeratedScanners[i].SCANNER_INDEX +  | 
 | 173 | +        ', connected=' +  | 
 | 174 | +        enumeratedScanners[i].SCANNER_CONNECTION_STATE  | 
 | 175 | +    );  | 
 | 176 | +    humanReadableScannerList += enumeratedScanners[i].SCANNER_NAME;  | 
 | 177 | +    if (i < enumeratedScanners.length - 1) {  | 
 | 178 | +      humanReadableScannerList += ', ';  | 
 | 179 | +    }  | 
 | 180 | +  }  | 
 | 181 | +  return humanReadableScannerList;  | 
 | 182 | +}  | 
0 commit comments