1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 17 package com.android.tv.settings.accessories; 18 19 import android.bluetooth.BluetoothDevice; 20 import android.content.BroadcastReceiver; 21 import android.content.Context; 22 import android.content.Intent; 23 24 /** 25 * BluetoothPairingRequest is a receiver for any Bluetooth pairing request. It 26 * starts the Bluetooth Pairing activity, displaying the PIN, the passkey or a 27 * confirmation entry dialog. 28 */ 29 public final class BluetoothPairingRequest extends BroadcastReceiver { 30 31 @Override 32 public void onReceive(Context context, Intent intent) { 33 String action = intent.getAction(); 34 if (!action.equals(BluetoothDevice.ACTION_PAIRING_REQUEST)) { 35 return; 36 } 37 38 // convert broadcast intent into activity intent (same action string) 39 BluetoothDevice device = 40 intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 41 int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, 42 BluetoothDevice.ERROR); 43 Intent pairingIntent = new Intent(); 44 pairingIntent.setClass(context, BluetoothPairingDialog.class); 45 pairingIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, device); 46 pairingIntent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, type); 47 if (type == BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION || 48 type == BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY || 49 type == BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN) { 50 int pairingKey = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_KEY, 51 BluetoothDevice.ERROR); 52 pairingIntent.putExtra(BluetoothDevice.EXTRA_PAIRING_KEY, pairingKey); 53 } 54 pairingIntent.setAction(BluetoothDevice.ACTION_PAIRING_REQUEST); 55 pairingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 56 57 // In Canvas, always start the pairing activity when we get the pairing broadcast, 58 // as opposed to displaying a notification that will start the pairing activity. 59 context.startActivity(pairingIntent); 60 } 61 } 62