BluetoothPairingRequest.java revision 5469ff8b3f9966c6f141078b37da5f178b7825e4
1afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project/* 2afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * Copyright (C) 2008 The Android Open Source Project 3afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * 4afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License"); 5afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * you may not use this file except in compliance with the License. 6afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * You may obtain a copy of the License at 7afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * 8afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * http://www.apache.org/licenses/LICENSE-2.0 9afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * 10afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * Unless required by applicable law or agreed to in writing, software 11afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS, 12afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * See the License for the specific language governing permissions and 14afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * limitations under the License. 15afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project */ 16afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project 17afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectpackage com.android.settings.bluetooth; 18afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project 19afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport com.android.settings.R; 20afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project 21afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.app.Notification; 22afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.app.NotificationManager; 23afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.app.PendingIntent; 24afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.bluetooth.BluetoothDevice; 25afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.content.BroadcastReceiver; 26afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.content.Context; 27afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.content.Intent; 28afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.content.res.Resources; 29afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.text.TextUtils; 30afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project 31afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project/** 323a76bcaa83c15f96832f934e67e6f0190e72a3dcJaikumar Ganesh * BluetoothPairingRequest is a receiver for any Bluetooth pairing request. It 333a76bcaa83c15f96832f934e67e6f0190e72a3dcJaikumar Ganesh * checks if the Bluetooth Settings is currently visible and brings up the PIN, the passkey or a 343a76bcaa83c15f96832f934e67e6f0190e72a3dcJaikumar Ganesh * confirmation entry dialog. Otherwise it puts a Notification in the status bar, which can 353a76bcaa83c15f96832f934e67e6f0190e72a3dcJaikumar Ganesh * be clicked to bring up the Pairing entry dialog. 36afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project */ 373a76bcaa83c15f96832f934e67e6f0190e72a3dcJaikumar Ganeshpublic class BluetoothPairingRequest extends BroadcastReceiver { 38afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project 39afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project public static final int NOTIFICATION_ID = android.R.drawable.stat_sys_data_bluetooth; 403a76bcaa83c15f96832f934e67e6f0190e72a3dcJaikumar Ganesh 41afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project @Override 42afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project public void onReceive(Context context, Intent intent) { 43afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project String action = intent.getAction(); 4416cc86315d7a8e1f6a0f3083d0a810a7cb097832Nick Pelly if (action.equals(BluetoothDevice.ACTION_PAIRING_REQUEST)) { 45afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project 463a76bcaa83c15f96832f934e67e6f0190e72a3dcJaikumar Ganesh LocalBluetoothManager localManager = LocalBluetoothManager.getInstance(context); 473a76bcaa83c15f96832f934e67e6f0190e72a3dcJaikumar Ganesh 48d63c0112251ab4e4e977545368dd703d875012a4Nick Pelly BluetoothDevice device = 4916cc86315d7a8e1f6a0f3083d0a810a7cb097832Nick Pelly intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 5016cc86315d7a8e1f6a0f3083d0a810a7cb097832Nick Pelly int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, 5116cc86315d7a8e1f6a0f3083d0a810a7cb097832Nick Pelly BluetoothDevice.ERROR); 523a76bcaa83c15f96832f934e67e6f0190e72a3dcJaikumar Ganesh Intent pairingIntent = new Intent(); 533a76bcaa83c15f96832f934e67e6f0190e72a3dcJaikumar Ganesh pairingIntent.setClass(context, BluetoothPairingDialog.class); 5416cc86315d7a8e1f6a0f3083d0a810a7cb097832Nick Pelly pairingIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, device); 5516cc86315d7a8e1f6a0f3083d0a810a7cb097832Nick Pelly pairingIntent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, type); 565f23cb39a1bd9c319383108c209fca7f0256894bJaikumar Ganesh if (type == BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION || 575f23cb39a1bd9c319383108c209fca7f0256894bJaikumar Ganesh type == BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY) { 5816cc86315d7a8e1f6a0f3083d0a810a7cb097832Nick Pelly int passkey = intent.getIntExtra(BluetoothDevice.EXTRA_PASSKEY, BluetoothDevice.ERROR); 5916cc86315d7a8e1f6a0f3083d0a810a7cb097832Nick Pelly pairingIntent.putExtra(BluetoothDevice.EXTRA_PASSKEY, passkey); 603a76bcaa83c15f96832f934e67e6f0190e72a3dcJaikumar Ganesh } 6116cc86315d7a8e1f6a0f3083d0a810a7cb097832Nick Pelly pairingIntent.setAction(BluetoothDevice.ACTION_PAIRING_REQUEST); 623a76bcaa83c15f96832f934e67e6f0190e72a3dcJaikumar Ganesh pairingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 633a76bcaa83c15f96832f934e67e6f0190e72a3dcJaikumar Ganesh 645469ff8b3f9966c6f141078b37da5f178b7825e4Michael Chan String deviceAddress = device != null ? device.getAddress() : null; 655469ff8b3f9966c6f141078b37da5f178b7825e4Michael Chan if (localManager.shouldShowDialogInForeground(deviceAddress)) { 66afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project // Since the BT-related activity is in the foreground, just open the dialog 673a76bcaa83c15f96832f934e67e6f0190e72a3dcJaikumar Ganesh context.startActivity(pairingIntent); 683a76bcaa83c15f96832f934e67e6f0190e72a3dcJaikumar Ganesh 69afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project } else { 703a76bcaa83c15f96832f934e67e6f0190e72a3dcJaikumar Ganesh 71afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project // Put up a notification that leads to the dialog 72afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project Resources res = context.getResources(); 73afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project Notification notification = new Notification( 74afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project android.R.drawable.stat_sys_data_bluetooth, 75afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project res.getString(R.string.bluetooth_notif_ticker), 76afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project System.currentTimeMillis()); 77afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project 783a76bcaa83c15f96832f934e67e6f0190e72a3dcJaikumar Ganesh PendingIntent pending = PendingIntent.getActivity(context, 0, 793a76bcaa83c15f96832f934e67e6f0190e72a3dcJaikumar Ganesh pairingIntent, PendingIntent.FLAG_ONE_SHOT); 803a76bcaa83c15f96832f934e67e6f0190e72a3dcJaikumar Ganesh 8116cc86315d7a8e1f6a0f3083d0a810a7cb097832Nick Pelly String name = intent.getStringExtra(BluetoothDevice.EXTRA_NAME); 82afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project if (TextUtils.isEmpty(name)) { 83d63c0112251ab4e4e977545368dd703d875012a4Nick Pelly name = device.getName(); 84afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project } 853a76bcaa83c15f96832f934e67e6f0190e72a3dcJaikumar Ganesh 863a76bcaa83c15f96832f934e67e6f0190e72a3dcJaikumar Ganesh notification.setLatestEventInfo(context, 873a76bcaa83c15f96832f934e67e6f0190e72a3dcJaikumar Ganesh res.getString(R.string.bluetooth_notif_title), 883a76bcaa83c15f96832f934e67e6f0190e72a3dcJaikumar Ganesh res.getString(R.string.bluetooth_notif_message) + name, 89afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project pending); 90afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project notification.flags |= Notification.FLAG_AUTO_CANCEL; 913a76bcaa83c15f96832f934e67e6f0190e72a3dcJaikumar Ganesh 923a76bcaa83c15f96832f934e67e6f0190e72a3dcJaikumar Ganesh NotificationManager manager = (NotificationManager) 93afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project context.getSystemService(Context.NOTIFICATION_SERVICE); 94afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project manager.notify(NOTIFICATION_ID, notification); 95afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project } 963a76bcaa83c15f96832f934e67e6f0190e72a3dcJaikumar Ganesh 9716cc86315d7a8e1f6a0f3083d0a810a7cb097832Nick Pelly } else if (action.equals(BluetoothDevice.ACTION_PAIRING_CANCEL)) { 983a76bcaa83c15f96832f934e67e6f0190e72a3dcJaikumar Ganesh 99afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project // Remove the notification 100afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project NotificationManager manager = (NotificationManager) context 101afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project .getSystemService(Context.NOTIFICATION_SERVICE); 102afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project manager.cancel(NOTIFICATION_ID); 103afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project } 104afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project } 105afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project} 106