2012-05-24 9 views
11

私のプロジェクトでは、バーコードスキャナSymbol CS3070を使用してバーコードを読み取る必要があります。すなわち、私はアンドロイドデバイスとバーコードスキャナの間の接続を確立する必要がありますブルートゥース。バーコードリーダーから値を読み取る方法と、通信をセットアップする方法を教えてもらえますか?私はすでにBluetooth Developer Guideを読んでおり、Bluetoothキーボードエミュレーション(HID)モードでバーコードリーダーを使用したくない(ソフトキーボードとバーコードリーダーを使って入力できるテキストビューがいくつかあり、フォーカスを制御できない)ブルートゥースバーコードスキャナからデータを読み取る方法Symbol 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, 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

を伝えるために、このようなスレッドを使用したいです現在フォーカスのあるEditTextです。私がICSとJBで試したので、あなたはどのAndroidのバージョンを使用していますか? 以前のバージョンではテストしていません。

編集:

私はジンジャーブレッドに自分の携帯電話を格下げし、それは同じように動作しませんがわかったが、私は解決策を持っている:

これは重要です! >>まず、「シリアルポートプロファイル(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); 
    } 
} 

これは私の作業コードの不完全なバージョンですが、あなたはその要点を取得する必要があります。
私はこのソリューションがうまくいくことを願っています!

+0

+1完全なコードを追加できますか? SPPモードで同じバーコードリーダーを使用しようとしています。 – Baz

+0

私が提供したコードで十分です。あなたはデバイスのアドレスを知る必要があります。ペアになった後にgetBondedDevices()を呼び出すことで取得できます。詳細についてはこちらをご覧ください:http://developer.android.com/guide/topics/connectivity/bluetooth.html –

+0

いいですが、どうすれば使用できますか?例として、スキャンされたバーコードを「TextView」に読み込むことができるようにしたい。どのように私はこれを行うには? – Baz

関連する問題