Code example for BluetoothSocket

Methods: close, connect, getInputStream, getOutputStream

0
	        mBluetoothAdapter.cancelDiscovery();
	  
	        try { 
	            // Connect the device through the socket. This will block 
	            // until it succeeds or throws an exception 
	            mmSocket.connect();
	        } catch (IOException connectException) {
	            // Unable to connect; close the socket and get out 
	            try { 
	                mmSocket.close();
	            } catch (IOException closeException) { }
	            return; 
	        } 
	  
	        socket_thread = new ConnectedThread(mmSocket);
	        socket_thread.start();
	         
	        Message m = new Message();
	        m.what = 1;
	        mHandler.sendMessage(m);
	    } 
	  
	    /** Will cancel an in-progress connection, and close the socket */ 
	    public void cancel() { 
	        try { 
	            mmSocket.close();
	        } catch (IOException e) { }
	    } 
	} 
 
	// Create a BroadcastReceiver for ACTION_FOUND 
	private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
	    public void onReceive(Context context, Intent intent) { 
	        String action = intent.getAction(); 
	        if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
	            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
	            if(device.getAddress().equals(mMAC)){ 
		    		hostDevice = device; 
		    		mBluetoothAdapter.cancelDiscovery(); 
		    		 
		    		Message m = new Message(); 
			        m.what = 2; 
			        mHandler.sendMessage(m); 
			         
		    		Log.e("Jason", "FOUND by scan"); 
		    	} 
	            Log.e("Jason", "s "+device.getAddress()); 
	        } 
	    } 
	}; 
	 
	private class ConnectedThread extends Thread {
	    private final BluetoothSocket mmSocket;
	    private final InputStream mmInStream;
	    private final OutputStream mmOutStream;
	  
	    public ConnectedThread(BluetoothSocket socket) {
	        mmSocket = socket;
	        InputStream tmpIn = null;
	        OutputStream tmpOut = null;
	  
	        // Get the input and output streams, using temp objects because 
	        // member streams are final 
	        try { 
	            tmpIn = socket.getInputStream();
	            tmpOut = socket.getOutputStream();
	        } catch (IOException e) { }
	  
	        mmInStream = tmpIn;
	        mmOutStream = tmpOut;
	    }