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