Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to connect bluetooth scanner without input box with Android

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to connect Bluetooth scanner without input box with Android". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to connect Bluetooth scanner without input box with Android" can help you solve the problem.

Android's APP needs to integrate a Bluetooth scanner. In particular, there is no input box (EditText) where the code needs to be scanned. It cannot be intuitively understood by listening to EditText input events to get the sweep result. And the device does not provide SDK.

After thinking about it, the Bluetooth scanner should essentially be a HID device, equivalent to a Bluetooth keyboard. And then it became clear.

Each code scan should trigger a keystroke event. By monitoring the keystroke event of the current Activity, it should be possible to obtain the code scan result without an input box.

Overload dispatchKeyEvent in Activity for key listening.

@ Override public boolean dispatchKeyEvent (KeyEvent event) {InputDevice inputDevice = event.getDevice (); if (inputDevice! = null) {int keyboardType = event.getDevice () .getKeyboardType (); String deviceName = event.getDevice () .getName (); boolean isVirtual = event.getDevice () .isVirtual () / / you can determine whether to input if (! isVirtual) {scannerHelper.onScanerInput (event);} for a specific decoder according to the input device information.

Usually, the scanner appends an end character after the scan result, and my device defaults to enter. Therefore, the receipt of an enter can be regarded as a scan result "input" completed.

Public class ScannerHelper {private String buffer= "; private MainOneActivity mainOneActivity; public ScannerHelper (MainOneActivity mainOneActivity) {this.mainOneActivity = mainOneActivity;} public void onScanerInput (KeyEvent event) {if (event.getKeyCode () = = KeyEvent.KEYCODE_ENTER) {mainOneActivity.onScannerResult (buffer); / / callback scan result buffer=" } else {if (event.getAction () = = KeyEvent.ACTION_UP & & event.isPrintingKey ()) {buffer + = (char) event.getUnicodeChar ();}

Test OK.

This is the end of the content about "how to connect the Bluetooth scanner without input box with Android". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report