如何读取数据蓝牙吧code扫描仪符号CS3070到Android设备 [英] How to read data from bluetooth barcode scanner Symbol CS3070 to Android Device

查看:404
本文介绍了如何读取数据蓝牙吧code扫描仪符号CS3070到Android设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目,我有通过蓝牙使用吧code扫描仪符号CS3070阅读吧codeS。即;我必须建立一个通过蓝牙与Android设备和酒吧code扫描仪的连接。任何一个可以告诉我如何读吧code阅读器和值如何设置的沟通?我已经阅读蓝牙开发者指南,我不想用吧$蓝牙键盘仿真(HID)模式c $ C读卡器(我TextView的一些可以使用软键盘和酒吧code Reader进行填补,我无法控制的重点)

In my project i have to read barcodes using barcode scanner Symbol CS3070 through bluetooth. i.e; i have to establish a connection between android device and barcode scanner through bluetooth. Can any one tell me how to read values from barcode reader and how to setup for communication? I've already read the Bluetooth Developer Guide, and I don't want to use Barcode Reader in Bluetooth Keyboard Emulation (HID) mode (I've some textview that can be filled using soft keyboard and Barcode Reader and I can't control the focus)

我会使用一个线程像这样与读者进行交流。

I'd use a thread like this to communicate with a reader

    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) { }
    }
}

感谢

推荐答案

我刚刚收到我的设备,当我配对并连接的设备,它会自动发送到当前聚焦的EditText数据。你使用的是什么版本的Andr​​oid,因为我想它的ICS和JB和它的工作这种方式。 我没有在任何早期版本进行了测试。

I just received my device and when I paired and connected the device it automatically sends the data to the currently focused EditText. What version of Android are you using because I tried it on ICS and JB and it worked this way. I have not tested it in any earlier versions.

编辑:

我降级我的手机姜饼,发现它不工作方式相同,但我有一个解决办法:

I downgraded my phone to Gingerbread and found out it does not work the same way but I have a solution:

这是很重要的! >>首先,你必须扫描吧code手册中,说:串行端口配置文件(SPP)。

This is important! >> First you must scan the barcode in the manual that says "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);
    }
}

这是一个不完整的版本,我的工作code,但你应该得到的要点。
我希望这个解决方案适用于你的!

This is an incomplete version of my working code but you should get the gist.
I hope this solution works for you as well!

这篇关于如何读取数据蓝牙吧code扫描仪符号CS3070到Android设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆