• 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 import android.os.Bundle;
24 
25 import com.googlecode.android_scripting.Log;
26 import com.googlecode.android_scripting.facade.EventFacade;
27 
28 public class BluetoothPairingHelper extends BroadcastReceiver {
29   private final EventFacade mEventFacade;
30 
BluetoothPairingHelper(EventFacade eventFacade)31   public BluetoothPairingHelper(EventFacade eventFacade) {
32     super();
33     mEventFacade = eventFacade;
34     Log.d("Pairing helper created.");
35   }
36   /**
37    * Blindly confirm bluetooth connection/bonding requests.
38    */
39   @Override
onReceive(Context c, Intent intent)40   public void onReceive(Context c, Intent intent) {
41     String action = intent.getAction();
42     Bundle result = new Bundle();
43     Log.d("Bluetooth pairing intent received: " + action);
44     BluetoothDevice mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
45     if(action.equals(BluetoothDevice.ACTION_PAIRING_REQUEST)) {
46       int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR);
47       Log.d("Processing Action Paring Request with type " + type);
48       int pin = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_KEY,0);
49       result.putInt("Pin", pin);
50       result.putInt("PairingVariant", type);
51       mEventFacade.postEvent("BluetoothActionPairingRequest" + Integer.toString(type), result.clone());
52       result.clear();
53       if(type == BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION ||
54          type == BluetoothDevice.PAIRING_VARIANT_CONSENT) {
55         mDevice.setPairingConfirmation(true);
56         Log.d("Connection confirmed");
57         abortBroadcast(); // Abort the broadcast so Settings app doesn't get it.
58       }
59     }
60     else if(action.equals(BluetoothDevice.ACTION_CONNECTION_ACCESS_REQUEST)) {
61       int type = intent.getIntExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE, BluetoothDevice.ERROR);
62       Log.d("Processing Action Connection Access Request type " + type);
63       if(type == BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS ||
64          type == BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS ||
65          type == BluetoothDevice.REQUEST_TYPE_PROFILE_CONNECTION) {
66     	  Intent newIntent = new Intent(BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY);
67     	  String mReturnPackage = intent.getStringExtra(BluetoothDevice.EXTRA_PACKAGE_NAME);
68           String mReturnClass = intent.getStringExtra(BluetoothDevice.EXTRA_CLASS_NAME);
69           int mRequestType = intent.getIntExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE,
70                   BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS);
71           if (mReturnPackage != null && mReturnClass != null) {
72               newIntent.setClassName(mReturnPackage, mReturnClass);
73           }
74     	  newIntent.putExtra(BluetoothDevice.EXTRA_CONNECTION_ACCESS_RESULT,
75     			             BluetoothDevice.CONNECTION_ACCESS_YES);
76           newIntent.putExtra(BluetoothDevice.EXTRA_ALWAYS_ALLOWED, true);
77     	  newIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
78     	  newIntent.putExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE, mRequestType);
79           Log.d("Sending connection access acceptance intent.");
80           abortBroadcast();
81           c.sendBroadcast(newIntent, android.Manifest.permission.BLUETOOTH_ADMIN);
82       }
83     }
84   }
85 }
86