2 * Copyright (C) 2016 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.android.settings.bluetooth;
18 import android.bluetooth.BluetoothClass;
19 import android.bluetooth.BluetoothDevice;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.text.Editable;
23 import android.util.Log;
24 import android.widget.CompoundButton;
25 import android.widget.CompoundButton.OnCheckedChangeListener;
26 import com.android.settings.R;
27 import com.android.settings.bluetooth.BluetoothPairingDialogFragment.BluetoothPairingDialogListener;
28 import com.android.settingslib.bluetooth.LocalBluetoothManager;
29 import com.android.settingslib.bluetooth.LocalBluetoothProfile;
30 import java.util.Locale;
33 * A controller used by {@link BluetoothPairingDialog} to manage connection state while we try to
34 * pair with a bluetooth device. It includes methods that allow the
35 * {@link BluetoothPairingDialogFragment} to interrogate the current state as well.
37 public class BluetoothPairingController implements OnCheckedChangeListener,
38 BluetoothPairingDialogListener {
40 private static final String TAG = "BTPairingController";
42 // Different types of dialogs we can map to
43 public static final int INVALID_DIALOG_TYPE = -1;
44 public static final int USER_ENTRY_DIALOG = 0;
45 public static final int CONFIRMATION_DIALOG = 1;
46 public static final int DISPLAY_PASSKEY_DIALOG = 2;
48 private static final int BLUETOOTH_PIN_MAX_LENGTH = 16;
49 private static final int BLUETOOTH_PASSKEY_MAX_LENGTH = 6;
51 // Bluetooth dependencies for the connection we are trying to establish
52 private LocalBluetoothManager mBluetoothManager;
53 private BluetoothDevice mDevice;
55 private String mUserInput;
56 private String mPasskeyFormatted;
58 private String mDeviceName;
59 private LocalBluetoothProfile mPbapClientProfile;
62 * Creates an instance of a BluetoothPairingController.
64 * @param intent - must contain {@link BluetoothDevice#EXTRA_PAIRING_VARIANT}, {@link
65 * BluetoothDevice#EXTRA_PAIRING_KEY}, and {@link BluetoothDevice#EXTRA_DEVICE}. Missing extra
66 * will lead to undefined behavior.
68 public BluetoothPairingController(Intent intent, Context context) {
69 mBluetoothManager = Utils.getLocalBtManager(context);
70 mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
73 if (mBluetoothManager == null) {
74 throw new IllegalStateException("Could not obtain LocalBluetoothManager");
75 } else if (mDevice == null) {
76 throw new IllegalStateException("Could not find BluetoothDevice");
79 mType = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR);
80 mPasskey = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_KEY, BluetoothDevice.ERROR);
81 mDeviceName = mBluetoothManager.getCachedDeviceManager().getName(mDevice);
82 mPbapClientProfile = mBluetoothManager.getProfileManager().getPbapClientProfile();
83 mPasskeyFormatted = formatKey(mPasskey);
88 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
90 mDevice.setPhonebookAccessPermission(BluetoothDevice.ACCESS_ALLOWED);
92 mDevice.setPhonebookAccessPermission(BluetoothDevice.ACCESS_REJECTED);
97 public void onDialogPositiveClick(BluetoothPairingDialogFragment dialog) {
98 if (getDialogType() == USER_ENTRY_DIALOG) {
106 public void onDialogNegativeClick(BluetoothPairingDialogFragment dialog) {
111 * A method for querying which bluetooth pairing dialog fragment variant this device requires.
113 * @return - The dialog view variant needed for this device.
115 public int getDialogType() {
117 case BluetoothDevice.PAIRING_VARIANT_PIN:
118 case BluetoothDevice.PAIRING_VARIANT_PIN_16_DIGITS:
119 case BluetoothDevice.PAIRING_VARIANT_PASSKEY:
120 return USER_ENTRY_DIALOG;
122 case BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION:
123 case BluetoothDevice.PAIRING_VARIANT_CONSENT:
124 case BluetoothDevice.PAIRING_VARIANT_OOB_CONSENT:
125 return CONFIRMATION_DIALOG;
127 case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY:
128 case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN:
129 return DISPLAY_PASSKEY_DIALOG;
132 return INVALID_DIALOG_TYPE;
137 * @return - A string containing the name provided by the device.
139 public String getDeviceName() {
144 * A method for querying if the bluetooth device has a profile already set up on this device.
146 * @return - A boolean indicating if the device has previous knowledge of a profile for this
149 public boolean isProfileReady() {
150 return mPbapClientProfile != null && mPbapClientProfile.isProfileReady();
154 * A method for querying if the bluetooth device has access to contacts on the device.
156 * @return - A boolean indicating if the bluetooth device has permission to access the device
159 public boolean getContactSharingState() {
160 switch (mDevice.getPhonebookAccessPermission()) {
161 case BluetoothDevice.ACCESS_ALLOWED:
163 case BluetoothDevice.ACCESS_REJECTED:
166 if (mDevice.getBluetoothClass().getDeviceClass()
167 == BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE) {
175 * Update Phone book permission
178 public void setContactSharingState() {
179 if ((mDevice.getPhonebookAccessPermission() != BluetoothDevice.ACCESS_ALLOWED)
180 && (mDevice.getPhonebookAccessPermission() != BluetoothDevice.ACCESS_REJECTED)) {
181 if (mDevice.getBluetoothClass().getDeviceClass()
182 == BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE) {
183 onCheckedChanged(null, true);
185 onCheckedChanged(null, false);
191 * A method for querying if the provided editable is a valid passkey/pin format for this device.
193 * @param s - The passkey/pin
194 * @return - A boolean indicating if the passkey/pin is of the correct format.
196 public boolean isPasskeyValid(Editable s) {
197 boolean requires16Digits = mType == BluetoothDevice.PAIRING_VARIANT_PIN_16_DIGITS;
198 return s.length() >= 16 && requires16Digits || s.length() > 0 && !requires16Digits;
202 * A method for querying what message should be shown to the user as additional text in the
203 * dialog for this device. Returns -1 to indicate a device type that does not use this message.
205 * @return - The message ID to show the user.
207 public int getDeviceVariantMessageId() {
209 case BluetoothDevice.PAIRING_VARIANT_PIN_16_DIGITS:
210 case BluetoothDevice.PAIRING_VARIANT_PIN:
211 return R.string.bluetooth_enter_pin_other_device;
213 case BluetoothDevice.PAIRING_VARIANT_PASSKEY:
214 return R.string.bluetooth_enter_passkey_other_device;
217 return INVALID_DIALOG_TYPE;
222 * A method for querying what message hint should be shown to the user as additional text in the
223 * dialog for this device. Returns -1 to indicate a device type that does not use this message.
225 * @return - The message ID to show the user.
227 public int getDeviceVariantMessageHintId() {
229 case BluetoothDevice.PAIRING_VARIANT_PIN_16_DIGITS:
230 return R.string.bluetooth_pin_values_hint_16_digits;
232 case BluetoothDevice.PAIRING_VARIANT_PIN:
233 case BluetoothDevice.PAIRING_VARIANT_PASSKEY:
234 return R.string.bluetooth_pin_values_hint;
237 return INVALID_DIALOG_TYPE;
242 * A method for querying the maximum passkey/pin length for this device.
244 * @return - An int indicating the maximum length
246 public int getDeviceMaxPasskeyLength() {
248 case BluetoothDevice.PAIRING_VARIANT_PIN_16_DIGITS:
249 case BluetoothDevice.PAIRING_VARIANT_PIN:
250 return BLUETOOTH_PIN_MAX_LENGTH;
252 case BluetoothDevice.PAIRING_VARIANT_PASSKEY:
253 return BLUETOOTH_PASSKEY_MAX_LENGTH;
262 * A method for querying if the device uses an alphanumeric passkey.
264 * @return - a boolean indicating if the passkey can be alphanumeric.
266 public boolean pairingCodeIsAlphanumeric() {
268 case BluetoothDevice.PAIRING_VARIANT_PASSKEY:
277 * A method used by the dialogfragment to notify the controller that the dialog has been
278 * displayed for bluetooth device types that just care about it being displayed.
280 protected void notifyDialogDisplayed() {
281 // send an OK to the framework, indicating that the dialog has been displayed.
282 if (mType == BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY) {
283 mDevice.setPairingConfirmation(true);
284 } else if (mType == BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN) {
285 byte[] pinBytes = BluetoothDevice.convertPinToBytes(mPasskeyFormatted);
286 mDevice.setPin(pinBytes);
291 * A method for querying if this bluetooth device type has a key it would like displayed
294 * @return - A boolean indicating if a key exists which should be displayed to the user.
296 public boolean isDisplayPairingKeyVariant() {
298 case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY:
299 case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN:
300 case BluetoothDevice.PAIRING_VARIANT_OOB_CONSENT:
308 * A method for querying if this bluetooth device type has other content it would like displayed
311 * @return - A boolean indicating if content exists which should be displayed to the user.
313 public boolean hasPairingContent() {
315 case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY:
316 case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN:
317 case BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION:
326 * A method for obtaining any additional content this bluetooth device has for displaying to the
329 * @return - A string containing the additional content, null if none exists.
330 * @see {@link BluetoothPairingController#hasPairingContent()}
332 public String getPairingContent() {
333 if (hasPairingContent()) {
334 return mPasskeyFormatted;
341 * A method that exists to allow the fragment to update the controller with input the user has
342 * provided in the fragment.
344 * @param input - A string containing the user input.
346 protected void updateUserInput(String input) {
351 * Returns the provided passkey in a format that this device expects. Only works for numeric
354 * @param passkey - An integer containing the passkey to format.
355 * @return - A string containing the formatted passkey/pin
357 private String formatKey(int passkey) {
359 case BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION:
360 case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY:
361 return String.format(Locale.US, "%06d", passkey);
363 case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN:
364 return String.format("%04d", passkey);
372 * handles the necessary communication with the bluetooth device to establish a successful
375 * @param passkey - The passkey we will attempt to pair to the device with.
377 private void onPair(String passkey) {
378 Log.d(TAG, "Pairing dialog accepted");
380 case BluetoothDevice.PAIRING_VARIANT_PIN:
381 case BluetoothDevice.PAIRING_VARIANT_PIN_16_DIGITS:
382 byte[] pinBytes = BluetoothDevice.convertPinToBytes(passkey);
383 if (pinBytes == null) {
386 mDevice.setPin(pinBytes);
389 case BluetoothDevice.PAIRING_VARIANT_PASSKEY:
390 int pass = Integer.parseInt(passkey);
391 mDevice.setPasskey(pass);
394 case BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION:
395 case BluetoothDevice.PAIRING_VARIANT_CONSENT:
396 mDevice.setPairingConfirmation(true);
399 case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY:
400 case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN:
404 case BluetoothDevice.PAIRING_VARIANT_OOB_CONSENT:
405 mDevice.setRemoteOutOfBandData();
409 Log.e(TAG, "Incorrect pairing type received");
414 * A method for properly ending communication with the bluetooth device. Will be called by the
415 * {@link BluetoothPairingDialogFragment} when it is dismissed.
417 public void onCancel() {
418 Log.d(TAG, "Pairing dialog canceled");
419 mDevice.cancelPairingUserInput();
423 * A method for checking if this device is equal to another device.
425 * @param device - The other device being compared to this device.
426 * @return - A boolean indicating if the devices were equal.
428 public boolean deviceEquals(BluetoothDevice device) {
429 return mDevice == device;