Get the App
SLTechnology News&Howtos  ›  Development  › 

How to connect bluetooth scanner without input box with Android

Shulou Source: shulou.com Published: 2022-06-01 12:06:18 10月04日 Update

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.

Tags: Input Bluetooth results devices events buttons knowledge monitoring industry different practical open suddenly enlightened information content place characters practicality actual situation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology NVidia Shulou Information Docker MariaDB