question: public void enable ( BluetoothAdapter adapter ) { int mask = ( BluetoothReceiver . STATE_TURNING_ON_FLAG | BluetoothReceiver . STATE_ON_FLAG | BluetoothReceiver . SCAN_MODE_CONNECTABLE_FLAG ) ; long start = - 1 ; BluetoothReceiver receiver = getBluetoothReceiver ( mask ) ; int state = adapter . getState ( ) ; switch ( state ) { case BluetoothAdapter . STATE_ON : assertTrue ( adapter . isEnabled ( ) ) ; removeReceiver ( receiver ) ; return ; case BluetoothAdapter . STATE_TURNING_ON : assertFalse ( adapter . isEnabled ( ) ) ; mask = 0 ; break ; case BluetoothAdapter . STATE_OFF : assertFalse ( adapter . isEnabled ( ) ) ; start = System . currentTimeMillis ( ) ; assertTrue ( adapter . enable ( ) ) ; break ; case BluetoothAdapter . STATE_TURNING_OFF : start = System . currentTimeMillis ( ) ; assertTrue ( adapter . enable ( ) ) ; break ; default : removeReceiver ( receiver ) ; fail ( String . format ( "enable() invalid state: state=%d" , state ) ) ; } long s = System . currentTimeMillis ( ) ; while ( System . currentTimeMillis ( ) - s < ENABLE_DISABLE_TIMEOUT ) { state = adapter . getState ( ) ; if ( state == BluetoothAdapter . STATE_ON && ( receiver . getFiredFlags ( ) & mask ) == mask ) { assertTrue ( adapter . isEnabled ( ) ) ; long finish = receiver . getCompletedTime ( ) ; if ( start != - 1 && finish != - 1 ) { writeOutput ( String . format ( "enable() completed in %d ms" , ( finish - start ) ) ) ; } else { writeOutput ( "enable() completed" ) ; } removeReceiver ( receiver ) ; return ; } sleep ( POLL_TIME ) ; } int firedFlags = receiver . getFiredFlags ( ) ; removeReceiver ( receiver ) ; fail ( String . format ( "enable() timeout: state=%d (expected %d), flags=0x%x (expected 0x%x)" , state , BluetoothAdapter . STATE_ON , firedFlags , mask ) ) ; }
target:  Enables Bluetooth and checks to make sure that Bluetooth was turned on and that the correct actions were broadcast.