86 lines
2.8 KiB
Plaintext
86 lines
2.8 KiB
Plaintext
import Activity from 'android.app.Activity';
|
|
import IntentFilter from 'android.content.IntentFilter';
|
|
import BroadcastReceiver from 'android.content.BroadcastReceiver';
|
|
import Context from 'android.content.Context';
|
|
import Intent from 'android.content.Intent';
|
|
|
|
import ScanManager from 'android.device.ScanManager';
|
|
import PropertyID from 'android.device.scanner.configuration.PropertyID';
|
|
|
|
import { OnScanReceiverCallback } from '../../interface.uts';
|
|
|
|
export default class ScanBroadcastReceiver {
|
|
private static instance : ScanBroadcastReceiver = new ScanBroadcastReceiver();
|
|
|
|
private context : Context | null = null;
|
|
|
|
private scanBroadcastReceiver: UrovoScanBroadcastReceiver | null = null;
|
|
|
|
private scanManager: ScanManager | null = null;
|
|
|
|
private intentFilter: IntentFilter | null = null;
|
|
|
|
private constructor() {
|
|
}
|
|
|
|
static getInstance(context : Context | null) : ScanBroadcastReceiver {
|
|
ScanBroadcastReceiver.instance.context = context;
|
|
return ScanBroadcastReceiver.instance;
|
|
}
|
|
|
|
registerReceiver(callback: OnScanReceiverCallback) {
|
|
this.scanBroadcastReceiver = new UrovoScanBroadcastReceiver(callback);
|
|
if(this.scanManager == null) {
|
|
this.scanManager = new ScanManager();
|
|
this.scanManager!.openScanner();
|
|
this.scanManager!.switchOutputMode(0);
|
|
}
|
|
this.intentFilter = new IntentFilter();
|
|
let idbuf = intArrayOf(PropertyID.WEDGE_INTENT_ACTION_NAME, PropertyID.WEDGE_INTENT_DATA_STRING_TAG);
|
|
let value_buf = this.scanManager!.getParameterString(idbuf);
|
|
if (value_buf != null && value_buf[0] != null && !value_buf[0].equals("")) {
|
|
this.intentFilter!.addAction(value_buf[0]);
|
|
} else {
|
|
this.intentFilter!.addAction(ScanManager.ACTION_DECODE);
|
|
}
|
|
//this.intentFilter!.addAction(ScanManager.ACTION_CAPTURE_IMAGE);
|
|
|
|
UTSAndroid.getUniActivity()!.registerReceiver(this.scanBroadcastReceiver, this.intentFilter);
|
|
}
|
|
|
|
unregisterReceiver () {
|
|
let uniActivity: Activity | null = UTSAndroid.getUniActivity();
|
|
if(this.scanBroadcastReceiver != null) {
|
|
uniActivity!.unregisterReceiver(this.scanBroadcastReceiver);
|
|
}
|
|
|
|
//停止扫描
|
|
if (this.scanManager != null){
|
|
this.scanManager?.stopDecode();
|
|
}
|
|
|
|
this.scanBroadcastReceiver = null;
|
|
}
|
|
}
|
|
|
|
class UrovoScanBroadcastReceiver extends BroadcastReceiver {
|
|
|
|
private callback: OnScanReceiverCallback;
|
|
|
|
constructor(callback: OnScanReceiverCallback) {
|
|
super();
|
|
this.callback = callback;
|
|
}
|
|
|
|
override onReceive(context : Context, intent : Intent) {
|
|
let barcode = intent.getByteArrayExtra(ScanManager.DECODE_DATA_TAG);
|
|
let barcodelen = intent.getIntExtra(ScanManager.BARCODE_LENGTH_TAG, 0);
|
|
let temp = intent.getByteExtra(ScanManager.BARCODE_TYPE_TAG, 0);
|
|
console.log("----codetype--:" + temp);
|
|
if(barcode != null) {
|
|
let barcodeStr : string = new String(barcode, 0, barcodelen.toInt());
|
|
this.callback(barcodeStr);
|
|
}
|
|
|
|
}
|
|
} |