2012-05-24 24 views
11

Nel mio progetto devo leggere i codici a barre utilizzando il codice a barre Symbol CS3070 tramite Bluetooth. vale a dire; Devo stabilire una connessione tra dispositivo Android e scanner di codici a barre tramite bluetooth. Qualcuno può dirmi come leggere i valori dal lettore di codici a barre e come impostare la comunicazione? Ho già letto Bluetooth Developer Guide e non voglio utilizzare il lettore di codici a barre in modalità HID (Keyboard Emulation) Bluetooth (ho una vista testuale che può essere riempita usando la tastiera morbida e il lettore di codici a barre e non posso controllare il fuoco)Come leggere i dati dal lettore di codici a barre bluetooth Symbol CS3070 al dispositivo Android

userei un thread come questo per comunicare con un lettore di

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

Grazie

+0

Ho bisogno della stessa funzionalità nella mia domanda, cortesemente ditemi se trovate qualcosa di utile in relazione a questa attività. –

+0

Sto anche cercando di realizzare questo, per favore fatemi sapere se trovate una soluzione. Grazie. –

+0

Nessuna soluzione per ora ... – Android84

risposta

12

ho appena ricevuto il mio dispositivo e quando ho abbinato e collegato il dispositivo invia automaticamente i dati al attualmente incentrato su EditText. Quale versione di Android stai utilizzando perché l'ho provata su ICS e JB e ha funzionato in questo modo. Non l'ho provato in nessuna versione precedente.

Edit:

ho declassato il mio telefono per Gingerbread e ha scoperto che non funziona allo stesso modo, ma ho una soluzione:

Questo è importante! >> Per prima cosa è necessario eseguire la scansione del codice a barre nel manuale con la dicitura "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); 
    } 
} 

Questa è una versione incompleta del mio codice di lavoro, ma si dovrebbe ottenere il succo.
Spero che questa soluzione funzioni anche per voi!

+0

+1 Sarebbe possibile aggiungere il codice completo? Sto cercando di utilizzare lo stesso lettore di codici a barre in modalità SPP. – Baz

+0

Il codice che ho fornito dovrebbe essere sufficiente, è sufficiente conoscere l'indirizzo del dispositivo. Puoi ottenerlo chiamando getBondedDevices() dopo essere stato accoppiato. Guarda qui per maggiori informazioni: http://developer.android.com/guide/topics/connectivity/bluetooth.html –

+0

Ok, ma come si usa? Ad esempio: voglio essere in grado di leggere il codice a barre scansionato in un 'TextView'. Come faccio questo? – Baz

Problemi correlati