• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 
17 package com.googlecode.android_scripting.facade.bluetooth;
18 
19 import android.bluetooth.BluetoothDevice;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 
24 import com.googlecode.android_scripting.Log;
25 
26 public class BluetoothPairingHelper extends BroadcastReceiver {
BluetoothPairingHelper()27   public BluetoothPairingHelper() {
28     super();
29     Log.d("Pairing helper created.");
30   }
31   /**
32    * Blindly confirm bluetooth connection/bonding requests.
33    */
34   @Override
onReceive(Context c, Intent intent)35   public void onReceive(Context c, Intent intent) {
36     String action = intent.getAction();
37     Log.d("Bluetooth pairing intent received: " + action);
38     BluetoothDevice mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
39     if(action.equals(BluetoothDevice.ACTION_PAIRING_REQUEST)) {
40       int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR);
41       Log.d("Processing Action Paring Request with type " + type);
42       if(type == BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION ||
43          type == BluetoothDevice.PAIRING_VARIANT_CONSENT) {
44         mDevice.setPairingConfirmation(true);
45         Log.d("Connection confirmed");
46         abortBroadcast(); // Abort the broadcast so Settings app doesn't get it.
47       }
48     }
49     else if(action.equals(BluetoothDevice.ACTION_CONNECTION_ACCESS_REQUEST)) {
50       int type = intent.getIntExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE, BluetoothDevice.ERROR);
51       Log.d("Processing Action Connection Access Request type " + type);
52       if(type == BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS ||
53          type == BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS ||
54          type == BluetoothDevice.REQUEST_TYPE_PROFILE_CONNECTION) {
55     	  Intent newIntent = new Intent(BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY);
56     	  String mReturnPackage = intent.getStringExtra(BluetoothDevice.EXTRA_PACKAGE_NAME);
57           String mReturnClass = intent.getStringExtra(BluetoothDevice.EXTRA_CLASS_NAME);
58           int mRequestType = intent.getIntExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE,
59                   BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS);
60           if (mReturnPackage != null && mReturnClass != null) {
61               newIntent.setClassName(mReturnPackage, mReturnClass);
62           }
63     	  newIntent.putExtra(BluetoothDevice.EXTRA_CONNECTION_ACCESS_RESULT,
64     			             BluetoothDevice.CONNECTION_ACCESS_YES);
65           newIntent.putExtra(BluetoothDevice.EXTRA_ALWAYS_ALLOWED, true);
66     	  newIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
67     	  newIntent.putExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE, mRequestType);
68           Log.d("Sending connection access acceptance intent.");
69           abortBroadcast();
70           c.sendBroadcast(newIntent, android.Manifest.permission.BLUETOOTH_ADMIN);
71       }
72     }
73   }
74 }