It prompts the user to turn on Bluetooth and asks for confirmation. If the user agrees, the sub¬Activity will close and return to the calling Activity when the Bluetooth Adapter has turned on (or has encountered an error). If the user selects no, the sub-Activity will close and return immediately. Use the result code parameter returned in the onActivityResult handler to determine the success of this operation, as shown in Listing 16-2. code snippet PA4AD_Ch16_Bluetooth/src/BluetoothActivity.javaEnabling and disabling the Bluetooth Adapter are somewhat time-consuming, asynchronous opera¬tions. Rather than polling the Bluetooth Adapter, your application should register a Broadcast Receiver that listens for action_state_changed. The Broadcast Intent will include two extras, extra_state and extra_previous_state, which indicate the current and previous Bluetooth Adapter states, respectively:BroadcastReceiver bluetoothState = new BroadcastReceiver() {©Overridepublic void onReceive(Context context, Intent intent) {String prevStateExtra = BluetoothAdapter.EXTRA_PREVI0US_STATE;String stateExtra = BluetoothAdapter.EXTRA_STATE;int state = intent.getIntExtra(stateExtra, -1);int previousState = intent.getIntExtra(prevStateExtra, -1);String tt = ""; switch (state) {case (BluetoothAdapter.STATE_TURNING_0N) : tt = "Bluetooth turning on"; break; case (BluetoothAdapter.STATE_0N) :
đang được dịch, vui lòng đợi..