Advertisement
moonlightcheese

Untitled

Jul 26th, 2011
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.52 KB | None | 0 0
  1. /**
  2.  * This thread runs during a connection with a remote device.
  3.  * It handles all incoming and outgoing transmissions.
  4.  */
  5. private class ConnectedThread extends Thread {
  6.     private final BluetoothSocket mmSocket;
  7.     private final LineNumberReader mmInStream;
  8.     private final OutputStream mmOutStream;
  9.    
  10.     public ConnectedThread(BluetoothSocket socket) {
  11.         Log.d(LOG_TAG, "create ConnectedThread");
  12.         mmSocket = socket;
  13.         LineNumberReader tmpIn = null;
  14.         OutputStream tmpOut = null;
  15.        
  16.         // Get the BluetoothSocket input and output streams
  17.         try {
  18.             tmpIn = new LineNumberReader(new InputStreamReader(socket.getInputStream()));
  19.             tmpOut = socket.getOutputStream();
  20.         } catch (IOException e) {
  21.             Log.e(LOG_TAG, "temp sockets not created", e);
  22.         }
  23.        
  24.         mmInStream = tmpIn;
  25.         mmOutStream = tmpOut;
  26.     }
  27.    
  28.     public void run() {
  29.         Log.i(LOG_TAG, "BEGIN mConnectedThread");
  30.        
  31.         while(true) {
  32.             try {
  33.                 String curScan = mmInStream.readLine();
  34.                 if (D) Log.i(LOG_TAG, "scan read in service: "+curScan);
  35.                
  36.                 //////////////////////////////////////////////
  37.                 //TODO: send the read intent from here
  38.                 //////////////////////////////////////////////
  39.                 Intent i = new Intent(ScannerService.ACTION_READ_SCANNER);
  40.                 i.putExtra("scannerRead", curScan);
  41.                 sendBroadcast(i);
  42.                
  43.             } catch(IOException e) {
  44.                 connectionLost();
  45.                 break;
  46.             }
  47.         }
  48.     }
  49.    
  50.     public void cancel() {
  51.         try {
  52.             mmSocket.close();
  53.         } catch (IOException e) {
  54.             Log.e(LOG_TAG, "close() of connect socket failed", e);
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement