简体   繁体   English

在Android中通过蓝牙接收数据

[英]Receive data via Bluetooth in Android

I have to realize an app that receive data via bluetooth from a 1D laser barcode scanner , here the description page. 我必须实现一个可以从一维激光条形码扫描仪此处为描述页面)通过蓝牙接收数据的应用程序。

This scanner works perfectly if you select, for example, an EditText : while the EditText has the focus, when you scan a barcode, it receives the content of the barcode and "magically" filled with this content. 如果您选择例如EditText ,则此扫描仪将完美工作:当EditText具有焦点时,当您扫描条形码时,它将接收条形码的内容并“神奇地”填充该内容。

The problem is that I would like not have the focus on an EditText , I would like the user scan the barcode, the app receive the data with a delegate method or in a socket, I parse the data and automatically put it in the correct EditText (every barcode have a check digit that permit me to recognize what is it). 问题是我不想将注意力集中在EditText ,我希望用户扫描条形码,应用程序使用委托方法或在套接字中接收数据,我解析数据并将其自动放入正确的EditText中。 (每个条形码都有一个校验位,使我可以识别出它是什么)。

The scanner is automatically connected via bluetooth so, I only need to understand how to stay listen on the socket and parsing the received data. 扫描仪通过蓝牙自动连接,因此,我只需要了解如何在套接字上继续监听并解析接收到的数据即可。

Ideas ? 想法?

There is no magic involved :). 没有魔术:)。 That scanner is simulating a bluetooth keyboard (HID interface). 该扫描仪正在模拟蓝牙键盘(HID接口)。 Google removed the ability to connect L2CAP sockets starting in Android 4.2, so you probably won't be able to get a socket connection to the device. Google从Android 4.2开始取消了连接L2CAP套接字的功能,因此您可能无法与该设备建立套接字连接。 What you can do is capture the keyboard input the same as the EditText field does. 您可以执行的操作是捕获键盘输入,就像EditText字段一样。 You will need to get the input focus to your main window and override this function in your java code: 您需要将输入焦点移至主窗口,并在Java代码中覆盖此函数:

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent msg)
    {
    }

You will then be able to receive a series of characters from the device and build/parse the resulting strings. 然后,您将能够从设备接收一系列字符并构建/解析结果字符串。

User zxing library for excellent barcode reading 用户zxing库可实现出色的条形码读取

https://code.google.com/p/zxing/source/browse/trunk#trunk/core/src/com/google/zxing https://code.google.com/p/zxing/source/browse/trunk#trunk/core/src/com/google/zxing

Voteup if it is helpful for you 投票(如果对您有帮助)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM