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 * A method for querying if the provided editable is a valid passkey/pin format for this device.
177 * @param s - The passkey/pin
178 * @return - A boolean indicating if the passkey/pin is of the correct format.
180 public boolean isPasskeyValid(Editable s) {
181 boolean requires16Digits = mType == BluetoothDevice.PAIRING_VARIANT_PIN_16_DIGITS;
182 return s.length() >= 16 && requires16Digits || s.length() > 0 && !requires16Digits;
186 * A method for querying what message should be shown to the user as additional text in the
187 * dialog for this device. Returns -1 to indicate a device type that does not use this message.
189 * @return - The message ID to show the user.
191 public int getDeviceVariantMessageId() {
193 case BluetoothDevice.PAIRING_VARIANT_PIN_16_DIGITS:
194 case BluetoothDevice.PAIRING_VARIANT_PIN:
195 return R.string.bluetooth_enter_pin_other_device;
197 case BluetoothDevice.PAIRING_VARIANT_PASSKEY:
198 return R.string.bluetooth_enter_passkey_other_device;
201 return INVALID_DIALOG_TYPE;
206 * A method for querying what message hint should be shown to the user as additional text in the
207 * dialog for this device. Returns -1 to indicate a device type that does not use this message.
209 * @return - The message ID to show the user.
211 public int getDeviceVariantMessageHintId() {
213 case BluetoothDevice.PAIRING_VARIANT_PIN_16_DIGITS:
214 return R.string.bluetooth_pin_values_hint_16_digits;
216 case BluetoothDevice.PAIRING_VARIANT_PIN:
217 case BluetoothDevice.PAIRING_VARIANT_PASSKEY:
218 return R.string.bluetooth_pin_values_hint;
221 return INVALID_DIALOG_TYPE;
226 * A method for querying the maximum passkey/pin length for this device.
228 * @return - An int indicating the maximum length
230 public int getDeviceMaxPasskeyLength() {
232 case BluetoothDevice.PAIRING_VARIANT_PIN_16_DIGITS:
233 case BluetoothDevice.PAIRING_VARIANT_PIN:
234 return BLUETOOTH_PIN_MAX_LENGTH;
236 case BluetoothDevice.PAIRING_VARIANT_PASSKEY:
237 return BLUETOOTH_PASSKEY_MAX_LENGTH;
246 * A method for querying if the device uses an alphanumeric passkey.
248 * @return - a boolean indicating if the passkey can be alphanumeric.
250 public boolean pairingCodeIsAlphanumeric() {
252 case BluetoothDevice.PAIRING_VARIANT_PASSKEY:
261 * A method used by the dialogfragment to notify the controller that the dialog has been
262 * displayed for bluetooth device types that just care about it being displayed.
264 protected void notifyDialogDisplayed() {
265 // send an OK to the framework, indicating that the dialog has been displayed.
266 if (mType == BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY) {
267 mDevice.setPairingConfirmation(true);
268 } else if (mType == BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN) {
269 byte[] pinBytes = BluetoothDevice.convertPinToBytes(mPasskeyFormatted);
270 mDevice.setPin(pinBytes);
275 * A method for querying if this bluetooth device type has a key it would like displayed
278 * @return - A boolean indicating if a key exists which should be displayed to the user.
280 public boolean isDisplayPairingKeyVariant() {
282 case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY:
283 case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN:
284 case BluetoothDevice.PAIRING_VARIANT_OOB_CONSENT:
292 * A method for querying if this bluetooth device type has other content it would like displayed
295 * @return - A boolean indicating if content exists which should be displayed to the user.
297 public boolean hasPairingContent() {
299 case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY:
300 case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN:
301 case BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION:
310 * A method for obtaining any additional content this bluetooth device has for displaying to the
313 * @return - A string containing the additional content, null if none exists.
314 * @see {@link BluetoothPairingController#hasPairingContent()}
316 public String getPairingContent() {
317 if (hasPairingContent()) {
318 return mPasskeyFormatted;
325 * A method that exists to allow the fragment to update the controller with input the user has
326 * provided in the fragment.
328 * @param input - A string containing the user input.
330 protected void updateUserInput(String input) {
335 * Returns the provided passkey in a format that this device expects. Only works for numeric
338 * @param passkey - An integer containing the passkey to format.
339 * @return - A string containing the formatted passkey/pin
341 private String formatKey(int passkey) {
343 case BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION:
344 case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY:
345 return String.format(Locale.US, "%06d", passkey);
347 case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN:
348 return String.format("%04d", passkey);
356 * handles the necessary communication with the bluetooth device to establish a successful
359 * @param passkey - The passkey we will attempt to pair to the device with.
361 private void onPair(String passkey) {
362 Log.d(TAG, "Pairing dialog accepted");
364 case BluetoothDevice.PAIRING_VARIANT_PIN:
365 case BluetoothDevice.PAIRING_VARIANT_PIN_16_DIGITS:
366 byte[] pinBytes = BluetoothDevice.convertPinToBytes(passkey);
367 if (pinBytes == null) {
370 mDevice.setPin(pinBytes);
373 case BluetoothDevice.PAIRING_VARIANT_PASSKEY:
374 int pass = Integer.parseInt(passkey);
375 mDevice.setPasskey(pass);
378 case BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION:
379 case BluetoothDevice.PAIRING_VARIANT_CONSENT:
380 mDevice.setPairingConfirmation(true);
383 case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY:
384 case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN:
388 case BluetoothDevice.PAIRING_VARIANT_OOB_CONSENT:
389 mDevice.setRemoteOutOfBandData();
393 Log.e(TAG, "Incorrect pairing type received");
398 * A method for properly ending communication with the bluetooth device. Will be called by the
399 * {@link BluetoothPairingDialogFragment} when it is dismissed.
401 public void onCancel() {
402 Log.d(TAG, "Pairing dialog canceled");
403 mDevice.cancelPairingUserInput();
407 * A method for checking if this device is equal to another device.
409 * @param device - The other device being compared to this device.
410 * @return - A boolean indicating if the devices were equal.
412 public boolean deviceEquals(BluetoothDevice device) {
413 return mDevice == device;