2012-05-24 17 views
11

मेरी परियोजना में मुझे ब्लूटूथ के माध्यम से बारकोड स्कैनर प्रतीक CS3070 का उपयोग करके बारकोड पढ़ना होगा। अर्थात; मुझे ब्लूटूथ के माध्यम से एंड्रॉइड डिवाइस और बारकोड स्कैनर के बीच एक कनेक्शन स्थापित करना होगा। क्या कोई मुझे बता सकता है कि बारकोड रीडर से मूल्य कैसे पढ़ा जाए और संचार के लिए कैसे सेटअप किया जाए? मैंने पहले से ही Bluetooth Developer Guide पढ़ा है, और मैं ब्लूटूथ कीबोर्ड इम्यूलेशन (एचआईडी) मोड में बारकोड रीडर का उपयोग नहीं करना चाहता (मेरे पास कुछ टेक्स्टव्यू है जिसे सॉफ्ट कीबोर्ड और बारकोड रीडर का उपयोग करके भर दिया जा सकता है और मैं फोकस को नियंत्रित नहीं कर सकता)ब्लूटूथ बारकोड स्कैनर से डेटा कैसे पढ़ा जाए सिग्नल सीएस 3070 एंड्रॉइड डिवाइस

मैं एक धागा इस तरह एक पाठक के साथ

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

धन्यवाद

+0

मुझे अपने आवेदन में एक ही कार्यक्षमता की आवश्यकता है, कृपया मुझे बताएं कि क्या आपको इस कार्य से संबंधित कुछ भी उपयोगी लगता है। –

+0

मैं इसे पूरा करने की भी कोशिश कर रहा हूं, अगर आपको समाधान मिल जाए तो कृपया मुझे बताएं। धन्यवाद। –

+0

अब के लिए कोई समाधान नहीं ... – Android84

उत्तर

12

मैं सिर्फ अपनी डिवाइस प्राप्त संवाद करने के लिए और का उपयोग करें जब मैं बनती और उपकरण कनेक्ट यह स्वचालित रूप से करने के लिए डेटा भेजता है वर्तमान में संपादित संपादन टेक्स्ट। एंड्रॉइड का कौन सा संस्करण आप उपयोग कर रहे हैं क्योंकि मैंने इसे आईसीएस और जेबी पर करने की कोशिश की और यह इस तरह से काम किया। मैंने इसे किसी भी पुराने संस्करण में परीक्षण नहीं किया है।

संपादित करें:

मैं जिंजरब्रेड करने के लिए मेरे फोन डाउनग्रेड और पता चला यह उसी तरह काम नहीं करता है, लेकिन मैं एक समाधान है:

यह महत्वपूर्ण है! >> सबसे पहले आपको मैनुअल में बारकोड स्कैन करना होगा जो "सीरियल पोर्ट प्रोफाइल (एसपीपी)" कहता है।

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

यह मेरे कामकाजी कोड का एक अधूरा संस्करण है लेकिन आपको यह समझना चाहिए।
मुझे आशा है कि यह समाधान आपके लिए भी काम करेगा!

+0

+1 क्या आप पूरा कोड जोड़ सकते हैं? मैं एसपीपी मोड में एक ही बारकोड रीडर का उपयोग करने की कोशिश कर रहा हूं। – Baz

+0

मेरे द्वारा प्रदान किया गया कोड पर्याप्त होना चाहिए, आपको बस डिवाइस के पते को जानने की आवश्यकता है। आप इसे जोड़ा जाने के बाद getBondedDevices() को कॉल करके प्राप्त कर सकते हैं। अधिक जानकारी के लिए यहां देखें: http://developer.android.com/guide/topics/connectivity/bluetooth.html –

+0

ठीक है, लेकिन मैं इसका उपयोग कैसे करूं? एक उदाहरण के रूप में: मैं स्कैन किए गए बारकोड को 'टेक्स्ट व्यू' में पढ़ने में सक्षम होना चाहता हूं। मैं यह कैसे करूँ? – Baz

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^