Code example for BluetoothAdapter: Communicating over Bluetooth

Methods: cancelDiscovery, getBondedDevices, isDiscovering, isEnabled, startDiscovery

-1
			return; 
		} 
 
		// enable bluetooth 
 
		if (!adapter.isEnabled()) { 
			Log.e(TAG, "Bluetooth is not enabled."); 
			(new AlertDialog.Builder(activity)) 
					.setTitle("ERROR!") 
					.setMessage("Bluetooth is not enabled.  You must enable it.") 
					.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
						public void onClick(DialogInterface dialog, int which) {
							Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
							activity.startActivity(intent);
							activity.finish();
						} 
					}) 
					.setCancelable(false) 
					.show(); 
			return; 
		} 
 
		// register broadcastReceiver 
 
		resetDeviceSet(false); 
 
		broadcastReceiver = new BroadcastReceiver() { 
			@Override 
			public void onReceive(Context context, Intent intent) { 
				String sAction = intent.getAction(); 
				if (BluetoothDevice.ACTION_FOUND.equals(sAction)) { 
					BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
					Log.i(TAG, String.format("%s (%s) is found.", device.getName(), device.getAddress())); 
					if (device.getBondState() != BluetoothDevice.BOND_BONDED) { 
						deviceSet.add(device); 
					} 
				} else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(sAction)) { 
					if (discoveryListener != null) { 
						discoveryListener.onStarted(); 
					} 
				} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(sAction)) { 
					if (discoveryListener != null) { 
						discoveryListener.onFinished(); 
					} 
				} 
			} 
		}; 
		IntentFilter intentFilter = new IntentFilter(); 
		intentFilter.addAction(BluetoothDevice.ACTION_FOUND); 
		intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); 
		intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 
		activity.registerReceiver(broadcastReceiver, intentFilter); 
	} 
 
	public void terminate() { 
		activity.unregisterReceiver(broadcastReceiver);
		broadcastReceiver = null;
	} 
	@Override 
	public void finalize() { 
		if (broadcastReceiver != null) {
			terminate(); 
		} 
	} 
 
	public void resetDeviceSet(boolean doScan) {
		Log.d(TAG, String.format("resetDeviceSet(%s)", doScan ? "true" : "false"));
		deviceSet = new HashSet<BluetoothDevice>(adapter.getBondedDevices());
		if (doScan) {
			if (adapter.isDiscovering()) {
				adapter.cancelDiscovery();
			} 
			adapter.startDiscovery();
		} 
	} 
 
	public Set<BluetoothDevice> getDevices() {
		return deviceSet;