大佬教程收集整理的这篇文章主要介绍了如何从蓝牙条码扫描器读取数据符号CS3070到Android设备,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我会使用这样的线程与读者通信
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,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) { } } }
谢谢
编辑:
这个很重要! >>首先,您必须扫描手册中的“串行端口配置文件(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,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); } }
以上是大佬教程为你收集整理的如何从蓝牙条码扫描器读取数据符号CS3070到Android设备全部内容,希望文章能够帮你解决如何从蓝牙条码扫描器读取数据符号CS3070到Android设备所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。