【问题标题】:How to read data from bluetooth barcode scanner Symbol CS3070 to Android Device如何从蓝牙条码扫描器 Symbol CS3070 读取数据到 Android 设备
【发布时间】:2012-05-30 21:48:50
【问题描述】:

在我的项目中,我必须使用条形码扫描仪 Symbol CS3070 通过蓝牙读取条形码。 IE;我必须通过蓝牙在 android 设备和条形码扫描仪之间建立连接。谁能告诉我如何从条形码阅读器读取值以及如何设置通信?我已经阅读了Bluetooth Developer Guide,并且我不想在蓝牙键盘仿真 (HID) 模式下使用条形码阅读器(我有一些可以使用软键盘和条形码阅读器填充的文本视图,但我无法控制重点)

我会使用这样的线程与读者交流

    private class BarcodeReaderThread extends Thread {
    private final BluetoothServerSocket mmServerSocket;

    public BarcodeReaderThread(UUID UUID_BLUETOOTH) {
        // Use a temporary object that is later assigned to mmServerSocket,
        // because mmServerSocket is final
        BluetoothServerSocket tmp = null;
        try {
            // MY_UUID is the app's UUID string, also used by the client code
            tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord("BarcodeScannerForSGST", UUID_BLUETOOTH);
            /*
             * The UUID is also included in the SDP entry and will be the basis for the connection
             * agreement with the client device. That is, when the client attempts to connect with this device,
             * it will carry a UUID that uniquely identifies the service with which it wants to connect.
             * These UUIDs must match in order for the connection to be accepted (in the next step)
             */
        } catch (IOException e) { }
        mmServerSocket = tmp;
    }

    public void run() {
        BluetoothSocket socket = null;
        // Keep listening until exception occurs or a socket is returned
        while (true) {
            try {
                socket = mmServerSocket.accept();
                try {
                    // If a connection was accepted
                    if (socket != null) {
                        // Do work to manage the connection (in a separate thread)
                        InputStream mmInStream = null;

                        // Get the input and output streams, using temp objects because
                        // member streams are final
                        mmInStream = socket.getInputStream();

                        byte[] buffer = new byte[1024];  // buffer store for the stream
                        int bytes; // bytes returned from read()

                        // Keep listening to the InputStream until an exception occurs
                        // Read from the InputStream
                        bytes = mmInStream.read(buffer);
                        if (bytes > 0) {
                            // Send the obtained bytes to the UI activity
                            String readMessage = new String(buffer, 0, bytes);
                            //doMainUIOp(BARCODE_READ, readMessage);
                            if (readMessage.length() > 0 && !etMlfb.isEnabled()) //Se sono nella parte di picking
                                new ServerWorker().execute(new Object[] {LEGGI_SPED, readMessage});
                        }
                        socket.close();
                    }
                }
                catch (Exception ex) { } 
            } catch (IOException e) {
                break;
            }
        }
    }

    /** 
     * Will cancel the listening socket, and cause the thread to finish
     */
    public void cancel() {
        try {
            mmServerSocket.close();
        } catch (IOException e) { }
    }
}

谢谢

【问题讨论】:

  • 我的应用程序需要相同的功能,如果您发现任何与此任务相关的有用信息,请告诉我。
  • 我也在尝试实现这一点,如果您找到解决方案,请告诉我。谢谢。
  • 您好!我想知道您是否需要读取蓝牙设备!我遇到了你提到的同样的问题! @Android84
  • 您好@sioesi,是的,下面的解决方案解决了问题,但最好在蓝牙键盘仿真模式下使用条形码扫描仪
  • @Android84 也是?你觉得我们可以联系吗?

标签: android bluetooth barcode


【解决方案1】:

我刚收到我的设备,当我配对并连接设备时,它会自动将数据发送到当前聚焦的 EditText。您使用的是什么版本的 Android,因为我在 ICS 和 JB 上尝试过它,它以这种方式工作。 我没有在任何早期版本中测试过它。

编辑:

我将手机降级为 Gingerbread,发现它的工作方式不一样,但我有一个解决方案:

这很重要! >> 首先,您必须扫描手册中写有“Serial Port Profile (SPP)”的条形码。

btAdapter = BluetoothAdapter.getDefaultAdapter();
if (btAdapter.isEnabled())
{
    new BluetoothConnect().execute("");
}

public class BluetoothConnect extends AsyncTask<String, String, Void>
{
    public static String MY_UUID = "00001101-0000-1000-8000-00805F9B34FB";

    @Override
    protected Void doInBackground(String... params)
    {
        String address = DB.GetOption("bluetoothAddress");
        BluetoothDevice device = btAdapter.getRemoteDevice(address);
        try
        {
            socket = device.createRfcommSocketToServiceRecord(UUID.fromString(MY_UUID));
            btAdapter.cancelDiscovery();
            socket.connect();
            InputStream stream = socket.getInputStream();
            int read = 0;
            byte[] buffer = new byte[128];
            do
            {
                try
                {
                    read = stream.read(buffer);
                    String data = new String(buffer, 0, read);
                    publishProgress(data);
                }
                catch(Exception ex)
                {
                    read = -1;
                }
            }
            while (read > 0);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(String... values)
    {
        if (values[0].equals("\r"))
        {
            addToList(input.getText().toString());
            pickupInput.setText("");
        }
        else input.setText(values[0]);
        super.onProgressUpdate(values);
    }
}

这是我工作代码的不完整版本,但您应该了解要点。
我希望这个解决方案也适用于您!

【讨论】:

  • +1 你能附加完整的代码吗?我正在尝试在 SPP 模式下使用相同的条形码阅读器。
  • 我提供的代码就足够了,你只需要知道设备的地址。您可以在配对后调用 getBondedDevices() 来获取它。在这里查看更多信息:developer.android.com/guide/topics/connectivity/bluetooth.html
  • 您只需使用 TextView.setText() 方法并将您从套接字读取的数据传递给它
  • 当你调用socket = device.createRfcommSocketToServiceRecord();然后 socket.connect();它将尝试连接到扫描仪。连接后,您使用 socket.getInputStream(); 创建一个 InputStream。当你调用 Stream.read();它将等待扫描仪发送数据。一旦你扫描了一些东西,它就会从流中读取字节。将字节数组转换为字符串,这就是您的数据!我注意到有时扫描仪会单独发送 \r,所以请记住这一点。希望能解决困惑。
  • 我绝对不想每次想从扫描仪获取数据时都连接。我会保持连接状态,或者有一个“完成”按钮,或者像你说的那样在调用 onPause 时关闭连接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-26
  • 1970-01-01
  • 2016-06-26
  • 2021-10-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多