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; 
	        } 
	  
	        // Do work to manage the connection (in a separate thread) 
	        //manageConnectedSocket(mmSocket); 
	        connectedThread = new ConnectedThread(mmSocket);
	        Thread t = new Thread(connectedThread);
	        t.start();
	    } 
	  
	    /** Will cancel an in-progress connection, and close the socket */ 
	    public void cancel() { 
	        try { 
	            mmSocket.close();
	        } catch (IOException e) { }
	    } 
	} 
	 
	@Override 
	protected void onStop() { 
		// TODO Auto-generated method stub 
		if(connectThread != null){
			connectThread.cancel();
		} 
		if(connectedThread != null){
			connectedThread.cancel();
		} 
		super.onStop(); 
	} 
	 
	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;
	    }