Code example for BluetoothAdapter: Communicating over Bluetooth

Methods: getState, isEnabled, startDiscovery

0
 
	@Override 
	public void run() { 
	    Log.d(TAG, "Run Bluetooth discovery thread");
	    if (scanEnabled) {
		if (btAdapter.isEnabled()) {
		    // start discovery 
		    deviceArray = new Vector<Map<BluetoothDevice, Short>>();
		    context.registerReceiver(btReceiver, new IntentFilter(
			    BluetoothDevice.ACTION_FOUND));
		    context.registerReceiver(btReceiver, new IntentFilter(
			    BluetoothAdapter.ACTION_DISCOVERY_FINISHED));
 
		    Log.i(TAG, "Starting Bluetooth discovery");
		    btAdapter.startDiscovery();
		} else if (btAdapter.getState() == BluetoothAdapter.STATE_TURNING_ON) {
		    // listen for the adapter state to change to STATE_ON 
		    context.registerReceiver(btReceiver, new IntentFilter(
			    BluetoothAdapter.ACTION_STATE_CHANGED));
		} else { 
		    // ask user for permission to start bluetooth