最近公司接了一个手持PDA扫描仪的项目,说是Android版本的,要我去查资料做这个东西。
在我一顿摸索下算是完成了,所以才想写一篇博客给第一次接触这个东西的人。希望能帮到你们
刚接到的时候我是一头雾水,因为一直在做手机app突然弄个手持设备有点懵逼(本人也刚入行没两年,水平差)
然后各种查资料,发现少之又少,所以来补充一点资料方便后人查询
开发步骤:首先你拿到设备的时候要厂家要到开发包,就是类似于demo之类的东西,然后运行一下,找到关键的项目(扫描枪找到调用了扫描头的demo就行了)
然后查看源码,可以看到调用扫描头其实就是获取广播,获取到了广播然后提取广播里传过来的信息就行了,一般是一串字符串。然后你就可以做自己想做的事情了
当时我发现手持PDA怎么都连不上eclipse,后来发现只要导出apk在设备上运行就行了,其实和普通app开发一样的(这个问题我纠结了很久)
下面贴一个代码出来吧(重要的只有广播那一点,其他的只是demo的业务逻辑)
public class MainActivity extends Activity {
private final static String SCAN_ACTION = ScanManager.ACTION_DECODE;//default action
private ActionBar actionBar;
private EditText showScanResult;
private Button btn;
private Button mScan;
private Button mClose;
private int type;
private int outPut;
private Vibrator mVibrator;
private ScanManager mScanManager;
private SoundPool soundpool = null;
private int soundid;
private String barcodeStr;
private boolean isScaning = false;
private BroadcastReceiver mScanReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
isScaning = false;
soundpool.play(soundid, 1, 1, 0, 0, 1);
showScanResult.setText("");
mVibrator.vibrate(100);
byte[] barcode = intent.getByteArrayExtra(ScanManager.DECODE_DATA_TAG);
int barcodelen = intent.getIntExtra(ScanManager.BARCODE_LENGTH_TAG, 0);
byte temp = intent.getByteExtra(ScanManager.BARCODE_TYPE_TAG, (byte) 0);
android.util.Log.i("debug", "----codetype--" + temp);
barcodeStr = new String(barcode, 0, barcodelen);
showScanResult.setText(barcodeStr);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);
mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
actionBar = getActionBar();
actionBar.show();
setupView();
}
private void initScan() {
// TODO Auto-generated method stub
mScanManager = new ScanManager();
mScanManager.openScanner();
mScanManager.switchOutputMode( 0);
soundpool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 100); // MODE_RINGTONE
soundid = soundpool.load("/etc/Scan_new.ogg", 1);
}
private void setupView() {
// TODO Auto-generated method stub
showScanResult = (EditText) findViewById(R.id.scan_result);
btn = (Button) findViewById(R.id.manager);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(mScanManager.getTriggerMode() != Triggering.CONTINUOUS)
mScanManager.setTriggerMode(Triggering.CONTINUOUS);
}
});
mScan = (Button) findViewById(R.id.scan);
mScan.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//if(type == 3)
mScanManager.stopDecode();
isScaning = true;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mScanManager.startDecode();
}
});
mClose = (Button) findViewById(R.id.close);
mClose.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// if(isScaning) {
// isScaning = false;
mScanManager.stopDecode();
//}
}
});
//btn.setVisibility(View.GONE);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if(mScanManager != null) {
mScanManager.stopDecode();
isScaning = false;
}
unregisterReceiver(mScanReceiver);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
initScan();
showScanResult.setText("");
IntentFilter filter = new IntentFilter();
int[] idbuf = new int[]{PropertyID.WEDGE_INTENT_ACTION_NAME, PropertyID.WEDGE_INTENT_DATA_STRING_TAG};
String[] value_buf = mScanManager.getParameterString(idbuf);
if(value_buf != null && value_buf[0] != null && !value_buf[0].equals("")) {
filter.addAction(value_buf[0]);
} else {
filter.addAction(SCAN_ACTION);
}
registerReceiver(mScanReceiver, filter);
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.activity_main, menu);
MenuItem settings = menu.add(0, 1, 0, R.string.menu_settings).setIcon(R.drawable.ic_action_settings);
// 绑定到actionbar
//SHOW_AS_ACTION_IF_ROOM 显示此项目在动作栏按钮如果系统决定有它。 可以用1来代替
MenuItem version = menu.add(0, 2, 0, R.string.menu_settings);
settings.setShowAsAction(1);
version.setShowAsAction(0);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case 1:
try{
Intent intent = new Intent("android.intent.action.SCANNER_SETTINGS");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
} catch (ActivityNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}
break;
case 2:
PackageManager pk = getPackageManager();
PackageInfo pi;
try {
pi = pk.getPackageInfo(getPackageName(), 0);
Toast.makeText(this, "V" +pi.versionName , Toast.LENGTH_SHORT).show();
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
return super.onOptionsItemSelected(item);
}
}