1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > android设备连接蓝牙扫描枪获取数据

android设备连接蓝牙扫描枪获取数据

时间:2018-08-02 13:43:46

相关推荐

android设备连接蓝牙扫描枪获取数据

第一步,开启连接

首先要开启蓝牙并且找到蓝牙扫描枪连接,在自己的应用中添加权限

<uses-permission android:name="android.permission.BLUETOOTH" /><uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

第二部,建立蓝牙扫描枪帮助类

import android.bluetooth.BluetoothAdapter;import android.bluetooth.BluetoothClass;import android.bluetooth.BluetoothDevice;import android.os.Handler;import android.view.InputDevice;import android.view.KeyEvent;import java.util.Iterator;import java.util.Set;/*** 扫码枪事件解析类 by chen*/public class ScanGunKeyEventHelper {private final static long MESSAGE_DELAY = 500;//延迟500ms,判断扫码是否完成。private StringBuffer mStringBufferResult;//扫码内容private boolean mCaps;//大小写区分private final Handler mHandler;private final BluetoothAdapter mBluetoothAdapter;private final Runnable mScanningFishedRunnable;private OnScanSuccessListener mOnScanSuccessListener;private String mDeviceName;public ScanGunKeyEventHelper(OnScanSuccessListener onScanSuccessListener) {mOnScanSuccessListener = onScanSuccessListener ;mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();mStringBufferResult = new StringBuffer();mHandler = new Handler();mScanningFishedRunnable = new Runnable() {@Overridepublic void run() {performScanSuccess();}};}/*** 返回扫码成功后的结果*/private void performScanSuccess() {String barcode = mStringBufferResult.toString();if (mOnScanSuccessListener != null)mOnScanSuccessListener.onScanSuccess(barcode);mStringBufferResult.setLength(0);}/*** 扫码枪事件解析* @param event*/public void analysisKeyEvent(KeyEvent event) {int keyCode = event.getKeyCode();//字母大小写判断checkLetterStatus(event);if (event.getAction() == KeyEvent.ACTION_DOWN) {char aChar = getInputCode(event);;if (aChar != 0) {mStringBufferResult.append(aChar);}if (keyCode == KeyEvent.KEYCODE_ENTER) {//若为回车键,直接返回mHandler.removeCallbacks(mScanningFishedRunnable);mHandler.post(mScanningFishedRunnable);} else {//延迟post,若500ms内,有其他事件mHandler.removeCallbacks(mScanningFishedRunnable);mHandler.postDelayed(mScanningFishedRunnable, MESSAGE_DELAY);}}}//检查shift键private void checkLetterStatus(KeyEvent event) {int keyCode = event.getKeyCode();if (keyCode == KeyEvent.KEYCODE_SHIFT_RIGHT || keyCode == KeyEvent.KEYCODE_SHIFT_LEFT) {if (event.getAction() == KeyEvent.ACTION_DOWN) {//按着shift键,表示大写mCaps = true;} else {//松开shift键,表示小写mCaps = false;}}}//获取扫描内容private char getInputCode(KeyEvent event) {int keyCode = event.getKeyCode();char aChar;if (keyCode >= KeyEvent.KEYCODE_A && keyCode <= KeyEvent.KEYCODE_Z) {//字母aChar = (char) ((mCaps ? 'A' : 'a') + keyCode - KeyEvent.KEYCODE_A);} else if (keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9) {//数字aChar = (char) ('0' + keyCode - KeyEvent.KEYCODE_0);} else {//其他符号switch (keyCode) {case KeyEvent.KEYCODE_PERIOD:aChar = '.';break;case KeyEvent.KEYCODE_MINUS:aChar = mCaps ? '_' : '-';break;case KeyEvent.KEYCODE_SLASH:aChar = '/';break;case KeyEvent.KEYCODE_BACKSLASH:aChar = mCaps ? '|' : '\\';break;default:aChar = 0;break;}}return aChar;}public interface OnScanSuccessListener {void onScanSuccess(String barcode);}public void onDestroy() {mHandler.removeCallbacks(mScanningFishedRunnable);mOnScanSuccessListener = null;}//部分手机如三星,无法使用该方法// private void hasScanGun() {// Configuration cfg = getResources().getConfiguration();// return cfg.keyboard != Configuration.KEYBOARD_NOKEYS;// }/*** 扫描枪是否连接* @return*/public boolean hasScanGun() {if (mBluetoothAdapter == null) {return false;}Set<BluetoothDevice> blueDevices = mBluetoothAdapter.getBondedDevices();if (blueDevices == null || blueDevices.size() <= 0) {return false;}for (Iterator<BluetoothDevice> iterator = blueDevices.iterator(); iterator.hasNext(); ) {BluetoothDevice bluetoothDevice = iterator.next();if (bluetoothDevice.getBluetoothClass().getMajorDeviceClass() == BluetoothClass.Device.Major.PERIPHERAL) {mDeviceName = bluetoothDevice.getName();return isInputDeviceExist(mDeviceName);}}return false;}/*** 输入设备是否存在* @param deviceName* @return*/private boolean isInputDeviceExist(String deviceName) {int[] deviceIds = InputDevice.getDeviceIds();for (int id : deviceIds) {if (InputDevice.getDevice(id).getName().equals(deviceName)) {return true;}}return false;}/*** 是否为扫码枪事件(部分机型KeyEvent获取的名字错误)* @param event* @return*/@Deprecatedpublic boolean isScanGunEvent(KeyEvent event) {return event.getDevice().getName().equals(mDeviceName);}}

第三部,举例:在MainActivity中调用

import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.KeyEvent;import android.view.Window;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity implements ScanGunKeyEventHelper.OnScanSuccessListener{private TextView textView;private ScanGunKeyEventHelper mScanGunKeyEventHelper;@Overrideprotected void onCreate(Bundle savedInstanceState) {this.requestWindowFeature(Window.FEATURE_NO_TITLE);super.onCreate(savedInstanceState);//初始化控件initView();Log.e("boolean", mScanGunKeyEventHelper.hasScanGun() + "");}private void initView(){textView=(TextView)findViewById(R.id.text);mScanGunKeyEventHelper=new ScanGunKeyEventHelper(this);}@Overrideprotected void onResume() {super.onResume();if (!mScanGunKeyEventHelper.hasScanGun()) {Toast.makeText(MainActivity.this, "未检测到扫码枪设备", Toast.LENGTH_SHORT).show();}}/*** 截获按键事件.发给ScanGunKeyEventHelper* @param event* @return*/@Overridepublic boolean dispatchKeyEvent(KeyEvent event) {if (mScanGunKeyEventHelper.isScanGunEvent(event)) {mScanGunKeyEventHelper.analysisKeyEvent(event);return true;}return super.dispatchKeyEvent(event);}@Overridepublic void onScanSuccess(String barcode) {*//这里就是获取的扫描内容*Log.e("onScanSuccess+++++",barcode);}@Overrideprotected void onDestroy() {super.onDestroy();mScanGunKeyEventHelper.onDestroy();}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。