Code example for BluetoothAdapter

Methods: cancelDiscovery, getRemoteDevice

0
	public synchronized boolean connect() { 
		Log.d(TAG, "+ ABOUT TO ATTEMPT CLIENT CONNECT +");
		outStream = null;
 
		if (device == null) {
			device = mBluetoothAdapter.getRemoteDevice(address);
		} 
 
		// We need two things before we can successfully connect 
		// (authentication issues aside): a MAC address, which we 
		// already have, and an RFCOMM channel. 
		// Because RFCOMM channels (aka ports) are limited in 
		// number, Android doesn't allow you to use them directly; 
		// instead you request a RFCOMM mapping based on a service 
		// ID. In our case, we will use the well-known SPP Service 
		// ID. This ID is in UUID (GUID to you Microsofties) 
		// format. Given the UUID, Android will handle the 
		// mapping for you. Generally, this will return RFCOMM 1, 
		// but not always; it depends what other BlueTooth services 
		// are in use on your Android device. 
		/* 
		 * if (device.getBondState() != BluetoothDevice.BOND_BONDED) { 
		 * Log.e(TAG, "Not bonded to " + device.getAddress()); return false; } 
		 */ 
 
		Log.d(TAG, "Device name: " + device.getName());
		try { 
			btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
		} catch (final IOException e) {
			Log.e(TAG, "Socket creation failed.", e);
			return false; 
		} 
 
		// Discovery may be going on, e.g., if you're running a 
		// 'scan for devices' search from your handset's Bluetooth 
		// settings, so we call cancelDiscovery(). It doesn't hurt 
		// to call it, but it might hurt not to... discovery is a 
		// heavyweight process; you don't want it in progress when 
		// a connection attempt is made. 
		mBluetoothAdapter.cancelDiscovery();
 
		// Blocking connect, for a simple client nothing else can 
		// happen until a successful connection is made, so we 
		// don't care if it blocks. 
		try {