BluetoothService.java revision dcbc97fc28f4eb5910acc11dfac5efcd597c737d
19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/**
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * TODO: Move this to
19bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * java/services/com/android/server/BluetoothService.java
209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * and make the contructor package private again.
219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @hide
239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpackage android.server;
269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
27bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pellyimport android.bluetooth.BluetoothAdapter;
28dd0463aef18d251c741bfc9dc7a2787443ef36f1Jaikumar Ganeshimport android.bluetooth.BluetoothClass;
299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.bluetooth.BluetoothDevice;
30f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganeshimport android.bluetooth.BluetoothDeviceProfileState;
315a1e4cf83f5be1b5d79e2643fa791aa269b6a4bcJaikumar Ganeshimport android.bluetooth.BluetoothHeadset;
322ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganeshimport android.bluetooth.BluetoothHealthAppConfiguration;
334ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganeshimport android.bluetooth.BluetoothInputDevice;
346fdd0c6274c81b337ad35b70480f881daf7354c3Danica Changimport android.bluetooth.BluetoothPan;
3596a79830ea1ae3ab3d6d3cce2bd1397fcd40ea0eJaikumar Ganeshimport android.bluetooth.BluetoothProfile;
36f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganeshimport android.bluetooth.BluetoothProfileState;
3724bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pellyimport android.bluetooth.BluetoothSocket;
3810eac971b3a6e5f34a420dd68ebfa796553ad2b9Jaikumar Ganeshimport android.bluetooth.BluetoothUuid;
39bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pellyimport android.bluetooth.IBluetooth;
4016fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pellyimport android.bluetooth.IBluetoothCallback;
41fb658c72a3a76dac334c39070d1501a2575c1069Jaikumar Ganeshimport android.bluetooth.IBluetoothHealthCallback;
42ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganeshimport android.bluetooth.IBluetoothStateChangeCallback;
439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.BroadcastReceiver;
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.ContentResolver;
459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.Context;
469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.Intent;
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.IntentFilter;
486e9c443460e40e9d663c117ba836585335e7c2c1Jaikumar Ganeshimport android.content.SharedPreferences;
499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.Binder;
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.Handler;
513fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganeshimport android.os.IBinder;
529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.Message;
532ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganeshimport android.os.ParcelFileDescriptor;
543fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganeshimport android.os.ParcelUuid;
559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.RemoteException;
56105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Projectimport android.os.ServiceManager;
579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.provider.Settings;
589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.util.Log;
59cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganeshimport android.util.Pair;
609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
61fb658c72a3a76dac334c39070d1501a2575c1069Jaikumar Ganeshimport com.android.internal.app.IBatteryStats;
62fb658c72a3a76dac334c39070d1501a2575c1069Jaikumar Ganesh
633fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganeshimport java.io.BufferedInputStream;
64a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xieimport java.io.BufferedReader;
653fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganeshimport java.io.BufferedWriter;
66a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xieimport java.io.DataInputStream;
67a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xieimport java.io.File;
689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.FileDescriptor;
693fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganeshimport java.io.FileInputStream;
703fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganeshimport java.io.FileNotFoundException;
713fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganeshimport java.io.FileWriter;
723fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganeshimport java.io.IOException;
73ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganeshimport java.io.InputStreamReader;
749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.PrintWriter;
75a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xieimport java.io.RandomAccessFile;
769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.UnsupportedEncodingException;
779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.util.ArrayList;
789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.util.Arrays;
79ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganeshimport java.util.Collection;
80ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganeshimport java.util.Collections;
819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.util.HashMap;
8216fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pellyimport java.util.Iterator;
835a1e4cf83f5be1b5d79e2643fa791aa269b6a4bcJaikumar Ganeshimport java.util.List;
849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.util.Map;
859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
86bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pellypublic class BluetoothService extends IBluetooth.Stub {
87bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    private static final String TAG = "BluetoothService";
889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final boolean DBG = true;
899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private int mNativeData;
919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private BluetoothEventLoop mEventLoop;
9296a79830ea1ae3ab3d6d3cce2bd1397fcd40ea0eJaikumar Ganesh    private BluetoothHeadset mBluetoothHeadset;
934ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh    private BluetoothInputDevice mInputDevice;
9474ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh    private BluetoothPan mPan;
959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private boolean mIsAirplaneSensitive;
9644303922f14ac71b446a6e50e1180be4c8fed2c7Jeff Sharkey    private boolean mIsAirplaneToggleable;
977f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie    private BluetoothAdapterStateMachine mBluetoothState;
9884690c88f37f395094147d27ace8319a2803a522Jaikumar Ganesh    private int[] mAdapterSdpHandles;
9950b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh    private ParcelUuid[] mAdapterUuids;
100997c7612a8cf63748165144b423ff2ae8e73c3e9Nick Pelly
101bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    private BluetoothAdapter mAdapter;  // constant after init()
1029a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    private final BluetoothBondState mBondState;  // local cache of bondings
103105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project    private final IBatteryStats mBatteryStats;
1049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private final Context mContext;
105ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh    private Map<Integer, IBluetoothStateChangeCallback> mStateChangeTracker =
106ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh        Collections.synchronizedMap(new HashMap<Integer, IBluetoothStateChangeCallback>());
1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final String BLUETOOTH_ADMIN_PERM = android.Manifest.permission.BLUETOOTH_ADMIN;
109a8571f15c39da2b7c6e672fef26106f5ac60dea0Jaikumar Ganesh    static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH;
1109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1113fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh    private static final String DOCK_ADDRESS_PATH = "/sys/class/switch/dock/bt_addr";
1123fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh    private static final String DOCK_PIN_PATH = "/sys/class/switch/dock/bt_pin";
1133fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh
1146e9c443460e40e9d663c117ba836585335e7c2c1Jaikumar Ganesh    private static final String SHARED_PREFERENCE_DOCK_ADDRESS = "dock_bluetooth_address";
1156e9c443460e40e9d663c117ba836585335e7c2c1Jaikumar Ganesh    private static final String SHARED_PREFERENCES_NAME = "bluetooth_service_settings";
1166e9c443460e40e9d663c117ba836585335e7c2c1Jaikumar Ganesh
1177f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie    private static final int MESSAGE_UUID_INTENT = 1;
1187f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie    private static final int MESSAGE_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY = 2;
119484867a8ed3d46fbb8df493c77f2e2d46a0db654Matthew Xie    private static final int MESSAGE_REMOVE_SERVICE_RECORD = 3;
120a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh
121ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh    private static final int RFCOMM_RECORD_REAPER = 10;
122ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh    private static final int STATE_CHANGE_REAPER = 11;
123ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh
124a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh    // The time (in millisecs) to delay the pairing attempt after the first
125a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh    // auto pairing attempt fails. We use an exponential delay with
126a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh    // INIT_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY as the initial value and
127a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh    // MAX_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY as the max value.
128a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh    private static final long INIT_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY = 3000;
129a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh    private static final long MAX_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY = 12000;
1301caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh
1311caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh    // The timeout used to sent the UUIDs Intent
1321caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh    // This timeout should be greater than the page timeout
1331caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh    private static final int UUID_INTENT_DELAY = 6000;
134105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project
13516fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly    /** Always retrieve RFCOMM channel for these SDP UUIDs */
13616fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly    private static final ParcelUuid[] RFCOMM_UUIDS = {
13716fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly            BluetoothUuid.Handsfree,
13816fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly            BluetoothUuid.HSP,
13916fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly            BluetoothUuid.ObexObjectPush };
14016fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly
1419a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    private final BluetoothAdapterProperties mAdapterProperties;
1429a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    private final BluetoothDeviceProperties mDeviceProperties;
143d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh
14416fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly    private final HashMap<String, Map<ParcelUuid, Integer>> mDeviceServiceChannelCache;
14516fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly    private final ArrayList<String> mUuidIntentTracker;
14616fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly    private final HashMap<RemoteService, IBluetoothCallback> mUuidCallbackTracker;
1471caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh
148f1754050ceaadb4603aaaa0b1f0b193a305834afMatthew Xie    private final HashMap<Integer, Pair<Integer, IBinder>> mServiceRecordToPid;
14924bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly
150f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh    private final HashMap<String, BluetoothDeviceProfileState> mDeviceProfileState;
151f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh    private final BluetoothProfileState mA2dpProfileState;
152f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh    private final BluetoothProfileState mHfpProfileState;
1539b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh
1549b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh    private BluetoothA2dpService mA2dpService;
155cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh    private final HashMap<String, Pair<byte[], byte[]>> mDeviceOobData;
156545e6708adda6859932b55fd824794b1401f5318Jaikumar Ganesh
157a46f2fb152355fe591f9dddeb41f01fef5ccad1dJaikumar Ganesh    private int mProfilesConnected = 0, mProfilesConnecting = 0, mProfilesDisconnecting = 0;
158707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh
1593fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh    private static String mDockAddress;
1603fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh    private String mDockPin;
1613fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh
162c53cab20b225ba69057441ecb06db6478ab8ff9bJaikumar Ganesh    private int mAdapterConnectionState = BluetoothAdapter.STATE_DISCONNECTED;
163a8571f15c39da2b7c6e672fef26106f5ac60dea0Jaikumar Ganesh    private BluetoothPanProfileHandler mBluetoothPanProfileHandler;
16430b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh    private BluetoothInputProfileHandler mBluetoothInputProfileHandler;
1652ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh    private BluetoothHealthProfileHandler mBluetoothHealthProfileHandler;
166a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie    private static final String INCOMING_CONNECTION_FILE =
167a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie      "/data/misc/bluetooth/incoming_connection.conf";
168a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie    private HashMap<String, Pair<Integer, String>> mIncomingConnections;
169cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh    private HashMap<Integer, Pair<Integer, Integer>> mProfileConnectionState;
170c53cab20b225ba69057441ecb06db6478ab8ff9bJaikumar Ganesh
17116fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly    private static class RemoteService {
17216fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly        public String address;
17316fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly        public ParcelUuid uuid;
17416fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly        public RemoteService(String address, ParcelUuid uuid) {
17516fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly            this.address = address;
17616fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly            this.uuid = uuid;
17716fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly        }
17816fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly        @Override
17916fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly        public boolean equals(Object o) {
18016fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly            if (o instanceof RemoteService) {
18116fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly                RemoteService service = (RemoteService)o;
18216fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly                return address.equals(service.address) && uuid.equals(service.uuid);
18316fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly            }
18416fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly            return false;
18516fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly        }
1865f61416305c47c4db0b94c0cf500e9a9d11d4cdbKenny Root
1875f61416305c47c4db0b94c0cf500e9a9d11d4cdbKenny Root        @Override
1885f61416305c47c4db0b94c0cf500e9a9d11d4cdbKenny Root        public int hashCode() {
1895f61416305c47c4db0b94c0cf500e9a9d11d4cdbKenny Root            int hash = 1;
1905f61416305c47c4db0b94c0cf500e9a9d11d4cdbKenny Root            hash = hash * 31 + (address == null ? 0 : address.hashCode());
1915f61416305c47c4db0b94c0cf500e9a9d11d4cdbKenny Root            hash = hash * 31 + (uuid == null ? 0 : uuid.hashCode());
1925f61416305c47c4db0b94c0cf500e9a9d11d4cdbKenny Root            return hash;
1935f61416305c47c4db0b94c0cf500e9a9d11d4cdbKenny Root        }
19416fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly    }
19516fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly
1969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static {
1979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        classInitNative();
1989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
200bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    public BluetoothService(Context context) {
2019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mContext = context;
202105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project
203105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project        // Need to do this in place of:
204105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project        // mBatteryStats = BatteryStatsService.getService();
205105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project        // Since we can not import BatteryStatsService from here. This class really needs to be
206105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project        // moved to java/services/com/android/server/
207105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project        mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService("batteryinfo"));
2089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        initializeNativeDataNative();
210ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project
211ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project        if (isEnabledNative() == 1) {
212ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project            Log.w(TAG, "Bluetooth daemons already running - runtime restart? ");
213ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project            disableNative();
2149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
215ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project
2169a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        mBondState = new BluetoothBondState(context, this);
2179a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        mAdapterProperties = new BluetoothAdapterProperties(context, this);
2189a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        mDeviceProperties = new BluetoothDeviceProperties(this);
21910eac971b3a6e5f34a420dd68ebfa796553ad2b9Jaikumar Ganesh
22010eac971b3a6e5f34a420dd68ebfa796553ad2b9Jaikumar Ganesh        mDeviceServiceChannelCache = new HashMap<String, Map<ParcelUuid, Integer>>();
221cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh        mDeviceOobData = new HashMap<String, Pair<byte[], byte[]>>();
2221caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh        mUuidIntentTracker = new ArrayList<String>();
22316fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly        mUuidCallbackTracker = new HashMap<RemoteService, IBluetoothCallback>();
224f1754050ceaadb4603aaaa0b1f0b193a305834afMatthew Xie        mServiceRecordToPid = new HashMap<Integer, Pair<Integer, IBinder>>();
225f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh        mDeviceProfileState = new HashMap<String, BluetoothDeviceProfileState>();
226f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh        mA2dpProfileState = new BluetoothProfileState(mContext, BluetoothProfileState.A2DP);
227f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh        mHfpProfileState = new BluetoothProfileState(mContext, BluetoothProfileState.HFP);
228f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh
229f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh        mHfpProfileState.start();
230f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh        mA2dpProfileState.start();
2313fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh
2323fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh        IntentFilter filter = new IntentFilter();
2336e9c443460e40e9d663c117ba836585335e7c2c1Jaikumar Ganesh        registerForAirplaneMode(filter);
2346e9c443460e40e9d663c117ba836585335e7c2c1Jaikumar Ganesh
2353fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh        filter.addAction(Intent.ACTION_DOCK_EVENT);
2366e9c443460e40e9d663c117ba836585335e7c2c1Jaikumar Ganesh        mContext.registerReceiver(mReceiver, filter);
23730b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh        mBluetoothInputProfileHandler = BluetoothInputProfileHandler.getInstance(mContext, this);
238a8571f15c39da2b7c6e672fef26106f5ac60dea0Jaikumar Ganesh        mBluetoothPanProfileHandler = BluetoothPanProfileHandler.getInstance(mContext, this);
2392ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh        mBluetoothHealthProfileHandler = BluetoothHealthProfileHandler.getInstance(mContext, this);
240a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        mIncomingConnections = new HashMap<String, Pair<Integer, String>>();
241cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        mProfileConnectionState = new HashMap<Integer, Pair<Integer, Integer>>();
2423fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh    }
2433fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh
2449b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh    public static synchronized String readDockBluetoothAddress() {
2453fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh        if (mDockAddress != null) return mDockAddress;
2463fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh
2473fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh        BufferedInputStream file = null;
2483fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh        String dockAddress;
2493fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh        try {
2503fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh            file = new BufferedInputStream(new FileInputStream(DOCK_ADDRESS_PATH));
2513fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh            byte[] address = new byte[17];
2523fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh            file.read(address);
2533fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh            dockAddress = new String(address);
2543fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh            dockAddress = dockAddress.toUpperCase();
2553fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh            if (BluetoothAdapter.checkBluetoothAddress(dockAddress)) {
2563fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh                mDockAddress = dockAddress;
2573fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh                return mDockAddress;
2583fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh            } else {
2599a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                Log.e(TAG, "CheckBluetoothAddress failed for car dock address: "
2609a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                        + dockAddress);
2613fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh            }
2623fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh        } catch (FileNotFoundException e) {
2639a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            Log.e(TAG, "FileNotFoundException while trying to read dock address");
2643fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh        } catch (IOException e) {
2659a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            Log.e(TAG, "IOException while trying to read dock address");
2663fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh        } finally {
2673fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh            if (file != null) {
2683fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh                try {
2693fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh                    file.close();
2703fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh                } catch (IOException e) {
2713fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh                    // Ignore
2723fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh                }
2733fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh            }
2743fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh        }
2753fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh        mDockAddress = null;
2763fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh        return null;
2773fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh    }
2783fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh
2793fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh    private synchronized boolean writeDockPin() {
2803fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh        BufferedWriter out = null;
2813fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh        try {
2823fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh            out = new BufferedWriter(new FileWriter(DOCK_PIN_PATH));
2833fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh
2843fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh            // Generate a random 4 digit pin between 0000 and 9999
2853fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh            // This is not truly random but good enough for our purposes.
2863fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh            int pin = (int) Math.floor(Math.random() * 10000);
2873fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh
2883fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh            mDockPin = String.format("%04d", pin);
2893fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh            out.write(mDockPin);
2903fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh            return true;
2913fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh        } catch (FileNotFoundException e) {
2929a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            Log.e(TAG, "FileNotFoundException while trying to write dock pairing pin");
2933fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh        } catch (IOException e) {
2949a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            Log.e(TAG, "IOException while while trying to write dock pairing pin");
2953fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh        } finally {
2963fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh            if (out != null) {
2973fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh                try {
2983fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh                    out.close();
2993fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh                } catch (IOException e) {
3003fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh                    // Ignore
3013fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh                }
3023fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh            }
3033fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh        }
3043fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh        mDockPin = null;
3053fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh        return false;
3063fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh    }
3073fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh
3083fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh    /*package*/ synchronized String getDockPin() {
3093fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh        return mDockPin;
310bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    }
311bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly
312bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    public synchronized void initAfterRegistration() {
313f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pelly        mAdapter = BluetoothAdapter.getDefaultAdapter();
3147f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie        mBluetoothState = new BluetoothAdapterStateMachine(mContext, this, mAdapter);
3157f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie        mBluetoothState.start();
3167f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie        mEventLoop = mBluetoothState.getBluetoothEventLoop();
3179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3197d0548d0944e48421857de4aec2822ced325bea0Jaikumar Ganesh    public synchronized void initAfterA2dpRegistration() {
3207d0548d0944e48421857de4aec2822ced325bea0Jaikumar Ganesh        mEventLoop.getProfileProxy();
3217d0548d0944e48421857de4aec2822ced325bea0Jaikumar Ganesh    }
3227d0548d0944e48421857de4aec2822ced325bea0Jaikumar Ganesh
3239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    @Override
3249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    protected void finalize() throws Throwable {
3256e9c443460e40e9d663c117ba836585335e7c2c1Jaikumar Ganesh        mContext.unregisterReceiver(mReceiver);
3269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
3279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            cleanupNativeDataNative();
3289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } finally {
3299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            super.finalize();
3309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public boolean isEnabled() {
3349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
3358c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh        return isEnabledInternal();
3368c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh    }
3378c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh
3388c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh    private boolean isEnabledInternal() {
3397f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie        return (getBluetoothStateInternal() == BluetoothAdapter.STATE_ON);
3409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
342105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project    public int getBluetoothState() {
343105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
3447f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie        return getBluetoothStateInternal();
345105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project    }
346105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project
3479a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    int getBluetoothStateInternal() {
3487f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie        return mBluetoothState.getBluetoothAdapterState();
3499a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    }
350105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project
3519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Bring down bluetooth and disable BT in settings. Returns true on success.
3539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public boolean disable() {
3559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return disable(true);
3569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Bring down bluetooth. Returns true on success.
3609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
361e6ee3be1c254404dad842298f6f56c11cc6c7ac8Nick Pelly     * @param saveSetting If true, persist the new setting
3629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public synchronized boolean disable(boolean saveSetting) {
364e6ee3be1c254404dad842298f6f56c11cc6c7ac8Nick Pelly        mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, "Need BLUETOOTH_ADMIN permission");
3659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3667f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie        int adapterState = getBluetoothStateInternal();
3677f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie
3687f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie        switch (adapterState) {
369de893f550301a60274e87aa8168225e7a7a42184Nick Pelly        case BluetoothAdapter.STATE_OFF:
370105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project            return true;
371de893f550301a60274e87aa8168225e7a7a42184Nick Pelly        case BluetoothAdapter.STATE_ON:
372105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project            break;
373105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project        default:
374105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project            return false;
375105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project        }
3767a0f816916abb200d9337a5afde5d6d2a637a20aJaikumar Ganesh
3777f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie        mBluetoothState.sendMessage(BluetoothAdapterStateMachine.USER_TURN_OFF, saveSetting);
378105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project        return true;
379105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project    }
380105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project
3817f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie    synchronized void disconnectDevices() {
3827a0f816916abb200d9337a5afde5d6d2a637a20aJaikumar Ganesh        // Disconnect devices handled by BluetoothService.
3837a0f816916abb200d9337a5afde5d6d2a637a20aJaikumar Ganesh        for (BluetoothDevice device: getConnectedInputDevices()) {
3847a0f816916abb200d9337a5afde5d6d2a637a20aJaikumar Ganesh            disconnectInputDevice(device);
3857a0f816916abb200d9337a5afde5d6d2a637a20aJaikumar Ganesh        }
3867a0f816916abb200d9337a5afde5d6d2a637a20aJaikumar Ganesh
3877a0f816916abb200d9337a5afde5d6d2a637a20aJaikumar Ganesh        for (BluetoothDevice device: getConnectedPanDevices()) {
3887a0f816916abb200d9337a5afde5d6d2a637a20aJaikumar Ganesh            disconnectPanDevice(device);
3897a0f816916abb200d9337a5afde5d6d2a637a20aJaikumar Ganesh        }
3907a0f816916abb200d9337a5afde5d6d2a637a20aJaikumar Ganesh    }
391105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project
3927f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie    /**
393dcbc97fc28f4eb5910acc11dfac5efcd597c737dMatthew Xie     * The Bluetooth has been turned off, but hot. Do bonding, profile cleanup
3947f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie     */
3957f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie    synchronized void finishDisable() {
3969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // mark in progress bondings as cancelled
3979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        for (String address : mBondState.listInState(BluetoothDevice.BOND_BONDING)) {
398005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly            mBondState.setBondState(address, BluetoothDevice.BOND_NONE,
3999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                    BluetoothDevice.UNBOND_REASON_AUTH_CANCELED);
4009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4022d1fc4e3f5eb612f9ef8a50080310ced1dfaaadfJaikumar Ganesh        // Stop the profile state machine for bonded devices.
4032d1fc4e3f5eb612f9ef8a50080310ced1dfaaadfJaikumar Ganesh        for (String address : mBondState.listInState(BluetoothDevice.BOND_BONDED)) {
4042d1fc4e3f5eb612f9ef8a50080310ced1dfaaadfJaikumar Ganesh            removeProfileState(address);
4052d1fc4e3f5eb612f9ef8a50080310ced1dfaaadfJaikumar Ganesh        }
4062d1fc4e3f5eb612f9ef8a50080310ced1dfaaadfJaikumar Ganesh
4079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // update mode
408de893f550301a60274e87aa8168225e7a7a42184Nick Pelly        Intent intent = new Intent(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
409de893f550301a60274e87aa8168225e7a7a42184Nick Pelly        intent.putExtra(BluetoothAdapter.EXTRA_SCAN_MODE, BluetoothAdapter.SCAN_MODE_NONE);
4109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mContext.sendBroadcast(intent, BLUETOOTH_PERM);
411dcbc97fc28f4eb5910acc11dfac5efcd597c737dMatthew Xie    }
4129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
413dcbc97fc28f4eb5910acc11dfac5efcd597c737dMatthew Xie    /**
414dcbc97fc28f4eb5910acc11dfac5efcd597c737dMatthew Xie     * Local clean up after broadcasting STATE_OFF intent
415dcbc97fc28f4eb5910acc11dfac5efcd597c737dMatthew Xie     */
416dcbc97fc28f4eb5910acc11dfac5efcd597c737dMatthew Xie    synchronized void cleanupAfterFinishDisable() {
417bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        mAdapterProperties.clear();
418dcbc97fc28f4eb5910acc11dfac5efcd597c737dMatthew Xie
419dcbc97fc28f4eb5910acc11dfac5efcd597c737dMatthew Xie        for (Integer srHandle : mServiceRecordToPid.keySet()) {
420dcbc97fc28f4eb5910acc11dfac5efcd597c737dMatthew Xie            removeServiceRecordNative(srHandle);
421dcbc97fc28f4eb5910acc11dfac5efcd597c737dMatthew Xie        }
42224bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        mServiceRecordToPid.clear();
423105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project
424a46f2fb152355fe591f9dddeb41f01fef5ccad1dJaikumar Ganesh        mProfilesConnected = 0;
425a46f2fb152355fe591f9dddeb41f01fef5ccad1dJaikumar Ganesh        mProfilesConnecting = 0;
426a46f2fb152355fe591f9dddeb41f01fef5ccad1dJaikumar Ganesh        mProfilesDisconnecting = 0;
427c53cab20b225ba69057441ecb06db6478ab8ff9bJaikumar Ganesh        mAdapterConnectionState = BluetoothAdapter.STATE_DISCONNECTED;
42850b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh        mAdapterUuids = null;
42950b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh        mAdapterSdpHandles = null;
430a46f2fb152355fe591f9dddeb41f01fef5ccad1dJaikumar Ganesh
431105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project        // Log bluetooth off to battery stats.
432105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project        long ident = Binder.clearCallingIdentity();
433105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project        try {
434105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project            mBatteryStats.noteBluetoothOff();
435105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project        } catch (RemoteException e) {
436105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project        } finally {
437105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project            Binder.restoreCallingIdentity(ident);
438105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project        }
4399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4417f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie    /**
4427f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie     * power off Bluetooth
4437f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie     */
4447f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie    synchronized void shutoffBluetooth() {
44514e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie        if (mAdapterSdpHandles != null) removeReservedServiceRecordsNative(mAdapterSdpHandles);
44614e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie        setBluetoothTetheringNative(false, BluetoothPanProfileHandler.NAP_ROLE,
44714e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie                BluetoothPanProfileHandler.NAP_BRIDGE);
4487f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie        tearDownNativeDataNative();
44914e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie    }
45014e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie
45114e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie    /**
45214e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie     * Data clean up after Bluetooth shutoff
45314e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie     */
45414e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie    synchronized void cleanNativeAfterShutoffBluetooth() {
45514e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie        // Ths method is called after shutdown of event loop in the Bluetooth shut down
45614e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie        // procedure
45714e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie
45814e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie        // the adapter property could be changed before event loop is stoped, clear it again
45914e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie        mAdapterProperties.clear();
4607f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie        disableNative();
4617f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie    }
4627f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie
463105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project    /** Bring up BT and persist BT on in settings */
464105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project    public boolean enable() {
465105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project        return enable(true);
4669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Enable this Bluetooth device, asynchronously.
4709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * This turns on/off the underlying hardware.
4719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
472105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project     * @param saveSetting If true, persist the new state of BT in settings
473105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project     * @return True on success (so far)
4749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
475105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project    public synchronized boolean enable(boolean saveSetting) {
4769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
4779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                "Need BLUETOOTH_ADMIN permission");
4789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // Airplane mode can prevent Bluetooth radio from being turned on.
48044303922f14ac71b446a6e50e1180be4c8fed2c7Jeff Sharkey        if (mIsAirplaneSensitive && isAirplaneModeOn() && !mIsAirplaneToggleable) {
4819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return false;
4829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4837f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie        mBluetoothState.sendMessage(BluetoothAdapterStateMachine.USER_TURN_ON, saveSetting);
4847f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie        return true;
4857f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie    }
4867f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie
4877f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie    /**
4887f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie     * Turn on Bluetooth Module, Load firmware, and do all the preparation
4897f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie     * needed to get the Bluetooth Module ready but keep it not discoverable
4907f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie     * and not connectable.
4917f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie     */
4927f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie    /* package */ synchronized boolean prepareBluetooth() {
4937f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie        if (!setupNativeDataNative()) {
4949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return false;
4959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4967f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie        switchConnectable(false);
4977f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie        updateSdpRecords();
4989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return true;
4999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private final Handler mHandler = new Handler() {
5029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        @Override
5039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public void handleMessage(Message msg) {
5049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            switch (msg.what) {
5051caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh            case MESSAGE_UUID_INTENT:
5061caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh                String address = (String)msg.obj;
50716fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly                if (address != null) {
5081caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh                    sendUuidIntent(address);
50916fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly                    makeServiceChannelCallbacks(address);
51016fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly                }
5111caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh                break;
512a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh            case MESSAGE_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY:
513a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh                address = (String)msg.obj;
5148adcacbf83697d1299812f5365380b097898567aJaikumar Ganesh                if (address == null) return;
5158adcacbf83697d1299812f5365380b097898567aJaikumar Ganesh                int attempt = mBondState.getAttempt(address);
5168adcacbf83697d1299812f5365380b097898567aJaikumar Ganesh
5178adcacbf83697d1299812f5365380b097898567aJaikumar Ganesh                // Try only if attemps are in progress and cap it 2 attempts
5188adcacbf83697d1299812f5365380b097898567aJaikumar Ganesh                // The 2 attempts cap is a fail safe if the stack returns
5198adcacbf83697d1299812f5365380b097898567aJaikumar Ganesh                // an incorrect error code for bonding failures and if the pin
5208adcacbf83697d1299812f5365380b097898567aJaikumar Ganesh                // is entered wrongly twice we should abort.
5218adcacbf83697d1299812f5365380b097898567aJaikumar Ganesh                if (attempt > 0 && attempt <= 2) {
5228adcacbf83697d1299812f5365380b097898567aJaikumar Ganesh                    mBondState.attempt(address);
523a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh                    createBond(address);
524a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh                    return;
525a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh                }
5268adcacbf83697d1299812f5365380b097898567aJaikumar Ganesh                if (attempt > 0) mBondState.clearPinAttempts(address);
527a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh                break;
528484867a8ed3d46fbb8df493c77f2e2d46a0db654Matthew Xie            case MESSAGE_REMOVE_SERVICE_RECORD:
529484867a8ed3d46fbb8df493c77f2e2d46a0db654Matthew Xie                Pair<Integer, Integer> pair = (Pair<Integer, Integer>) msg.obj;
530484867a8ed3d46fbb8df493c77f2e2d46a0db654Matthew Xie                checkAndRemoveRecord(pair.first, pair.second);
531484867a8ed3d46fbb8df493c77f2e2d46a0db654Matthew Xie                break;
5329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
5339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    };
5359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
53650b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh    private synchronized void addReservedSdpRecords(final ArrayList<ParcelUuid> uuids) {
53750b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh        //Register SDP records.
53850b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh        int[] svcIdentifiers = new int[uuids.size()];
53950b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh        for (int i = 0; i < uuids.size(); i++) {
54050b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh            svcIdentifiers[i] = BluetoothUuid.getServiceIdentifierFromParcelUuid(uuids.get(i));
54150b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh        }
54250b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh        mAdapterSdpHandles = addReservedServiceRecordsNative(svcIdentifiers);
54350b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh    }
544105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project
54550b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh    private synchronized void updateSdpRecords() {
54650b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh        ArrayList<ParcelUuid> uuids = new ArrayList<ParcelUuid>();
54750b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh
54850b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh        // Add the default records
54950b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh        uuids.add(BluetoothUuid.HSP_AG);
55050b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh        uuids.add(BluetoothUuid.ObexObjectPush);
55150b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh
55250b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh        if (mContext.getResources().
55350b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh                getBoolean(com.android.internal.R.bool.config_voice_capable)) {
55450b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh            uuids.add(BluetoothUuid.Handsfree_AG);
55550b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh            uuids.add(BluetoothUuid.PBAP_PSE);
55650b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh        }
55750b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh
55850b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh        // Add SDP records for profiles maintained by Android userspace
55950b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh        addReservedSdpRecords(uuids);
56050b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh
56150b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh        // Enable profiles maintained by Bluez userspace.
56274ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh        setBluetoothTetheringNative(true, BluetoothPanProfileHandler.NAP_ROLE,
56374ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh                BluetoothPanProfileHandler.NAP_BRIDGE);
56450b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh
56550b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh        // Add SDP records for profiles maintained by Bluez userspace
56650b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh        uuids.add(BluetoothUuid.AudioSource);
56750b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh        uuids.add(BluetoothUuid.AvrcpTarget);
56850b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh        uuids.add(BluetoothUuid.NAP);
56950b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh
57050b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh        // Cannot cast uuids.toArray directly since ParcelUuid is parcelable
57150b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh        mAdapterUuids = new ParcelUuid[uuids.size()];
57250b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh        for (int i = 0; i < uuids.size(); i++) {
57350b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh            mAdapterUuids[i] = uuids.get(i);
57450b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh        }
57550b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh    }
57650b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh
5779efb1343a018c479ff57d70e8058658faa8a926dJaikumar Ganesh    /**
5789efb1343a018c479ff57d70e8058658faa8a926dJaikumar Ganesh     * This function is called from Bluetooth Event Loop when onPropertyChanged
5799efb1343a018c479ff57d70e8058658faa8a926dJaikumar Ganesh     * for adapter comes in with UUID property.
5809efb1343a018c479ff57d70e8058658faa8a926dJaikumar Ganesh     * @param uuidsThe uuids of adapter as reported by Bluez.
5819efb1343a018c479ff57d70e8058658faa8a926dJaikumar Ganesh     */
5827f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie    /*package*/ synchronized void updateBluetoothState(String uuids) {
5837f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie        ParcelUuid[] adapterUuids = convertStringToParcelUuid(uuids);
5849efb1343a018c479ff57d70e8058658faa8a926dJaikumar Ganesh
5857f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie        if (mAdapterUuids != null &&
5867f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie            BluetoothUuid.containsAllUuids(adapterUuids, mAdapterUuids)) {
5877f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie            mBluetoothState.sendMessage(BluetoothAdapterStateMachine.SERVICE_RECORD_LOADED);
5889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5917f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie    /**
5928594394aadeaaa3f834d33d7b198fb071e0f31feMatthew Xie     * This method is called immediately before Bluetooth module is turned on after
5938594394aadeaaa3f834d33d7b198fb071e0f31feMatthew Xie     * the adapter became pariable.
5948594394aadeaaa3f834d33d7b198fb071e0f31feMatthew Xie     * It inits bond state and profile state before STATE_ON intent is broadcasted.
5958594394aadeaaa3f834d33d7b198fb071e0f31feMatthew Xie     */
5968594394aadeaaa3f834d33d7b198fb071e0f31feMatthew Xie    /*package*/ void initBluetoothAfterTurningOn() {
597d96daf1ae5f314ea5f56b3e0b465e3f3b0314235Matthew Xie        String discoverable = getProperty("Discoverable", false);
598d96daf1ae5f314ea5f56b3e0b465e3f3b0314235Matthew Xie        String timeout = getProperty("DiscoverableTimeout", false);
599d96daf1ae5f314ea5f56b3e0b465e3f3b0314235Matthew Xie        if (discoverable.equals("true") && Integer.valueOf(timeout) != 0) {
600d96daf1ae5f314ea5f56b3e0b465e3f3b0314235Matthew Xie            setAdapterPropertyBooleanNative("Discoverable", 0);
601d96daf1ae5f314ea5f56b3e0b465e3f3b0314235Matthew Xie        }
6028594394aadeaaa3f834d33d7b198fb071e0f31feMatthew Xie        mBondState.initBondState();
6038594394aadeaaa3f834d33d7b198fb071e0f31feMatthew Xie        initProfileState();
6048594394aadeaaa3f834d33d7b198fb071e0f31feMatthew Xie    }
6058594394aadeaaa3f834d33d7b198fb071e0f31feMatthew Xie
6068594394aadeaaa3f834d33d7b198fb071e0f31feMatthew Xie    /**
6077f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie     * This method is called immediately after Bluetooth module is turned on.
6087f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie     * It starts auto-connection and places bluetooth on sign onto the battery
6097f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie     * stats
6107f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie     */
6117f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie    /*package*/ void runBluetooth() {
6127f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie        autoConnect();
6137f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie
6147f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie        // Log bluetooth on to battery stats.
6157f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie        long ident = Binder.clearCallingIdentity();
6167f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie        try {
6177f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie            mBatteryStats.noteBluetoothOn();
6187f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie        } catch (RemoteException e) {
6197f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie            Log.e(TAG, "", e);
6207f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie        } finally {
6217f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie            Binder.restoreCallingIdentity(ident);
6227f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie        }
6239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
625a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh    /*package*/ synchronized boolean attemptAutoPair(String address) {
626a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh        if (!mBondState.hasAutoPairingFailed(address) &&
627a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh                !mBondState.isAutoPairingBlacklisted(address)) {
628a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh            mBondState.attempt(address);
629a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh            setPin(address, BluetoothDevice.convertPinToBytes("0000"));
630a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh            return true;
631a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh        }
632a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh        return false;
633a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh    }
634a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh
635f487d72215421a02e5a1b2fbff4618bc5ee185cbJaikumar Ganesh    /*package*/ synchronized boolean isFixedPinZerosAutoPairKeyboard(String address) {
636f487d72215421a02e5a1b2fbff4618bc5ee185cbJaikumar Ganesh        // Check for keyboards which have fixed PIN 0000 as the pairing pin
637f487d72215421a02e5a1b2fbff4618bc5ee185cbJaikumar Ganesh        return mBondState.isFixedPinZerosAutoPairKeyboard(address);
638f487d72215421a02e5a1b2fbff4618bc5ee185cbJaikumar Ganesh    }
639f487d72215421a02e5a1b2fbff4618bc5ee185cbJaikumar Ganesh
640a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh    /*package*/ synchronized void onCreatePairedDeviceResult(String address, int result) {
641a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh        if (result == BluetoothDevice.BOND_SUCCESS) {
642a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh            setBondState(address, BluetoothDevice.BOND_BONDED);
643a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh            if (mBondState.isAutoPairingAttemptsInProgress(address)) {
644a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh                mBondState.clearPinAttempts(address);
645a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh            }
646a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh        } else if (result == BluetoothDevice.UNBOND_REASON_AUTH_FAILED &&
647a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh                mBondState.getAttempt(address) == 1) {
648a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh            mBondState.addAutoPairingFailure(address);
649a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh            pairingAttempt(address, result);
650a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh        } else if (result == BluetoothDevice.UNBOND_REASON_REMOTE_DEVICE_DOWN &&
651a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh              mBondState.isAutoPairingAttemptsInProgress(address)) {
652a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh            pairingAttempt(address, result);
653a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh        } else {
654a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh            setBondState(address, BluetoothDevice.BOND_NONE, result);
655a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh            if (mBondState.isAutoPairingAttemptsInProgress(address)) {
656a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh                mBondState.clearPinAttempts(address);
657a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh            }
658a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh        }
659a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh    }
660a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh
661a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh    /*package*/ synchronized String getPendingOutgoingBonding() {
662a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh        return mBondState.getPendingOutgoingBonding();
663a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh    }
664a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh
665a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh    private void pairingAttempt(String address, int result) {
666a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh        // This happens when our initial guess of "0000" as the pass key
667a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh        // fails. Try to create the bond again and display the pin dialog
668a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh        // to the user. Use back-off while posting the delayed
669a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh        // message. The initial value is
670a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh        // INIT_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY and the max value is
671a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh        // MAX_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY. If the max value is
672a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh        // reached, display an error to the user.
673a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh        int attempt = mBondState.getAttempt(address);
674a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh        if (attempt * INIT_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY >
675a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh                    MAX_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY) {
676a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh            mBondState.clearPinAttempts(address);
677a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh            setBondState(address, BluetoothDevice.BOND_NONE, result);
678a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh            return;
679a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh        }
680a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh
681a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh        Message message = mHandler.obtainMessage(MESSAGE_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY);
682a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh        message.obj = address;
683a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh        boolean postResult =  mHandler.sendMessageDelayed(message,
684a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh                                        attempt * INIT_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY);
685a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh        if (!postResult) {
686a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh            mBondState.clearPinAttempts(address);
687a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh            setBondState(address,
688a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh                    BluetoothDevice.BOND_NONE, result);
689a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh            return;
690a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh        }
6919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6939a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    /*package*/ BluetoothDevice getRemoteDevice(String address) {
6949a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        return mAdapter.getRemoteDevice(address);
6959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static String toBondStateString(int bondState) {
6989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        switch (bondState) {
699005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly        case BluetoothDevice.BOND_NONE:
7009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return "not bonded";
7019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case BluetoothDevice.BOND_BONDING:
7029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return "bonding";
7039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case BluetoothDevice.BOND_BONDED:
7049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return "bonded";
7059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        default:
7069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return "??????";
7079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public synchronized boolean setName(String name) {
7119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
7129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                "Need BLUETOOTH_ADMIN permission");
7139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (name == null) {
7149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return false;
7159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
716d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        return setPropertyString("Name", name);
7179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
719d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    //TODO(): setPropertyString, setPropertyInteger, setPropertyBoolean
720d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    // Either have a single property function with Object as the parameter
721d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    // or have a function for each property and then obfuscate in the JNI layer.
722d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    // The following looks dirty.
723d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    private boolean setPropertyString(String key, String value) {
7249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
7258c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh        if (!isEnabledInternal()) return false;
726d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        return setAdapterPropertyStringNative(key, value);
7279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
729d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    private boolean setPropertyInteger(String key, int value) {
730d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
7318c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh        if (!isEnabledInternal()) return false;
732d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        return setAdapterPropertyIntegerNative(key, value);
733d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    }
7349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
735d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    private boolean setPropertyBoolean(String key, boolean value) {
736d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
7378c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh        if (!isEnabledInternal()) return false;
738d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        return setAdapterPropertyBooleanNative(key, value ? 1 : 0);
7399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
741d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    /**
742d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh     * Set the discoverability window for the device.  A timeout of zero
743d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh     * makes the device permanently discoverable (if the device is
744d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh     * discoverable).  Setting the timeout to a nonzero value does not make
745d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh     * a device discoverable; you need to call setMode() to make the device
746d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh     * explicitly discoverable.
747d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh     *
748f51eadaf1f83abfe16a609a4ded6d789494689b2Jake Hamby     * @param timeout The discoverable timeout in seconds.
749d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh     */
750d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    public synchronized boolean setDiscoverableTimeout(int timeout) {
7519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
7529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                "Need BLUETOOTH_ADMIN permission");
753d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        return setPropertyInteger("DiscoverableTimeout", timeout);
7549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
75612835478ee687a493d1b5882e67b6725bd539c26Nick Pelly    public synchronized boolean setScanMode(int mode, int duration) {
75718b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS,
75818b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly                                                "Need WRITE_SECURE_SETTINGS permission");
7599a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        boolean pairable;
7609a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        boolean discoverable;
76112835478ee687a493d1b5882e67b6725bd539c26Nick Pelly
762de893f550301a60274e87aa8168225e7a7a42184Nick Pelly        switch (mode) {
763de893f550301a60274e87aa8168225e7a7a42184Nick Pelly        case BluetoothAdapter.SCAN_MODE_NONE:
764d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh            pairable = false;
765d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh            discoverable = false;
766de893f550301a60274e87aa8168225e7a7a42184Nick Pelly            break;
767de893f550301a60274e87aa8168225e7a7a42184Nick Pelly        case BluetoothAdapter.SCAN_MODE_CONNECTABLE:
768d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh            pairable = true;
769d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh            discoverable = false;
770005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly            break;
771de893f550301a60274e87aa8168225e7a7a42184Nick Pelly        case BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE:
772d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh            pairable = true;
773d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh            discoverable = true;
77412835478ee687a493d1b5882e67b6725bd539c26Nick Pelly            if (DBG) Log.d(TAG, "BT Discoverable for " + duration + " seconds");
775005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly            break;
776de893f550301a60274e87aa8168225e7a7a42184Nick Pelly        default:
777de893f550301a60274e87aa8168225e7a7a42184Nick Pelly            Log.w(TAG, "Requested invalid scan mode " + mode);
778de893f550301a60274e87aa8168225e7a7a42184Nick Pelly            return false;
779d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        }
7809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
78114e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie        setPropertyBoolean("Discoverable", discoverable);
78214e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie        setPropertyBoolean("Pairable", pairable);
783d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        return true;
7849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
78614e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie    /**
78714e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie     * @param on true set the local Bluetooth module to be connectable
78814e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie     *                The dicoverability is recovered to what it was before
78914e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie     *                switchConnectable(false) call
79014e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie     *           false set the local Bluetooth module to be not connectable
79114e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie     *                 and not dicoverable
79214e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie     */
79314e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie    /*package*/ synchronized void switchConnectable(boolean on) {
79414e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie        setAdapterPropertyBooleanNative("Powered", on ? 1 : 0);
79514e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie    }
79614e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie
79714e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie    /*package*/ synchronized void setPairable() {
79814e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie        String pairableString = getProperty("Pairable", false);
79914e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie        if (pairableString == null) {
80014e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie            Log.e(TAG, "null pairableString");
80114e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie            return;
80214e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie        }
80314e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie        if (pairableString.equals("false")) {
80414e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie            setAdapterPropertyBooleanNative("Pairable", 1);
80514e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie        }
80614e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie    }
80714e48e91f6def5448db9994cb13ddcdc676cba53Matthew Xie
808a66590ed78d0ec8e837dab80a4376a10d7023e9aJaikumar Ganesh    /*package*/ synchronized String getProperty(String name, boolean checkState) {
809a66590ed78d0ec8e837dab80a4376a10d7023e9aJaikumar Ganesh        // If checkState is false, check if the event loop is running.
810a66590ed78d0ec8e837dab80a4376a10d7023e9aJaikumar Ganesh        // before making the call to Bluez
811a66590ed78d0ec8e837dab80a4376a10d7023e9aJaikumar Ganesh        if (checkState) {
812a66590ed78d0ec8e837dab80a4376a10d7023e9aJaikumar Ganesh            if (!isEnabledInternal()) return null;
813a66590ed78d0ec8e837dab80a4376a10d7023e9aJaikumar Ganesh        } else if (!mEventLoop.isEventLoopRunning()) {
814a66590ed78d0ec8e837dab80a4376a10d7023e9aJaikumar Ganesh            return null;
815a66590ed78d0ec8e837dab80a4376a10d7023e9aJaikumar Ganesh        }
816a66590ed78d0ec8e837dab80a4376a10d7023e9aJaikumar Ganesh
8179a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        return mAdapterProperties.getProperty(name);
818b148bc844e5eddb07bef2fd1b4b754716decb43eJaikumar Ganesh    }
819b148bc844e5eddb07bef2fd1b4b754716decb43eJaikumar Ganesh
8209a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    BluetoothAdapterProperties getAdapterProperties() {
8219a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        return mAdapterProperties;
8229a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    }
8239a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby
8249a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    BluetoothDeviceProperties getDeviceProperties() {
8259a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        return mDeviceProperties;
8269a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    }
8279a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby
8289a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    boolean isRemoteDeviceInCache(String address) {
8299a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        return mDeviceProperties.isInCache(address);
8309a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    }
8319a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby
8329a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    void setRemoteDeviceProperty(String address, String name, String value) {
8339a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        mDeviceProperties.setProperty(address, name, value);
8349a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    }
8359a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby
8369a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    void updateRemoteDevicePropertiesCache(String address) {
8379a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        mDeviceProperties.updateCache(address);
8389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
840d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    public synchronized String getAddress() {
841d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
842a66590ed78d0ec8e837dab80a4376a10d7023e9aJaikumar Ganesh        // Don't check state since we want to provide address, even if BT is off
843a66590ed78d0ec8e837dab80a4376a10d7023e9aJaikumar Ganesh        return getProperty("Address", false);
8449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
846d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    public synchronized String getName() {
8479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
848a66590ed78d0ec8e837dab80a4376a10d7023e9aJaikumar Ganesh        // Don't check state since we want to provide name, even if BT is off
849a66590ed78d0ec8e837dab80a4376a10d7023e9aJaikumar Ganesh        return getProperty("Name", false);
8509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
85258b93c36409c7fb91a644605f004b6d247d9b540Jaikumar Ganesh    public synchronized ParcelUuid[] getUuids() {
85358b93c36409c7fb91a644605f004b6d247d9b540Jaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
854a66590ed78d0ec8e837dab80a4376a10d7023e9aJaikumar Ganesh        String value =  getProperty("UUIDs", true);
85558b93c36409c7fb91a644605f004b6d247d9b540Jaikumar Ganesh        if (value == null) return null;
85650b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh        return convertStringToParcelUuid(value);
85750b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh    }
85858b93c36409c7fb91a644605f004b6d247d9b540Jaikumar Ganesh
85950b40cec9c43eeeb9389ba2a99bcffd160246132Jaikumar Ganesh    private synchronized ParcelUuid[] convertStringToParcelUuid(String value) {
86058b93c36409c7fb91a644605f004b6d247d9b540Jaikumar Ganesh        String[] uuidStrings = null;
86158b93c36409c7fb91a644605f004b6d247d9b540Jaikumar Ganesh        // The UUIDs are stored as a "," separated string.
86258b93c36409c7fb91a644605f004b6d247d9b540Jaikumar Ganesh        uuidStrings = value.split(",");
86358b93c36409c7fb91a644605f004b6d247d9b540Jaikumar Ganesh        ParcelUuid[] uuids = new ParcelUuid[uuidStrings.length];
86458b93c36409c7fb91a644605f004b6d247d9b540Jaikumar Ganesh
86558b93c36409c7fb91a644605f004b6d247d9b540Jaikumar Ganesh        for (int i = 0; i < uuidStrings.length; i++) {
86658b93c36409c7fb91a644605f004b6d247d9b540Jaikumar Ganesh            uuids[i] = ParcelUuid.fromString(uuidStrings[i]);
86758b93c36409c7fb91a644605f004b6d247d9b540Jaikumar Ganesh        }
86858b93c36409c7fb91a644605f004b6d247d9b540Jaikumar Ganesh        return uuids;
86958b93c36409c7fb91a644605f004b6d247d9b540Jaikumar Ganesh    }
87058b93c36409c7fb91a644605f004b6d247d9b540Jaikumar Ganesh
8719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
872d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh     * Returns the user-friendly name of a remote device.  This value is
873d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh     * returned from our local cache, which is updated when onPropertyChange
874d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh     * event is received.
875d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh     * Do not expect to retrieve the updated remote name immediately after
876d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh     * changing the name on the remote device.
8779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
878d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh     * @param address Bluetooth address of remote device.
879d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh     *
880d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh     * @return The user-friendly name of the specified remote device.
8819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
882d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    public synchronized String getRemoteName(String address) {
883d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
884005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly        if (!BluetoothAdapter.checkBluetoothAddress(address)) {
885d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh            return null;
886d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        }
8879a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        return mDeviceProperties.getProperty(address, "Name");
8889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
891269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie     * Returns alias of a remote device.  This value is returned from our
892269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie     * local cache, which is updated when onPropertyChange event is received.
893269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie     *
894269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie     * @param address Bluetooth address of remote device.
895269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie     *
896269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie     * @return The alias of the specified remote device.
897269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie     */
898269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie    public synchronized String getRemoteAlias(String address) {
899269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie
900269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
901269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie        if (!BluetoothAdapter.checkBluetoothAddress(address)) {
902269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie            return null;
903269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie        }
904269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie        return mDeviceProperties.getProperty(address, "Alias");
905269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie    }
906269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie
907269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie    /**
908269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie     * Set the alias of a remote device.
909269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie     *
910269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie     * @param address Bluetooth address of remote device.
911269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie     * @param alias new alias to change to
912269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie     * @return true on success, false on error
913269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie     */
914269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie    public synchronized boolean setRemoteAlias(String address, String alias) {
915269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
916269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie        if (!BluetoothAdapter.checkBluetoothAddress(address)) {
917269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie            return false;
918269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie        }
919269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie
920269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie        return setDevicePropertyStringNative(getObjectPathFromAddress(address),
921269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie                                             "Alias", alias);
922269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie    }
923269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie
924269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie    /**
9259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Get the discoverability window for the device.  A timeout of zero
9269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * means that the device is permanently discoverable (if the device is
9279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * in the discoverable mode).
9289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
9299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return The discoverability window of the device, in seconds.  A negative
9309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         value indicates an error.
9319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
9329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public synchronized int getDiscoverableTimeout() {
9339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
934a66590ed78d0ec8e837dab80a4376a10d7023e9aJaikumar Ganesh        String timeout = getProperty("DiscoverableTimeout", true);
935d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        if (timeout != null)
936d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh           return Integer.valueOf(timeout);
937d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        else
938d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh            return -1;
9399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
9409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public synchronized int getScanMode() {
9429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
9438c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh        if (!isEnabledInternal())
944de893f550301a60274e87aa8168225e7a7a42184Nick Pelly            return BluetoothAdapter.SCAN_MODE_NONE;
945d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh
946a66590ed78d0ec8e837dab80a4376a10d7023e9aJaikumar Ganesh        boolean pairable = getProperty("Pairable", true).equals("true");
947a66590ed78d0ec8e837dab80a4376a10d7023e9aJaikumar Ganesh        boolean discoverable = getProperty("Discoverable", true).equals("true");
948d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        return bluezStringToScanMode (pairable, discoverable);
9499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
9509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
951d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    public synchronized boolean startDiscovery() {
9529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
9539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                "Need BLUETOOTH_ADMIN permission");
9548c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh        if (!isEnabledInternal()) return false;
9558c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh
956d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        return startDiscoveryNative();
9579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
9589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
959d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    public synchronized boolean cancelDiscovery() {
9609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
9619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                "Need BLUETOOTH_ADMIN permission");
9628c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh        if (!isEnabledInternal()) return false;
9638c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh
964d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        return stopDiscoveryNative();
965d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    }
966d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh
967d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    public synchronized boolean isDiscovering() {
968d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
969d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh
970a66590ed78d0ec8e837dab80a4376a10d7023e9aJaikumar Ganesh        String discoveringProperty = getProperty("Discovering", false);
9718594394aadeaaa3f834d33d7b198fb071e0f31feMatthew Xie        if (discoveringProperty == null) {
9728594394aadeaaa3f834d33d7b198fb071e0f31feMatthew Xie            return false;
9738594394aadeaaa3f834d33d7b198fb071e0f31feMatthew Xie        }
9748594394aadeaaa3f834d33d7b198fb071e0f31feMatthew Xie
9758594394aadeaaa3f834d33d7b198fb071e0f31feMatthew Xie        return discoveringProperty.equals("true");
9769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
9779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
978cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh    private boolean isBondingFeasible(String address) {
9799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
9809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                "Need BLUETOOTH_ADMIN permission");
9818c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh        if (!isEnabledInternal()) return false;
9828c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh
983005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly        if (!BluetoothAdapter.checkBluetoothAddress(address)) {
9849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return false;
9859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
9869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        address = address.toUpperCase();
9879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9882092361d586a20190c9137fb3cc9434cdc9ec99fJaikumar Ganesh        if (mBondState.getPendingOutgoingBonding() != null) {
9899a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            Log.d(TAG, "Ignoring createBond(): another device is bonding");
9909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            // a different device is currently bonding, fail
9919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return false;
9929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
9939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // Check for bond state only if we are not performing auto
9959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // pairing exponential back-off attempts.
9969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (!mBondState.isAutoPairingAttemptsInProgress(address) &&
997005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly                mBondState.getBondState(address) != BluetoothDevice.BOND_NONE) {
9989a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            Log.d(TAG, "Ignoring createBond(): this device is already bonding or bonded");
9999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return false;
10009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10023fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh        if (address.equals(mDockAddress)) {
10033fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh            if (!writeDockPin()) {
10049a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                Log.e(TAG, "Error while writing Pin for the dock");
10053fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh                return false;
10063fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh            }
10073fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh        }
1008cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh        return true;
1009cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh    }
10103fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh
1011cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh    public synchronized boolean createBond(String address) {
1012cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh        if (!isBondingFeasible(address)) return false;
1013cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh
1014cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh        if (!createPairedDeviceNative(address, 60000  /*1 minute*/ )) {
10159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return false;
10169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10182092361d586a20190c9137fb3cc9434cdc9ec99fJaikumar Ganesh        mBondState.setPendingOutgoingBonding(address);
10199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mBondState.setBondState(address, BluetoothDevice.BOND_BONDING);
10202092361d586a20190c9137fb3cc9434cdc9ec99fJaikumar Ganesh
10219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return true;
10229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
10239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1024cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh    public synchronized boolean createBondOutOfBand(String address, byte[] hash,
1025cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh                                                    byte[] randomizer) {
1026cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh        if (!isBondingFeasible(address)) return false;
10273fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh
1028cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh        if (!createPairedDeviceOutOfBandNative(address, 60000 /* 1 minute */)) {
10299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return false;
10309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1032cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh        setDeviceOutOfBandData(address, hash, randomizer);
10332092361d586a20190c9137fb3cc9434cdc9ec99fJaikumar Ganesh        mBondState.setPendingOutgoingBonding(address);
10349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mBondState.setBondState(address, BluetoothDevice.BOND_BONDING);
10352092361d586a20190c9137fb3cc9434cdc9ec99fJaikumar Ganesh
10369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return true;
10379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
10389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1039cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh    public synchronized boolean setDeviceOutOfBandData(String address, byte[] hash,
1040cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh            byte[] randomizer) {
1041cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
1042cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh                                                "Need BLUETOOTH_ADMIN permission");
1043cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh        if (!isEnabledInternal()) return false;
1044cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh
1045cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh        Pair <byte[], byte[]> value = new Pair<byte[], byte[]>(hash, randomizer);
1046cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh
1047cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh        if (DBG) {
10489a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            Log.d(TAG, "Setting out of band data for: " + address + ":" +
10499a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                  Arrays.toString(hash) + ":" + Arrays.toString(randomizer));
1050cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh        }
1051cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh
1052cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh        mDeviceOobData.put(address, value);
1053cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh        return true;
1054cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh    }
1055cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh
1056cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh    Pair<byte[], byte[]> getDeviceOutOfBandData(BluetoothDevice device) {
1057cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh        return mDeviceOobData.get(device.getAddress());
1058cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh    }
1059cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh
1060cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh
1061cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh    public synchronized byte[] readOutOfBandData() {
1062cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
1063cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh                                                "Need BLUETOOTH permission");
1064cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh        if (!isEnabledInternal()) return null;
1065cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh
1066cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh        return readAdapterOutOfBandDataNative();
1067cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh    }
1068cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh
10699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public synchronized boolean cancelBondProcess(String address) {
10709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
10719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                "Need BLUETOOTH_ADMIN permission");
10728c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh        if (!isEnabledInternal()) return false;
10738c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh
1074005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly        if (!BluetoothAdapter.checkBluetoothAddress(address)) {
10759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return false;
10769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        address = address.toUpperCase();
10789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mBondState.getBondState(address) != BluetoothDevice.BOND_BONDING) {
10799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return false;
10809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1082005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly        mBondState.setBondState(address, BluetoothDevice.BOND_NONE,
10839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                BluetoothDevice.UNBOND_REASON_AUTH_CANCELED);
1084d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        cancelDeviceCreationNative(address);
10859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return true;
10869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
10879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public synchronized boolean removeBond(String address) {
10899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
10909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                "Need BLUETOOTH_ADMIN permission");
10918c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh        if (!isEnabledInternal()) return false;
10928c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh
1093005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly        if (!BluetoothAdapter.checkBluetoothAddress(address)) {
10949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return false;
10959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1096f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh        BluetoothDeviceProfileState state = mDeviceProfileState.get(address);
10979b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        if (state != null) {
1098f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh            state.sendMessage(BluetoothDeviceProfileState.UNPAIR);
10999b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh            return true;
11009b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        } else {
11019b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh            return false;
11029b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        }
11039b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh    }
11049b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh
11059b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh    public synchronized boolean removeBondInternal(String address) {
1106cc2c06656ceafbcd91ec5b679aa6c9eae0d9982cJaikumar Ganesh        // Unset the trusted device state and then unpair
1107cc2c06656ceafbcd91ec5b679aa6c9eae0d9982cJaikumar Ganesh        setTrust(address, false);
1108d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        return removeDeviceNative(getObjectPathFromAddress(address));
11099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
11109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public synchronized String[] listBonds() {
11129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
11139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return mBondState.listInState(BluetoothDevice.BOND_BONDED);
11149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
11159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1116a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh    /*package*/ synchronized String[] listInState(int state) {
1117a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh      return mBondState.listInState(state);
1118a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh    }
1119a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh
11209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public synchronized int getBondState(String address) {
11219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
1122005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly        if (!BluetoothAdapter.checkBluetoothAddress(address)) {
1123b24e11baac589fe16426f2d243b460ab84991c7bNick Pelly            return BluetoothDevice.ERROR;
11249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
11259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return mBondState.getBondState(address.toUpperCase());
11269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
11279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1128a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh    /*package*/ synchronized boolean setBondState(String address, int state) {
1129a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh        return setBondState(address, state, 0);
1130a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh    }
1131a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh
1132a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh    /*package*/ synchronized boolean setBondState(String address, int state, int reason) {
1133ae5fbb0b4363a2a399e92e310777d7a955a25370Arek Lichwa        mBondState.setBondState(address.toUpperCase(), state, reason);
1134a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh        return true;
1135a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh    }
1136a224f70b1efc29d9698da5b5c143251a43838f2bJaikumar Ganesh
11373fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh    public synchronized boolean isBluetoothDock(String address) {
11386e9c443460e40e9d663c117ba836585335e7c2c1Jaikumar Ganesh        SharedPreferences sp = mContext.getSharedPreferences(SHARED_PREFERENCES_NAME,
11399a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                Context.MODE_PRIVATE);
11406e9c443460e40e9d663c117ba836585335e7c2c1Jaikumar Ganesh
11416e9c443460e40e9d663c117ba836585335e7c2c1Jaikumar Ganesh        return sp.contains(SHARED_PREFERENCE_DOCK_ADDRESS + address);
11423fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh    }
11433fbf7b62bb48b10316353087d09cc3720af00642Jaikumar Ganesh
11449488cbd0b9ce0a9b49647910e76c6fa910f948eaJaikumar Ganesh    /*package*/ String[] getRemoteDeviceProperties(String address) {
11458c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh        if (!isEnabledInternal()) return null;
11468c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh
11479488cbd0b9ce0a9b49647910e76c6fa910f948eaJaikumar Ganesh        String objectPath = getObjectPathFromAddress(address);
11489488cbd0b9ce0a9b49647910e76c6fa910f948eaJaikumar Ganesh        return (String [])getDevicePropertiesNative(objectPath);
11499488cbd0b9ce0a9b49647910e76c6fa910f948eaJaikumar Ganesh    }
11509488cbd0b9ce0a9b49647910e76c6fa910f948eaJaikumar Ganesh
11519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1152efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue     * Sets the remote device trust state.
1153efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue     *
1154efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue     * @return boolean to indicate operation success or fail
1155efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue     */
1156efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue    public synchronized boolean setTrust(String address, boolean value) {
1157005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly        if (!BluetoothAdapter.checkBluetoothAddress(address)) {
1158e6ee3be1c254404dad842298f6f56c11cc6c7ac8Nick Pelly            mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
1159e6ee3be1c254404dad842298f6f56c11cc6c7ac8Nick Pelly                    "Need BLUETOOTH_ADMIN permission");
1160efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue            return false;
1161efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue        }
1162efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue
11638c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh        if (!isEnabledInternal()) return false;
11648c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh
11659a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        return setDevicePropertyBooleanNative(
11669a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                getObjectPathFromAddress(address), "Trusted", value ? 1 : 0);
1167efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue    }
1168efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue
1169efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue    /**
1170efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue     * Gets the remote device trust state as boolean.
1171efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue     * Note: this value may be
1172efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue     * retrieved from cache if we retrieved the data before *
1173efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue     *
11749a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby     * @return boolean to indicate trusted or untrusted state
1175efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue     */
1176efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue    public synchronized boolean getTrustState(String address) {
1177005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly        if (!BluetoothAdapter.checkBluetoothAddress(address)) {
1178efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue            mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
1179efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue            return false;
1180efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue        }
1181efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue
11829a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        String val = mDeviceProperties.getProperty(address, "Trusted");
1183efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue        if (val == null) {
1184efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue            return false;
1185efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue        } else {
11869a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            return val.equals("true");
1187efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue        }
1188efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue    }
1189efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue
1190efa1dd716da3372cc74a201d11de5e0ef1a9fe9aLixin Yue    /**
1191d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh     * Gets the remote major, minor classes encoded as a 32-bit
11929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * integer.
11939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
11949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Note: this value is retrieved from cache, because we get it during
11959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *       remote-device discovery.
11969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
11979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return 32-bit integer encoding the remote major, minor, and service
11989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         classes.
11999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
12009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public synchronized int getRemoteClass(String address) {
1201005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly        if (!BluetoothAdapter.checkBluetoothAddress(address)) {
1202ea600ccfb7568f60377c4abc85f56c80af7fdbfcNick Pelly            mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
1203ea600ccfb7568f60377c4abc85f56c80af7fdbfcNick Pelly            return BluetoothClass.ERROR;
12049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
12059a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        String val = mDeviceProperties.getProperty(address, "Class");
1206d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        if (val == null)
1207d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh            return BluetoothClass.ERROR;
1208d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        else {
1209d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh            return Integer.valueOf(val);
1210d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        }
12119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1212d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh
12139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1215dd0463aef18d251c741bfc9dc7a2787443ef36f1Jaikumar Ganesh     * Gets the UUIDs supported by the remote device
12169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
1217dd0463aef18d251c741bfc9dc7a2787443ef36f1Jaikumar Ganesh     * @return array of 128bit ParcelUuids
12189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1219dd0463aef18d251c741bfc9dc7a2787443ef36f1Jaikumar Ganesh    public synchronized ParcelUuid[] getRemoteUuids(String address) {
12209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
1221005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly        if (!BluetoothAdapter.checkBluetoothAddress(address)) {
12229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return null;
12239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
12241caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh        return getUuidFromCache(address);
12251caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh    }
12261caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh
12279a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    ParcelUuid[] getUuidFromCache(String address) {
12289a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        String value = mDeviceProperties.getProperty(address, "UUIDs");
1229dd0463aef18d251c741bfc9dc7a2787443ef36f1Jaikumar Ganesh        if (value == null) return null;
1230dd0463aef18d251c741bfc9dc7a2787443ef36f1Jaikumar Ganesh
1231dd0463aef18d251c741bfc9dc7a2787443ef36f1Jaikumar Ganesh        String[] uuidStrings = null;
1232d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        // The UUIDs are stored as a "," separated string.
1233dd0463aef18d251c741bfc9dc7a2787443ef36f1Jaikumar Ganesh        uuidStrings = value.split(",");
1234dd0463aef18d251c741bfc9dc7a2787443ef36f1Jaikumar Ganesh        ParcelUuid[] uuids = new ParcelUuid[uuidStrings.length];
1235dd0463aef18d251c741bfc9dc7a2787443ef36f1Jaikumar Ganesh
1236dd0463aef18d251c741bfc9dc7a2787443ef36f1Jaikumar Ganesh        for (int i = 0; i < uuidStrings.length; i++) {
1237dd0463aef18d251c741bfc9dc7a2787443ef36f1Jaikumar Ganesh            uuids[i] = ParcelUuid.fromString(uuidStrings[i]);
1238dd0463aef18d251c741bfc9dc7a2787443ef36f1Jaikumar Ganesh        }
1239d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        return uuids;
12409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
12419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
124216fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly    /**
124316fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly     * Connect and fetch new UUID's using SDP.
124416fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly     * The UUID's found are broadcast as intents.
124516fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly     * Optionally takes a uuid and callback to fetch the RFCOMM channel for the
124616fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly     * a given uuid.
124716fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly     * TODO: Don't wait UUID_INTENT_DELAY to broadcast UUID intents on success
124816fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly     * TODO: Don't wait UUID_INTENT_DELAY to handle the failure case for
124916fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly     * callback and broadcast intents.
125016fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly     */
125116fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly    public synchronized boolean fetchRemoteUuids(String address, ParcelUuid uuid,
125216fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly            IBluetoothCallback callback) {
12531caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
12548c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh        if (!isEnabledInternal()) return false;
12558c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh
12561caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh        if (!BluetoothAdapter.checkBluetoothAddress(address)) {
12571caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh            return false;
12581caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh        }
12591caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh
126016fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly        RemoteService service = new RemoteService(address, uuid);
126116fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly        if (uuid != null && mUuidCallbackTracker.get(service) != null) {
126216fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly            // An SDP query for this address & uuid is already in progress
126316fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly            // Do not add this callback for the uuid
126416fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly            return false;
126516fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly        }
126616fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly
12671caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh        if (mUuidIntentTracker.contains(address)) {
12681caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh            // An SDP query for this address is already in progress
126916fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly            // Add this uuid onto the in-progress SDP query
127016fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly            if (uuid != null) {
127116fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly                mUuidCallbackTracker.put(new RemoteService(address, uuid), callback);
127216fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly            }
12731caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh            return true;
12741caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh        }
12751caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh
1276421f0101087bbede1152ccc6d8212f6686a10e54Jaikumar Ganesh        // If the device is already created, we will
1277421f0101087bbede1152ccc6d8212f6686a10e54Jaikumar Ganesh        // do the SDP on the callback of createDeviceNative.
1278421f0101087bbede1152ccc6d8212f6686a10e54Jaikumar Ganesh        boolean ret= createDeviceNative(address);
12791caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh
12801caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh        mUuidIntentTracker.add(address);
128116fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly        if (uuid != null) {
128216fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly            mUuidCallbackTracker.put(new RemoteService(address, uuid), callback);
128316fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly        }
12841caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh
12851caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh        Message message = mHandler.obtainMessage(MESSAGE_UUID_INTENT);
12861caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh        message.obj = address;
12871caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh        mHandler.sendMessageDelayed(message, UUID_INTENT_DELAY);
12881caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh        return ret;
12891caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh    }
12901caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh
12919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1292d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh     * Gets the rfcomm channel associated with the UUID.
129316fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly     * Pulls records from the cache only.
12949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
1295d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh     * @param address Address of the remote device
1296dd0463aef18d251c741bfc9dc7a2787443ef36f1Jaikumar Ganesh     * @param uuid ParcelUuid of the service attribute
12979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
1298d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh     * @return rfcomm channel associated with the service attribute
129910eac971b3a6e5f34a420dd68ebfa796553ad2b9Jaikumar Ganesh     *         -1 on error
13009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1301dd0463aef18d251c741bfc9dc7a2787443ef36f1Jaikumar Ganesh    public int getRemoteServiceChannel(String address, ParcelUuid uuid) {
13029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
13038c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh        if (!isEnabledInternal()) return -1;
13048c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh
1305005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly        if (!BluetoothAdapter.checkBluetoothAddress(address)) {
1306b24e11baac589fe16426f2d243b460ab84991c7bNick Pelly            return BluetoothDevice.ERROR;
13079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
130810eac971b3a6e5f34a420dd68ebfa796553ad2b9Jaikumar Ganesh        // Check if we are recovering from a crash.
130910eac971b3a6e5f34a420dd68ebfa796553ad2b9Jaikumar Ganesh        if (mDeviceProperties.isEmpty()) {
13109a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            if (mDeviceProperties.updateCache(address) == null)
131110eac971b3a6e5f34a420dd68ebfa796553ad2b9Jaikumar Ganesh                return -1;
131210eac971b3a6e5f34a420dd68ebfa796553ad2b9Jaikumar Ganesh        }
131310eac971b3a6e5f34a420dd68ebfa796553ad2b9Jaikumar Ganesh
131410eac971b3a6e5f34a420dd68ebfa796553ad2b9Jaikumar Ganesh        Map<ParcelUuid, Integer> value = mDeviceServiceChannelCache.get(address);
131510eac971b3a6e5f34a420dd68ebfa796553ad2b9Jaikumar Ganesh        if (value != null && value.containsKey(uuid))
131610eac971b3a6e5f34a420dd68ebfa796553ad2b9Jaikumar Ganesh            return value.get(uuid);
131710eac971b3a6e5f34a420dd68ebfa796553ad2b9Jaikumar Ganesh        return -1;
13189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
13199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
13209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public synchronized boolean setPin(String address, byte[] pin) {
13219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
13229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                "Need BLUETOOTH_ADMIN permission");
13238c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh        if (!isEnabledInternal()) return false;
13248c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh
13259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (pin == null || pin.length <= 0 || pin.length > 16 ||
1326005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly            !BluetoothAdapter.checkBluetoothAddress(address)) {
13279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return false;
13289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
13299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        address = address.toUpperCase();
13309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        Integer data = mEventLoop.getPasskeyAgentRequestData().remove(address);
13319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (data == null) {
13329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Log.w(TAG, "setPin(" + address + ") called but no native data available, " +
13339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                  "ignoring. Maybe the PasskeyAgent Request was cancelled by the remote device" +
13349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                  " or by bluez.\n");
13359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return false;
13369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
13379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // bluez API wants pin as a string
13389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        String pinString;
13399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
13409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            pinString = new String(pin, "UTF8");
13419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (UnsupportedEncodingException uee) {
13429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Log.e(TAG, "UTF8 not supported?!?");
13439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return false;
13449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
13459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return setPinNative(address, pinString, data.intValue());
13469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
13479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1348b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh    public synchronized boolean setPasskey(String address, int passkey) {
1349b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
1350b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh                                                "Need BLUETOOTH_ADMIN permission");
13518c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh        if (!isEnabledInternal()) return false;
13528c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh
1353005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly        if (passkey < 0 || passkey > 999999 || !BluetoothAdapter.checkBluetoothAddress(address)) {
1354b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh            return false;
1355b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh        }
1356b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh        address = address.toUpperCase();
1357b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh        Integer data = mEventLoop.getPasskeyAgentRequestData().remove(address);
1358b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh        if (data == null) {
1359b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh            Log.w(TAG, "setPasskey(" + address + ") called but no native data available, " +
1360b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh                  "ignoring. Maybe the PasskeyAgent Request was cancelled by the remote device" +
1361b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh                  " or by bluez.\n");
1362b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh            return false;
1363b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh        }
1364b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh        return setPasskeyNative(address, passkey, data.intValue());
1365b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh    }
1366b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh
1367b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh    public synchronized boolean setPairingConfirmation(String address, boolean confirm) {
1368b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
1369b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh                                                "Need BLUETOOTH_ADMIN permission");
13708c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh        if (!isEnabledInternal()) return false;
13718c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh
1372b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh        address = address.toUpperCase();
1373b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh        Integer data = mEventLoop.getPasskeyAgentRequestData().remove(address);
1374b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh        if (data == null) {
1375b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh            Log.w(TAG, "setPasskey(" + address + ") called but no native data available, " +
1376b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh                  "ignoring. Maybe the PasskeyAgent Request was cancelled by the remote device" +
1377b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh                  " or by bluez.\n");
1378b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh            return false;
1379b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh        }
1380b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh        return setPairingConfirmationNative(address, confirm, data.intValue());
1381b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh    }
1382b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh
1383cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh    public synchronized boolean setRemoteOutOfBandData(String address) {
1384cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
1385cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh                                                "Need BLUETOOTH_ADMIN permission");
1386cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh        if (!isEnabledInternal()) return false;
1387cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh        address = address.toUpperCase();
1388cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh        Integer data = mEventLoop.getPasskeyAgentRequestData().remove(address);
1389cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh        if (data == null) {
1390cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh            Log.w(TAG, "setRemoteOobData(" + address + ") called but no native data available, " +
1391cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh                  "ignoring. Maybe the PasskeyAgent Request was cancelled by the remote device" +
1392cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh                  " or by bluez.\n");
1393cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh            return false;
1394cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh        }
1395cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh
1396cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh        Pair<byte[], byte[]> val = mDeviceOobData.get(address);
1397cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh        byte[] hash, randomizer;
1398cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh        if (val == null) {
1399cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh            // TODO: check what should be passed in this case.
1400cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh            hash = new byte[16];
1401cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh            randomizer = new byte[16];
1402cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh        } else {
1403cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh            hash = val.first;
1404cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh            randomizer = val.second;
1405cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh        }
1406cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh        return setRemoteOutOfBandDataNative(address, hash, randomizer, data.intValue());
1407cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh    }
1408cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh
1409b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh    public synchronized boolean cancelPairingUserInput(String address) {
14109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
14119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                "Need BLUETOOTH_ADMIN permission");
14128c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh        if (!isEnabledInternal()) return false;
14138c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh
1414005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly        if (!BluetoothAdapter.checkBluetoothAddress(address)) {
14159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return false;
14169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1417005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly        mBondState.setBondState(address, BluetoothDevice.BOND_NONE,
1418397d8f4f4829a45f4fe7a672cc395466bbc0f442Jaikumar Ganesh                BluetoothDevice.UNBOND_REASON_AUTH_CANCELED);
14199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        address = address.toUpperCase();
14209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        Integer data = mEventLoop.getPasskeyAgentRequestData().remove(address);
14219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (data == null) {
1422b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh            Log.w(TAG, "cancelUserInputNative(" + address + ") called but no native data " +
1423b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh                "available, ignoring. Maybe the PasskeyAgent Request was already cancelled " +
1424b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh                "by the remote or by bluez.\n");
14259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return false;
14269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1427b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh        return cancelPairingUserInputNative(address, data.intValue());
14289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
14299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
14308c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh    /*package*/ void updateDeviceServiceChannelCache(String address) {
14319a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        if (DBG) Log.d(TAG, "updateDeviceServiceChannelCache(" + address + ")");
14329a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby
143310eac971b3a6e5f34a420dd68ebfa796553ad2b9Jaikumar Ganesh        // We are storing the rfcomm channel numbers only for the uuids
143410eac971b3a6e5f34a420dd68ebfa796553ad2b9Jaikumar Ganesh        // we are interested in.
14359a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        ParcelUuid[] deviceUuids = getRemoteUuids(address);
143616fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly
14379a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        ArrayList<ParcelUuid> applicationUuids = new ArrayList<ParcelUuid>();
143816fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly
143916fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly        synchronized (this) {
144016fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly            for (RemoteService service : mUuidCallbackTracker.keySet()) {
144116fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly                if (service.address.equals(address)) {
144216fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly                    applicationUuids.add(service.uuid);
144316fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly                }
144416fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly            }
144516fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly        }
144610eac971b3a6e5f34a420dd68ebfa796553ad2b9Jaikumar Ganesh
14479a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        Map <ParcelUuid, Integer> uuidToChannelMap = new HashMap<ParcelUuid, Integer>();
144816fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly
144916fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly        // Retrieve RFCOMM channel for default uuids
145016fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly        for (ParcelUuid uuid : RFCOMM_UUIDS) {
145110eac971b3a6e5f34a420dd68ebfa796553ad2b9Jaikumar Ganesh            if (BluetoothUuid.isUuidPresent(deviceUuids, uuid)) {
14529a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                int channel = getDeviceServiceChannelForUuid(address, uuid);
14539a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                uuidToChannelMap.put(uuid, channel);
14549a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                if (DBG) Log.d(TAG, "\tuuid(system): " + uuid + " " + channel);
145510eac971b3a6e5f34a420dd68ebfa796553ad2b9Jaikumar Ganesh            }
145610eac971b3a6e5f34a420dd68ebfa796553ad2b9Jaikumar Ganesh        }
145716fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly        // Retrieve RFCOMM channel for application requested uuids
145816fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly        for (ParcelUuid uuid : applicationUuids) {
145916fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly            if (BluetoothUuid.isUuidPresent(deviceUuids, uuid)) {
14609a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                int channel = getDeviceServiceChannelForUuid(address, uuid);
14619a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                uuidToChannelMap.put(uuid, channel);
14629a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                if (DBG) Log.d(TAG, "\tuuid(application): " + uuid + " " + channel);
146316fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly            }
146416fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly        }
146516fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly
146616fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly        synchronized (this) {
146716fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly            // Make application callbacks
146816fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly            for (Iterator<RemoteService> iter = mUuidCallbackTracker.keySet().iterator();
146916fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly                    iter.hasNext();) {
147016fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly                RemoteService service = iter.next();
147116fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly                if (service.address.equals(address)) {
14729a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                    if (uuidToChannelMap.containsKey(service.uuid)) {
14739a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                        int channel = uuidToChannelMap.get(service.uuid);
14749a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby
14759a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                        if (DBG) Log.d(TAG, "Making callback for " + service.uuid +
14769a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                                    " with result " + channel);
147716fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly                        IBluetoothCallback callback = mUuidCallbackTracker.get(service);
147816fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly                        if (callback != null) {
147916fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly                            try {
148016fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly                                callback.onRfcommChannelFound(channel);
148116fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly                            } catch (RemoteException e) {Log.e(TAG, "", e);}
148216fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly                        }
148316fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly
148416fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly                        iter.remove();
148516fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly                    }
148616fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly                }
148716fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly            }
148816fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly
148916fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly            // Update cache
14909a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            mDeviceServiceChannelCache.put(address, uuidToChannelMap);
149116fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly        }
149210eac971b3a6e5f34a420dd68ebfa796553ad2b9Jaikumar Ganesh    }
149310eac971b3a6e5f34a420dd68ebfa796553ad2b9Jaikumar Ganesh
14949a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    private int getDeviceServiceChannelForUuid(String address,
14959a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            ParcelUuid uuid) {
14969a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        return getDeviceServiceChannelNative(getObjectPathFromAddress(address),
14979a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                uuid.toString(), 0x0004);
14989a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    }
14999a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby
150024bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly    /**
150124bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly     * b is a handle to a Binder instance, so that this service can be notified
150224bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly     * for Applications that terminate unexpectedly, to clean there service
150324bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly     * records
150424bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly     */
150524bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly    public synchronized int addRfcommServiceRecord(String serviceName, ParcelUuid uuid,
150624bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            int channel, IBinder b) {
15078c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
15088c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh        if (!isEnabledInternal()) return -1;
15098c9dd7d8b99e1b064fc064a3c6737eaf179eae68Jaikumar Ganesh
151024bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        if (serviceName == null || uuid == null || channel < 1 ||
151124bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly                channel > BluetoothSocket.MAX_RFCOMM_CHANNEL) {
151224bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            return -1;
151324bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        }
151424bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        if (BluetoothUuid.isUuidPresent(BluetoothUuid.RESERVED_UUIDS, uuid)) {
151524bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            Log.w(TAG, "Attempted to register a reserved UUID: " + uuid);
151624bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            return -1;
151724bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        }
151824bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        int handle = addRfcommServiceRecordNative(serviceName,
151924bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly                uuid.getUuid().getMostSignificantBits(), uuid.getUuid().getLeastSignificantBits(),
152024bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly                (short)channel);
15219a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        if (DBG) Log.d(TAG, "new handle " + Integer.toHexString(handle));
152224bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        if (handle == -1) {
152324bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            return -1;
152424bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        }
152524bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly
152624bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        int pid = Binder.getCallingPid();
1527f1754050ceaadb4603aaaa0b1f0b193a305834afMatthew Xie        mServiceRecordToPid.put(new Integer(handle), new Pair<Integer, IBinder>(pid, b));
152824bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        try {
1529ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh            b.linkToDeath(new Reaper(handle, pid, RFCOMM_RECORD_REAPER), 0);
1530f1754050ceaadb4603aaaa0b1f0b193a305834afMatthew Xie        } catch (RemoteException e) {Log.e(TAG, "", e);}
153124bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        return handle;
153224bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly    }
153324bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly
153424bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly    public void removeServiceRecord(int handle) {
153524bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
153624bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly                                                "Need BLUETOOTH permission");
1537dcbc97fc28f4eb5910acc11dfac5efcd597c737dMatthew Xie        // Since this is a binder call check if Bluetooth is off
1538dcbc97fc28f4eb5910acc11dfac5efcd597c737dMatthew Xie        if (getBluetoothStateInternal() == BluetoothAdapter.STATE_OFF) return;
1539484867a8ed3d46fbb8df493c77f2e2d46a0db654Matthew Xie        Message message = mHandler.obtainMessage(MESSAGE_REMOVE_SERVICE_RECORD);
1540484867a8ed3d46fbb8df493c77f2e2d46a0db654Matthew Xie        message.obj = new Pair<Integer, Integer>(handle, Binder.getCallingPid());
1541484867a8ed3d46fbb8df493c77f2e2d46a0db654Matthew Xie        mHandler.sendMessage(message);
154224bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly    }
154324bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly
154424bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly    private synchronized void checkAndRemoveRecord(int handle, int pid) {
1545f1754050ceaadb4603aaaa0b1f0b193a305834afMatthew Xie        Pair<Integer, IBinder> pidPair = mServiceRecordToPid.get(handle);
1546dcbc97fc28f4eb5910acc11dfac5efcd597c737dMatthew Xie        if (pidPair != null && pid == pidPair.first) {
15479a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            if (DBG) Log.d(TAG, "Removing service record " +
15489a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                Integer.toHexString(handle) + " for pid " + pid);
1549f1754050ceaadb4603aaaa0b1f0b193a305834afMatthew Xie            mServiceRecordToPid.remove(handle);
155024bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            removeServiceRecordNative(handle);
155124bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        }
155224bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly    }
155324bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly
155424bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly    private class Reaper implements IBinder.DeathRecipient {
1555ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh        int mPid;
1556ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh        int mHandle;
1557ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh        int mType;
1558ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh
1559ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh        Reaper(int handle, int pid, int type) {
1560ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh            mPid = pid;
1561ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh            mHandle = handle;
1562ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh            mType = type;
156324bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        }
1564ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh
1565ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh        Reaper(int pid, int type) {
1566ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh            mPid = pid;
1567ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh            mType = type;
1568ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh        }
1569ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh
1570ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh        @Override
157124bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        public void binderDied() {
157224bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            synchronized (BluetoothService.this) {
1573ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh                if (DBG) Log.d(TAG, "Tracked app " + mPid + " died" + "Type:" + mType);
1574ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh                if (mType == RFCOMM_RECORD_REAPER) {
1575ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh                    checkAndRemoveRecord(mHandle, mPid);
1576ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh                } else if (mType == STATE_CHANGE_REAPER) {
1577ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh                    mStateChangeTracker.remove(mPid);
1578ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh                }
1579ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh            }
1580ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh        }
1581ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh    }
1582ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh
1583ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh
1584ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh    @Override
1585ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh    public boolean changeApplicationBluetoothState(boolean on,
1586ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh            IBluetoothStateChangeCallback callback, IBinder binder) {
1587f5fb6c8ce36a98da47f32f4142d372bd5f650bfaJaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
1588f5fb6c8ce36a98da47f32f4142d372bd5f650bfaJaikumar Ganesh
1589ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh        int pid = Binder.getCallingPid();
1590ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh        //mStateChangeTracker is a synchronized map
1591ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh        if (!mStateChangeTracker.containsKey(pid)) {
1592ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh            if (on) {
1593ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh                mStateChangeTracker.put(pid, callback);
1594ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh            } else {
1595ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh                return false;
159624bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            }
1597ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh        } else if (!on) {
1598ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh            mStateChangeTracker.remove(pid);
159924bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        }
1600ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh
1601ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh        if (binder != null) {
1602ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh            try {
1603ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh                binder.linkToDeath(new Reaper(pid, STATE_CHANGE_REAPER), 0);
1604ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh            } catch (RemoteException e) {
1605f1754050ceaadb4603aaaa0b1f0b193a305834afMatthew Xie                Log.e(TAG, "", e);
1606ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh                return false;
1607ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh            }
1608ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh        }
1609ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh
1610ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh        int type;
1611ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh        if (on) {
1612ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh            type = BluetoothAdapterStateMachine.PER_PROCESS_TURN_ON;
1613ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh        } else {
1614ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh            type = BluetoothAdapterStateMachine.PER_PROCESS_TURN_OFF;
1615ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh        }
1616ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh
1617ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh        mBluetoothState.sendMessage(type, callback);
1618ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh        return true;
1619ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh    }
1620ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh
1621ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh    boolean isApplicationStateChangeTrackerEmpty() {
1622ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh        return mStateChangeTracker.isEmpty();
1623ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh    }
1624ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh
1625ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh    void clearApplicationStateChangeTracker() {
1626ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh        mStateChangeTracker.clear();
1627ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh    }
1628ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh
1629ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh    Collection<IBluetoothStateChangeCallback> getApplicationStateChangeCallbacks() {
1630ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh        return mStateChangeTracker.values();
1631ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh    }
1632ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh
1633ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh    int getNumberOfApplicationStateChangeTrackers() {
1634ef2cb7c93a99096799d415e721dda46d1bf7a005Jaikumar Ganesh        return mStateChangeTracker.size();
163524bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly    }
163624bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly
16379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
16389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        @Override
16399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public void onReceive(Context context, Intent intent) {
16406e9c443460e40e9d663c117ba836585335e7c2c1Jaikumar Ganesh            if (intent == null) return;
16416e9c443460e40e9d663c117ba836585335e7c2c1Jaikumar Ganesh
16429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            String action = intent.getAction();
16439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (action.equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)) {
16449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                ContentResolver resolver = context.getContentResolver();
16459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                // Query the airplane mode from Settings.System just to make sure that
16469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                // some random app is not sending this intent and disabling bluetooth
16477f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie                if (isAirplaneModeOn()) {
16487f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie                    mBluetoothState.sendMessage(BluetoothAdapterStateMachine.AIRPLANE_MODE_ON);
16497f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie                } else {
16507f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie                    mBluetoothState.sendMessage(BluetoothAdapterStateMachine.AIRPLANE_MODE_OFF);
16519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
16526e9c443460e40e9d663c117ba836585335e7c2c1Jaikumar Ganesh            } else if (Intent.ACTION_DOCK_EVENT.equals(action)) {
16536e9c443460e40e9d663c117ba836585335e7c2c1Jaikumar Ganesh                int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
16546e9c443460e40e9d663c117ba836585335e7c2c1Jaikumar Ganesh                        Intent.EXTRA_DOCK_STATE_UNDOCKED);
16556e9c443460e40e9d663c117ba836585335e7c2c1Jaikumar Ganesh                if (DBG) Log.v(TAG, "Received ACTION_DOCK_EVENT with State:" + state);
16566e9c443460e40e9d663c117ba836585335e7c2c1Jaikumar Ganesh                if (state == Intent.EXTRA_DOCK_STATE_UNDOCKED) {
16576e9c443460e40e9d663c117ba836585335e7c2c1Jaikumar Ganesh                    mDockAddress = null;
16586e9c443460e40e9d663c117ba836585335e7c2c1Jaikumar Ganesh                    mDockPin = null;
16596e9c443460e40e9d663c117ba836585335e7c2c1Jaikumar Ganesh                } else {
16606e9c443460e40e9d663c117ba836585335e7c2c1Jaikumar Ganesh                    SharedPreferences.Editor editor =
16616e9c443460e40e9d663c117ba836585335e7c2c1Jaikumar Ganesh                        mContext.getSharedPreferences(SHARED_PREFERENCES_NAME,
16626e9c443460e40e9d663c117ba836585335e7c2c1Jaikumar Ganesh                                mContext.MODE_PRIVATE).edit();
16636e9c443460e40e9d663c117ba836585335e7c2c1Jaikumar Ganesh                    editor.putBoolean(SHARED_PREFERENCE_DOCK_ADDRESS + mDockAddress, true);
166466fce5068a8a3aeb28aaf713843891b286a75280Brad Fitzpatrick                    editor.apply();
16656e9c443460e40e9d663c117ba836585335e7c2c1Jaikumar Ganesh                }
16669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
16679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
16689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    };
16699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
16706e9c443460e40e9d663c117ba836585335e7c2c1Jaikumar Ganesh    private void registerForAirplaneMode(IntentFilter filter) {
167144303922f14ac71b446a6e50e1180be4c8fed2c7Jeff Sharkey        final ContentResolver resolver = mContext.getContentResolver();
167244303922f14ac71b446a6e50e1180be4c8fed2c7Jeff Sharkey        final String airplaneModeRadios = Settings.System.getString(resolver,
16739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                Settings.System.AIRPLANE_MODE_RADIOS);
167444303922f14ac71b446a6e50e1180be4c8fed2c7Jeff Sharkey        final String toggleableRadios = Settings.System.getString(resolver,
167544303922f14ac71b446a6e50e1180be4c8fed2c7Jeff Sharkey                Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
167644303922f14ac71b446a6e50e1180be4c8fed2c7Jeff Sharkey
167744303922f14ac71b446a6e50e1180be4c8fed2c7Jeff Sharkey        mIsAirplaneSensitive = airplaneModeRadios == null ? true :
167844303922f14ac71b446a6e50e1180be4c8fed2c7Jeff Sharkey                airplaneModeRadios.contains(Settings.System.RADIO_BLUETOOTH);
167944303922f14ac71b446a6e50e1180be4c8fed2c7Jeff Sharkey        mIsAirplaneToggleable = toggleableRadios == null ? false :
168044303922f14ac71b446a6e50e1180be4c8fed2c7Jeff Sharkey                toggleableRadios.contains(Settings.System.RADIO_BLUETOOTH);
168144303922f14ac71b446a6e50e1180be4c8fed2c7Jeff Sharkey
16829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mIsAirplaneSensitive) {
16836e9c443460e40e9d663c117ba836585335e7c2c1Jaikumar Ganesh            filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
16849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
16859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
16869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
16879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /* Returns true if airplane mode is currently on */
16889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private final boolean isAirplaneModeOn() {
16899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return Settings.System.getInt(mContext.getContentResolver(),
16909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                Settings.System.AIRPLANE_MODE_ON, 0) == 1;
16919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
16929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
16931caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh    /* Broadcast the Uuid intent */
16941caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh    /*package*/ synchronized void sendUuidIntent(String address) {
16956179965e85ec17b836084a4a3d7963d8a7a1e1ccJaikumar Ganesh        ParcelUuid[] uuid = getUuidFromCache(address);
16966179965e85ec17b836084a4a3d7963d8a7a1e1ccJaikumar Ganesh        Intent intent = new Intent(BluetoothDevice.ACTION_UUID);
16972d3b98d868cda30535505b2a2fba47aa1c9c052bJaikumar Ganesh        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mAdapter.getRemoteDevice(address));
16986179965e85ec17b836084a4a3d7963d8a7a1e1ccJaikumar Ganesh        intent.putExtra(BluetoothDevice.EXTRA_UUID, uuid);
16996179965e85ec17b836084a4a3d7963d8a7a1e1ccJaikumar Ganesh        mContext.sendBroadcast(intent, BLUETOOTH_ADMIN_PERM);
17009a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        mUuidIntentTracker.remove(address);
170116fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly    }
170216fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly
170316fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly    /*package*/ synchronized void makeServiceChannelCallbacks(String address) {
170416fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly        for (Iterator<RemoteService> iter = mUuidCallbackTracker.keySet().iterator();
170516fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly                iter.hasNext();) {
170616fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly            RemoteService service = iter.next();
170716fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly            if (service.address.equals(address)) {
17089a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                if (DBG) Log.d(TAG, "Cleaning up failed UUID channel lookup: "
17099a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                    + service.address + " " + service.uuid);
171016fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly                IBluetoothCallback callback = mUuidCallbackTracker.get(service);
171116fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly                if (callback != null) {
171216fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly                    try {
171316fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly                        callback.onRfcommChannelFound(-1);
171416fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly                    } catch (RemoteException e) {Log.e(TAG, "", e);}
171516fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly                }
171616fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly
171716fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly                iter.remove();
171816fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly            }
171916fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly        }
17201caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh    }
17211caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh
17229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    @Override
17239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
17247f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie        if (getBluetoothStateInternal() != BluetoothAdapter.STATE_ON) {
1725105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project            return;
1726105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project        }
1727105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project
172824bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        pw.println("mIsAirplaneSensitive = " + mIsAirplaneSensitive);
172944303922f14ac71b446a6e50e1180be4c8fed2c7Jeff Sharkey        pw.println("mIsAirplaneToggleable = " + mIsAirplaneToggleable);
173024bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly
173124bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        pw.println("Local address = " + getAddress());
173224bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        pw.println("Local name = " + getName());
173324bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        pw.println("isDiscovering() = " + isDiscovering());
1734105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project
173596a79830ea1ae3ab3d6d3cce2bd1397fcd40ea0eJaikumar Ganesh        mAdapter.getProfileProxy(mContext,
173696a79830ea1ae3ab3d6d3cce2bd1397fcd40ea0eJaikumar Ganesh                                 mBluetoothProfileServiceListener, BluetoothProfile.HEADSET);
17374ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh        mAdapter.getProfileProxy(mContext,
17384ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh                mBluetoothProfileServiceListener, BluetoothProfile.INPUT_DEVICE);
173974ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh        mAdapter.getProfileProxy(mContext,
174074ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh                mBluetoothProfileServiceListener, BluetoothProfile.PAN);
1741105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project
17429a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        dumpKnownDevices(pw);
17439a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        dumpAclConnectedDevices(pw);
17449a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        dumpHeadsetService(pw);
17454ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh        dumpInputDeviceProfile(pw);
174674ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh        dumpPanProfile(pw);
17479a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        dumpApplicationServiceRecords(pw);
1748cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        dumpProfileState(pw);
1749cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh    }
1750cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh
1751cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh    private void dumpProfileState(PrintWriter pw) {
1752cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        pw.println("\n--Profile State dump--");
1753cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        pw.println("\n Headset profile state:" +
1754cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh                mAdapter.getProfileConnectionState(BluetoothProfile.HEADSET));
1755cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        pw.println("\n A2dp profile state:" +
1756cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh                mAdapter.getProfileConnectionState(BluetoothProfile.A2DP));
1757cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        pw.println("\n HID profile state:" +
1758cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh                mAdapter.getProfileConnectionState(BluetoothProfile.INPUT_DEVICE));
1759cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        pw.println("\n PAN profile state:" +
1760cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh                mAdapter.getProfileConnectionState(BluetoothProfile.PAN));
17614ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh    }
17624ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh
17639a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    private void dumpHeadsetService(PrintWriter pw) {
1764105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project        pw.println("\n--Headset Service--");
176596a79830ea1ae3ab3d6d3cce2bd1397fcd40ea0eJaikumar Ganesh        if (mBluetoothHeadset != null) {
1766bb0773fac888c6748cbf778ab5c99c7d0a2c1309Jaikumar Ganesh            List<BluetoothDevice> deviceList = mBluetoothHeadset.getConnectedDevices();
1767bb0773fac888c6748cbf778ab5c99c7d0a2c1309Jaikumar Ganesh            if (deviceList.size() == 0) {
176874ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh                pw.println("No headsets connected");
1769bb0773fac888c6748cbf778ab5c99c7d0a2c1309Jaikumar Ganesh            } else {
1770bb0773fac888c6748cbf778ab5c99c7d0a2c1309Jaikumar Ganesh                BluetoothDevice device = deviceList.get(0);
17719a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                pw.println("\ngetConnectedDevices[0] = " + device);
17729a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                dumpHeadsetConnectionState(pw, device);
1773bb0773fac888c6748cbf778ab5c99c7d0a2c1309Jaikumar Ganesh                pw.println("getBatteryUsageHint() = " +
1774bb0773fac888c6748cbf778ab5c99c7d0a2c1309Jaikumar Ganesh                             mBluetoothHeadset.getBatteryUsageHint(device));
177596a79830ea1ae3ab3d6d3cce2bd1397fcd40ea0eJaikumar Ganesh            }
177696a79830ea1ae3ab3d6d3cce2bd1397fcd40ea0eJaikumar Ganesh
17775a1e4cf83f5be1b5d79e2643fa791aa269b6a4bcJaikumar Ganesh            deviceList.clear();
17785a1e4cf83f5be1b5d79e2643fa791aa269b6a4bcJaikumar Ganesh            deviceList = mBluetoothHeadset.getDevicesMatchingConnectionStates(new int[] {
177996a79830ea1ae3ab3d6d3cce2bd1397fcd40ea0eJaikumar Ganesh                     BluetoothProfile.STATE_CONNECTED, BluetoothProfile.STATE_DISCONNECTED});
178074ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh            pw.println("--Connected and Disconnected Headsets");
1781bb0773fac888c6748cbf778ab5c99c7d0a2c1309Jaikumar Ganesh            for (BluetoothDevice device: deviceList) {
178296a79830ea1ae3ab3d6d3cce2bd1397fcd40ea0eJaikumar Ganesh                pw.println(device);
178396a79830ea1ae3ab3d6d3cce2bd1397fcd40ea0eJaikumar Ganesh                if (mBluetoothHeadset.isAudioConnected(device)) {
178496a79830ea1ae3ab3d6d3cce2bd1397fcd40ea0eJaikumar Ganesh                    pw.println("SCO audio connected to device:" + device);
178596a79830ea1ae3ab3d6d3cce2bd1397fcd40ea0eJaikumar Ganesh                }
178696a79830ea1ae3ab3d6d3cce2bd1397fcd40ea0eJaikumar Ganesh            }
1787105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project        }
17884ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh        mAdapter.closeProfileProxy(BluetoothProfile.HEADSET, mBluetoothHeadset);
17894ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh    }
17906c901db72dbaf57d8fdf26adae6721de14ecae22Nick Pelly
17914ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh    private void dumpInputDeviceProfile(PrintWriter pw) {
17924ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh        pw.println("\n--Bluetooth Service- Input Device Profile");
17934ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh        if (mInputDevice != null) {
17944ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh            List<BluetoothDevice> deviceList = mInputDevice.getConnectedDevices();
17954ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh            if (deviceList.size() == 0) {
179674ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh                pw.println("No input devices connected");
17974ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh            } else {
179874ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh                pw.println("Number of connected devices:" + deviceList.size());
17994ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh                BluetoothDevice device = deviceList.get(0);
18004ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh                pw.println("getConnectedDevices[0] = " + device);
18014ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh                pw.println("Priority of Connected device = " + mInputDevice.getPriority(device));
18024ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh
18034ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh                switch (mInputDevice.getConnectionState(device)) {
18044ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh                    case BluetoothInputDevice.STATE_CONNECTING:
18054ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh                        pw.println("getConnectionState() = STATE_CONNECTING");
18064ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh                        break;
18074ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh                    case BluetoothInputDevice.STATE_CONNECTED:
18084ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh                        pw.println("getConnectionState() = STATE_CONNECTED");
18094ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh                        break;
18104ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh                    case BluetoothInputDevice.STATE_DISCONNECTING:
18114ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh                        pw.println("getConnectionState() = STATE_DISCONNECTING");
18124ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh                        break;
18134ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh                }
18144ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh            }
18154ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh            deviceList.clear();
18164ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh            deviceList = mInputDevice.getDevicesMatchingConnectionStates(new int[] {
18174ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh                     BluetoothProfile.STATE_CONNECTED, BluetoothProfile.STATE_DISCONNECTED});
18184ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh            pw.println("--Connected and Disconnected input devices");
18194ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh            for (BluetoothDevice device: deviceList) {
18204ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh                pw.println(device);
18214ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh            }
182224bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        }
18234ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh        mAdapter.closeProfileProxy(BluetoothProfile.INPUT_DEVICE, mBluetoothHeadset);
18249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
18259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
182674ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh    private void dumpPanProfile(PrintWriter pw) {
182774ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh        pw.println("\n--Bluetooth Service- Pan Profile");
182874ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh        if (mPan != null) {
182974ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh            List<BluetoothDevice> deviceList = mPan.getConnectedDevices();
183074ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh            if (deviceList.size() == 0) {
183174ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh                pw.println("No Pan devices connected");
183274ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh            } else {
183374ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh                pw.println("Number of connected devices:" + deviceList.size());
183474ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh                BluetoothDevice device = deviceList.get(0);
183574ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh                pw.println("getConnectedDevices[0] = " + device);
183674ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh
183774ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh                switch (mPan.getConnectionState(device)) {
183874ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh                    case BluetoothInputDevice.STATE_CONNECTING:
183974ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh                        pw.println("getConnectionState() = STATE_CONNECTING");
184074ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh                        break;
184174ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh                    case BluetoothInputDevice.STATE_CONNECTED:
184274ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh                        pw.println("getConnectionState() = STATE_CONNECTED");
184374ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh                        break;
184474ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh                    case BluetoothInputDevice.STATE_DISCONNECTING:
184574ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh                        pw.println("getConnectionState() = STATE_DISCONNECTING");
184674ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh                        break;
184774ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh                }
184874ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh            }
184974ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh            deviceList.clear();
185074ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh            deviceList = mPan.getDevicesMatchingConnectionStates(new int[] {
185174ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh                     BluetoothProfile.STATE_CONNECTED, BluetoothProfile.STATE_DISCONNECTED});
185274ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh            pw.println("--Connected and Disconnected Pan devices");
185374ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh            for (BluetoothDevice device: deviceList) {
185474ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh                pw.println(device);
185574ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh            }
185674ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh        }
18579a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    }
18589a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby
18599a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    private void dumpHeadsetConnectionState(PrintWriter pw,
18609a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            BluetoothDevice device) {
18619a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        switch (mBluetoothHeadset.getConnectionState(device)) {
18629a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            case BluetoothHeadset.STATE_CONNECTING:
18639a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                pw.println("getConnectionState() = STATE_CONNECTING");
18649a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                break;
18659a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            case BluetoothHeadset.STATE_CONNECTED:
18669a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                pw.println("getConnectionState() = STATE_CONNECTED");
18679a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                break;
18689a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            case BluetoothHeadset.STATE_DISCONNECTING:
18699a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                pw.println("getConnectionState() = STATE_DISCONNECTING");
18709a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                break;
18719a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            case BluetoothHeadset.STATE_AUDIO_CONNECTED:
18729a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                pw.println("getConnectionState() = STATE_AUDIO_CONNECTED");
18739a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                break;
18749a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        }
18759a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    }
18769a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby
18779a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    private void dumpApplicationServiceRecords(PrintWriter pw) {
18789a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        pw.println("\n--Application Service Records--");
18799a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        for (Integer handle : mServiceRecordToPid.keySet()) {
1880f1754050ceaadb4603aaaa0b1f0b193a305834afMatthew Xie            Integer pid = mServiceRecordToPid.get(handle).first;
18819a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            pw.println("\tpid " + pid + " handle " + Integer.toHexString(handle));
18829a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        }
188374ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh        mAdapter.closeProfileProxy(BluetoothProfile.PAN, mBluetoothHeadset);
188474ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh    }
188574ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh
18869a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    private void dumpAclConnectedDevices(PrintWriter pw) {
18879a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        String[] devicesObjectPath = getKnownDevices();
18889a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        pw.println("\n--ACL connected devices--");
18899a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        if (devicesObjectPath != null) {
18909a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            for (String device : devicesObjectPath) {
18919a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                pw.println(getAddressFromObjectPath(device));
18929a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            }
18939a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        }
18949a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    }
18959a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby
18969a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    private void dumpKnownDevices(PrintWriter pw) {
18979a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        pw.println("\n--Known devices--");
18989a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        for (String address : mDeviceProperties.keySet()) {
18999a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            int bondState = mBondState.getBondState(address);
19009a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            pw.printf("%s %10s (%d) %s\n", address,
19019a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                       toBondStateString(bondState),
19029a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                       mBondState.getAttempt(address),
19039a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                       getRemoteName(address));
19049a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby
19059a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            Map<ParcelUuid, Integer> uuidChannels = mDeviceServiceChannelCache.get(address);
19069a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            if (uuidChannels == null) {
19079a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                pw.println("\tuuids = null");
19089a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            } else {
19099a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                for (ParcelUuid uuid : uuidChannels.keySet()) {
19109a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                    Integer channel = uuidChannels.get(uuid);
19119a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                    if (channel == null) {
19129a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                        pw.println("\t" + uuid);
19139a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                    } else {
19149a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                        pw.println("\t" + uuid + " RFCOMM channel = " + channel);
19159a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                    }
19169a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                }
19179a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            }
19189a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            for (RemoteService service : mUuidCallbackTracker.keySet()) {
19199a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                if (service.address.equals(address)) {
19209a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                    pw.println("\tPENDING CALLBACK: " + service.uuid);
19219a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                }
19229a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            }
19239a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        }
19249a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    }
19259a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby
192696a79830ea1ae3ab3d6d3cce2bd1397fcd40ea0eJaikumar Ganesh    private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener =
192796a79830ea1ae3ab3d6d3cce2bd1397fcd40ea0eJaikumar Ganesh        new BluetoothProfile.ServiceListener() {
192896a79830ea1ae3ab3d6d3cce2bd1397fcd40ea0eJaikumar Ganesh        public void onServiceConnected(int profile, BluetoothProfile proxy) {
19294ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh            if (profile == BluetoothProfile.HEADSET) {
19304ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh                mBluetoothHeadset = (BluetoothHeadset) proxy;
19314ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh            } else if (profile == BluetoothProfile.INPUT_DEVICE) {
19324ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh                mInputDevice = (BluetoothInputDevice) proxy;
193374ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh            } else if (profile == BluetoothProfile.PAN) {
193474ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh                mPan = (BluetoothPan) proxy;
19354ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh            }
19364ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh        }
193796a79830ea1ae3ab3d6d3cce2bd1397fcd40ea0eJaikumar Ganesh        public void onServiceDisconnected(int profile) {
19384ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh            if (profile == BluetoothProfile.HEADSET) {
19394ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh                mBluetoothHeadset = null;
19404ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh            } else if (profile == BluetoothProfile.INPUT_DEVICE) {
19414ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh                mInputDevice = null;
194274ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh            } else if (profile == BluetoothProfile.PAN) {
194374ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh                mPan = null;
19444ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh            }
194596a79830ea1ae3ab3d6d3cce2bd1397fcd40ea0eJaikumar Ganesh        }
194696a79830ea1ae3ab3d6d3cce2bd1397fcd40ea0eJaikumar Ganesh    };
194796a79830ea1ae3ab3d6d3cce2bd1397fcd40ea0eJaikumar Ganesh
1948d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    /* package */ static int bluezStringToScanMode(boolean pairable, boolean discoverable) {
1949d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        if (pairable && discoverable)
1950bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly            return BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE;
1951d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        else if (pairable && !discoverable)
1952bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly            return BluetoothAdapter.SCAN_MODE_CONNECTABLE;
1953d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        else
1954bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly            return BluetoothAdapter.SCAN_MODE_NONE;
19559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
19569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
19579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /* package */ static String scanModeToBluezString(int mode) {
19589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        switch (mode) {
1959bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        case BluetoothAdapter.SCAN_MODE_NONE:
19609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return "off";
1961bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        case BluetoothAdapter.SCAN_MODE_CONNECTABLE:
19629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return "connectable";
1963bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        case BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE:
19649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return "discoverable";
19659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
19669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return null;
19679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
19689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1969d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    /*package*/ String getAddressFromObjectPath(String objectPath) {
19709a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        String adapterObjectPath = mAdapterProperties.getObjectPath();
1971d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        if (adapterObjectPath == null || objectPath == null) {
1972f51eadaf1f83abfe16a609a4ded6d789494689b2Jake Hamby            Log.e(TAG, "getAddressFromObjectPath: AdapterObjectPath:" + adapterObjectPath +
1973d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh                    "  or deviceObjectPath:" + objectPath + " is null");
1974d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh            return null;
1975d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        }
1976d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        if (!objectPath.startsWith(adapterObjectPath)) {
1977f51eadaf1f83abfe16a609a4ded6d789494689b2Jake Hamby            Log.e(TAG, "getAddressFromObjectPath: AdapterObjectPath:" + adapterObjectPath +
1978d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh                    "  is not a prefix of deviceObjectPath:" + objectPath +
1979d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh                    "bluetoothd crashed ?");
1980d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh            return null;
1981d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        }
1982d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        String address = objectPath.substring(adapterObjectPath.length());
1983d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        if (address != null) return address.replace('_', ':');
1984d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh
1985d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        Log.e(TAG, "getAddressFromObjectPath: Address being returned is null");
1986d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        return null;
1987d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    }
1988d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh
1989d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    /*package*/ String getObjectPathFromAddress(String address) {
19909a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        String path = mAdapterProperties.getObjectPath();
1991d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        if (path == null) {
1992d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh            Log.e(TAG, "Error: Object Path is null");
1993d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh            return null;
1994d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        }
1995d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        path = path + address.replace(":", "_");
1996d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh        return path;
1997d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    }
1998d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh
1999b7e029d03c115ed65cdea9b2bba307e882c308e2Jaikumar Ganesh    /*package */ void setLinkTimeout(String address, int num_slots) {
2000b7e029d03c115ed65cdea9b2bba307e882c308e2Jaikumar Ganesh        String path = getObjectPathFromAddress(address);
2001b7e029d03c115ed65cdea9b2bba307e882c308e2Jaikumar Ganesh        boolean result = setLinkTimeoutNative(path, num_slots);
2002b7e029d03c115ed65cdea9b2bba307e882c308e2Jaikumar Ganesh
20039a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        if (!result) Log.d(TAG, "Set Link Timeout to " + num_slots + " slots failed");
2004b7e029d03c115ed65cdea9b2bba307e882c308e2Jaikumar Ganesh    }
2005b7e029d03c115ed65cdea9b2bba307e882c308e2Jaikumar Ganesh
200630b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh    /**** Handlers for PAN  Profile ****/
20076003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh    // TODO: This needs to be converted to a state machine.
200830b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh
20096003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh    public boolean isTetheringOn() {
201030b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
20116003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        synchronized (mBluetoothPanProfileHandler) {
20126003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh            return mBluetoothPanProfileHandler.isTetheringOn();
20136003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        }
201430b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh    }
201530b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh
20166003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh    /*package*/boolean allowIncomingTethering() {
20176003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        synchronized (mBluetoothPanProfileHandler) {
20186003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh            return mBluetoothPanProfileHandler.allowIncomingTethering();
20196003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        }
202030b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh    }
202130b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh
20226003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh    public void setBluetoothTethering(boolean value) {
202330b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
20246003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        synchronized (mBluetoothPanProfileHandler) {
20256003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh            mBluetoothPanProfileHandler.setBluetoothTethering(value);
20266003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        }
202730b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh    }
202830b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh
20296003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh    public int getPanDeviceConnectionState(BluetoothDevice device) {
203030b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
20316003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        synchronized (mBluetoothPanProfileHandler) {
20326003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh            return mBluetoothPanProfileHandler.getPanDeviceConnectionState(device);
20336003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        }
203430b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh    }
203530b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh
20366003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh    public boolean connectPanDevice(BluetoothDevice device) {
203730b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
203830b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh            "Need BLUETOOTH_ADMIN permission");
20396003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        synchronized (mBluetoothPanProfileHandler) {
20406003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh            return mBluetoothPanProfileHandler.connectPanDevice(device);
20416003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        }
204230b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh    }
204330b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh
20446003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh    public List<BluetoothDevice> getConnectedPanDevices() {
204530b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
20466003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        synchronized (mBluetoothPanProfileHandler) {
20476003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh            return mBluetoothPanProfileHandler.getConnectedPanDevices();
20486003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        }
204930b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh    }
205030b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh
20516003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh    public List<BluetoothDevice> getPanDevicesMatchingConnectionStates(
205274ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh            int[] states) {
205374ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
20546003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        synchronized (mBluetoothPanProfileHandler) {
20556003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh            return mBluetoothPanProfileHandler.getPanDevicesMatchingConnectionStates(states);
20566003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        }
205774ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh    }
205874ef1199459629c5dd9f272f8cd706d82cdfeeb1Jaikumar Ganesh
20596003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh    public boolean disconnectPanDevice(BluetoothDevice device) {
206030b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
206130b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh            "Need BLUETOOTH_ADMIN permission");
20626003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        synchronized (mBluetoothPanProfileHandler) {
20636003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh            return mBluetoothPanProfileHandler.disconnectPanDevice(device);
20646003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        }
206530b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh    }
206630b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh
20676003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh    /*package*/void handlePanDeviceStateChange(BluetoothDevice device,
206830b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh                                                             String iface,
206930b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh                                                             int state,
207030b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh                                                             int role) {
20716003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        synchronized (mBluetoothPanProfileHandler) {
20726003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh            mBluetoothPanProfileHandler.handlePanDeviceStateChange(device, iface, state, role);
20736003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        }
207430b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh    }
207530b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh
20766003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh    /*package*/void handlePanDeviceStateChange(BluetoothDevice device,
207730b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh                                                             int state, int role) {
20786003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        synchronized (mBluetoothPanProfileHandler) {
20796003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh            mBluetoothPanProfileHandler.handlePanDeviceStateChange(device, null, state, role);
20806003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        }
208130b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh    }
208230b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh
208330b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh    /**** Handlers for Input Device Profile ****/
20846003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh    // This needs to be converted to state machine
208530b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh
20866003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh    public boolean connectInputDevice(BluetoothDevice device) {
208730b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
208830b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh                                                "Need BLUETOOTH_ADMIN permission");
208930b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh        BluetoothDeviceProfileState state = mDeviceProfileState.get(device.getAddress());
20906003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        synchronized (mBluetoothInputProfileHandler) {
20916003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh            return mBluetoothInputProfileHandler.connectInputDevice(device, state);
20926003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        }
209330b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh    }
209430b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh
20956003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh    public boolean connectInputDeviceInternal(BluetoothDevice device) {
20966003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        synchronized (mBluetoothInputProfileHandler) {
20976003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh            return mBluetoothInputProfileHandler.connectInputDeviceInternal(device);
20986003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        }
209930b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh    }
210030b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh
21016003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh    public boolean disconnectInputDevice(BluetoothDevice device) {
210230b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
210330b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh                                                "Need BLUETOOTH_ADMIN permission");
210430b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh        BluetoothDeviceProfileState state = mDeviceProfileState.get(device.getAddress());
21056003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        synchronized (mBluetoothInputProfileHandler) {
21066003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh            return mBluetoothInputProfileHandler.disconnectInputDevice(device, state);
21076003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        }
210830b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh    }
210930b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh
21106003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh    public boolean disconnectInputDeviceInternal(BluetoothDevice device) {
21116003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        synchronized (mBluetoothInputProfileHandler) {
21126003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh            return mBluetoothInputProfileHandler.disconnectInputDeviceInternal(device);
21136003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        }
211430b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh    }
211530b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh
21166003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh    public int getInputDeviceConnectionState(BluetoothDevice device) {
211730b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
21186003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        synchronized (mBluetoothInputProfileHandler) {
21196003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh            return mBluetoothInputProfileHandler.getInputDeviceConnectionState(device);
21206003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        }
212130b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh    }
212230b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh
21236003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh    public List<BluetoothDevice> getConnectedInputDevices() {
212430b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
21256003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        synchronized (mBluetoothInputProfileHandler) {
21266003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh            return mBluetoothInputProfileHandler.getConnectedInputDevices();
21276003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        }
212830b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh    }
212930b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh
21306003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh    public List<BluetoothDevice> getInputDevicesMatchingConnectionStates(
21314ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh            int[] states) {
21324ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
21336003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        synchronized (mBluetoothInputProfileHandler) {
21346003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh            return mBluetoothInputProfileHandler.getInputDevicesMatchingConnectionStates(states);
21356003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        }
21364ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh    }
21374ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh
21384ab0e7746fe74a9e4d75d374f73b7af87420b2f6Jaikumar Ganesh
21396003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh    public int getInputDevicePriority(BluetoothDevice device) {
214030b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
21416003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        synchronized (mBluetoothInputProfileHandler) {
21426003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh            return mBluetoothInputProfileHandler.getInputDevicePriority(device);
21436003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        }
214430b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh    }
214530b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh
21466003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh    public boolean setInputDevicePriority(BluetoothDevice device, int priority) {
214730b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
214830b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh                                                "Need BLUETOOTH_ADMIN permission");
21496003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        synchronized (mBluetoothInputProfileHandler) {
21506003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh            return mBluetoothInputProfileHandler.setInputDevicePriority(device, priority);
21516003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        }
215230b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh    }
215330b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh
2154bbd86750570ff75e428b3810dc2e65db558c14b2Jaikumar Ganesh    /**
2155bbd86750570ff75e428b3810dc2e65db558c14b2Jaikumar Ganesh     * Handle incoming profile acceptance for profiles handled by Bluetooth Service,
2156bbd86750570ff75e428b3810dc2e65db558c14b2Jaikumar Ganesh     * currently PAN and HID. This also is the catch all for all rejections for profiles
2157bbd86750570ff75e428b3810dc2e65db558c14b2Jaikumar Ganesh     * that is not supported.
2158bbd86750570ff75e428b3810dc2e65db558c14b2Jaikumar Ganesh     *
2159bbd86750570ff75e428b3810dc2e65db558c14b2Jaikumar Ganesh     * @param device - Bluetooth Device
2160bbd86750570ff75e428b3810dc2e65db558c14b2Jaikumar Ganesh     * @param allow - true / false
2161bbd86750570ff75e428b3810dc2e65db558c14b2Jaikumar Ganesh     * @return
2162bbd86750570ff75e428b3810dc2e65db558c14b2Jaikumar Ganesh     */
2163bbd86750570ff75e428b3810dc2e65db558c14b2Jaikumar Ganesh    public boolean allowIncomingProfileConnect(BluetoothDevice device, boolean allow) {
2164a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
2165a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                                                "Need BLUETOOTH_ADMIN permission");
2166a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        String address = device.getAddress();
2167a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        if (!BluetoothAdapter.checkBluetoothAddress(address)) {
2168a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie            return false;
2169a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        }
2170a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie
2171a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        Integer data = getAuthorizationAgentRequestData(address);
2172a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        if (data == null) {
2173bbd86750570ff75e428b3810dc2e65db558c14b2Jaikumar Ganesh            Log.w(TAG, "allowIncomingProfileConnect(" + device +
2174a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                  ") called but no native data available");
2175a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie            return false;
2176a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        }
2177bbd86750570ff75e428b3810dc2e65db558c14b2Jaikumar Ganesh        if (DBG) log("allowIncomingProfileConnect: " + device + " : " + allow + " : " + data);
2178a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        return setAuthorizationNative(address, allow, data.intValue());
2179a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie    }
2180a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie
21816003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh    /*package*/List<BluetoothDevice> lookupInputDevicesMatchingStates(int[] states) {
21826003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        synchronized (mBluetoothInputProfileHandler) {
21836003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh            return mBluetoothInputProfileHandler.lookupInputDevicesMatchingStates(states);
21846003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        }
218530b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh    }
218630b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh
21876003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh    /*package*/void handleInputDevicePropertyChange(String address, boolean connected) {
21886003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        synchronized (mBluetoothInputProfileHandler) {
21896003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh            mBluetoothInputProfileHandler.handleInputDevicePropertyChange(address, connected);
21906003fe9af08b134485ebac61ee312528325f6c3cJaikumar Ganesh        }
219130b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh    }
219230b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh
21932ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh    /**** Handlers for Health Device Profile ****/
21942ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh    // TODO: All these need to be converted to a state machine.
21952ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh
2196fb658c72a3a76dac334c39070d1501a2575c1069Jaikumar Ganesh    public boolean registerAppConfiguration(BluetoothHealthAppConfiguration config,
2197fb658c72a3a76dac334c39070d1501a2575c1069Jaikumar Ganesh                                            IBluetoothHealthCallback callback) {
21982ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
21992ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh                "Need BLUETOOTH permission");
22002ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh        synchronized (mBluetoothHealthProfileHandler) {
2201fb658c72a3a76dac334c39070d1501a2575c1069Jaikumar Ganesh                return mBluetoothHealthProfileHandler.registerAppConfiguration(config, callback);
22022ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh        }
22032ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh    }
22042ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh
22052ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh    public boolean unregisterAppConfiguration(BluetoothHealthAppConfiguration config) {
22062ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
22072ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh                "Need BLUETOOTH permission");
22082ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh        synchronized (mBluetoothHealthProfileHandler) {
22092ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh                return mBluetoothHealthProfileHandler.unregisterAppConfiguration(config);
22102ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh        }
22112ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh    }
22122ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh
22132ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh
22142ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh    public boolean connectChannelToSource(BluetoothDevice device,
22152ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh            BluetoothHealthAppConfiguration config) {
22162ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
22172ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh                "Need BLUETOOTH permission");
22182ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh        synchronized (mBluetoothHealthProfileHandler) {
22192ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh            return mBluetoothHealthProfileHandler.connectChannelToSource(device,
22202ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh                    config);
22212ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh        }
22222ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh    }
22232ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh
22242ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh    public boolean connectChannelToSink(BluetoothDevice device,
22252ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh            BluetoothHealthAppConfiguration config, int channelType) {
22262ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
22272ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh                                                "Need BLUETOOTH permission");
22282ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh        synchronized (mBluetoothHealthProfileHandler) {
22292ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh            return mBluetoothHealthProfileHandler.connectChannel(device, config,
22302ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh                    channelType);
22312ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh        }
22322ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh    }
22332ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh
22342ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh    public boolean disconnectChannel(BluetoothDevice device,
2235eb9d34630f74d0260690287f2df57c0cd3d7ba1dJaikumar Ganesh            BluetoothHealthAppConfiguration config, int id) {
22362ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
22372ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh                "Need BLUETOOTH permission");
22382ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh        synchronized (mBluetoothHealthProfileHandler) {
2239eb9d34630f74d0260690287f2df57c0cd3d7ba1dJaikumar Ganesh            return mBluetoothHealthProfileHandler.disconnectChannel(device, config, id);
22402ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh        }
22412ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh    }
22422ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh
22432ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh    public ParcelFileDescriptor getMainChannelFd(BluetoothDevice device,
22442ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh            BluetoothHealthAppConfiguration config) {
22452ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
22462ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh                "Need BLUETOOTH permission");
22472ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh        synchronized (mBluetoothHealthProfileHandler) {
22482ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh            return mBluetoothHealthProfileHandler.getMainChannelFd(device, config);
22492ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh        }
22502ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh    }
22512ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh
22522ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh    /*package*/ void onHealthDevicePropertyChanged(String devicePath,
22532ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh            String channelPath) {
22542ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh        synchronized (mBluetoothHealthProfileHandler) {
22552ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh            mBluetoothHealthProfileHandler.onHealthDevicePropertyChanged(devicePath,
22562ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh                    channelPath);
22572ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh        }
22582ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh    }
22592ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh
22602ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh    /*package*/ void onHealthDeviceChannelChanged(String devicePath,
22612ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh            String channelPath, boolean exists) {
22622ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh        synchronized(mBluetoothHealthProfileHandler) {
22632ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh            mBluetoothHealthProfileHandler.onHealthDeviceChannelChanged(devicePath,
22642ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh                    channelPath, exists);
22652ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh        }
22662ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh    }
22672ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh
2268b5d2d4526cd2c0117b7a33b1297ac683c37ac5c7Jaikumar Ganesh    /*package*/ void onHealthDeviceChannelConnectionError(int channelCode,
2269b5d2d4526cd2c0117b7a33b1297ac683c37ac5c7Jaikumar Ganesh            int newState) {
2270b5d2d4526cd2c0117b7a33b1297ac683c37ac5c7Jaikumar Ganesh        synchronized(mBluetoothHealthProfileHandler) {
2271b5d2d4526cd2c0117b7a33b1297ac683c37ac5c7Jaikumar Ganesh            mBluetoothHealthProfileHandler.onHealthDeviceChannelConnectionError(channelCode,
2272b5d2d4526cd2c0117b7a33b1297ac683c37ac5c7Jaikumar Ganesh                                                                                newState);
2273b5d2d4526cd2c0117b7a33b1297ac683c37ac5c7Jaikumar Ganesh        }
2274b5d2d4526cd2c0117b7a33b1297ac683c37ac5c7Jaikumar Ganesh    }
2275b5d2d4526cd2c0117b7a33b1297ac683c37ac5c7Jaikumar Ganesh
22762ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh    public int getHealthDeviceConnectionState(BluetoothDevice device) {
22772ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
22782ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh                "Need BLUETOOTH permission");
22792ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh        synchronized (mBluetoothHealthProfileHandler) {
22802ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh            return mBluetoothHealthProfileHandler.getHealthDeviceConnectionState(device);
22812ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh        }
22822ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh    }
22832ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh
22842ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh    public List<BluetoothDevice> getConnectedHealthDevices() {
22852ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
22862ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh                "Need BLUETOOTH permission");
22872ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh        synchronized (mBluetoothHealthProfileHandler) {
22882ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh            return mBluetoothHealthProfileHandler.getConnectedHealthDevices();
22892ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh        }
22902ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh    }
22912ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh
22922ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh    public List<BluetoothDevice> getHealthDevicesMatchingConnectionStates(
22932ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh            int[] states) {
22942ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
22952ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh                "Need BLUETOOTH permission");
22962ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh        synchronized (mBluetoothHealthProfileHandler) {
22972ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh            return mBluetoothHealthProfileHandler.
22982ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh                    getHealthDevicesMatchingConnectionStates(states);
22992ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh        }
23002ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh    }
23012ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh
2302a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie    /*package*/boolean notifyIncomingHidConnection(String address) {
2303a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        BluetoothDeviceProfileState state = mDeviceProfileState.get(address);
2304a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        if (state == null) {
2305a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie            return false;
2306a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        }
2307a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        Message msg = new Message();
2308a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        msg.what = BluetoothDeviceProfileState.CONNECT_HID_INCOMING;
2309a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        state.sendMessage(msg);
2310a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        return true;
2311a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie    }
2312a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie
23139b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh    public boolean connectHeadset(String address) {
231496a79830ea1ae3ab3d6d3cce2bd1397fcd40ea0eJaikumar Ganesh        if (getBondState(address) != BluetoothDevice.BOND_BONDED) return false;
231596a79830ea1ae3ab3d6d3cce2bd1397fcd40ea0eJaikumar Ganesh
2316f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh        BluetoothDeviceProfileState state = mDeviceProfileState.get(address);
23179b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        if (state != null) {
2318f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh            Message msg = new Message();
2319f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh            msg.arg1 = BluetoothDeviceProfileState.CONNECT_HFP_OUTGOING;
2320f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh            msg.obj = state;
2321f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh            mHfpProfileState.sendMessage(msg);
2322f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh            return true;
23239b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        }
23249b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        return false;
23259b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh    }
23269b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh
23279b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh    public boolean disconnectHeadset(String address) {
232896a79830ea1ae3ab3d6d3cce2bd1397fcd40ea0eJaikumar Ganesh        if (getBondState(address) != BluetoothDevice.BOND_BONDED) return false;
232996a79830ea1ae3ab3d6d3cce2bd1397fcd40ea0eJaikumar Ganesh
2330f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh        BluetoothDeviceProfileState state = mDeviceProfileState.get(address);
23319b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        if (state != null) {
2332f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh            Message msg = new Message();
2333f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh            msg.arg1 = BluetoothDeviceProfileState.DISCONNECT_HFP_OUTGOING;
2334f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh            msg.obj = state;
2335f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh            mHfpProfileState.sendMessage(msg);
23369b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh            return true;
23379b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        }
23389b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        return false;
23399b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh    }
23409b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh
23419b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh    public boolean connectSink(String address) {
234296a79830ea1ae3ab3d6d3cce2bd1397fcd40ea0eJaikumar Ganesh        if (getBondState(address) != BluetoothDevice.BOND_BONDED) return false;
234396a79830ea1ae3ab3d6d3cce2bd1397fcd40ea0eJaikumar Ganesh
2344f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh        BluetoothDeviceProfileState state = mDeviceProfileState.get(address);
23459b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        if (state != null) {
2346f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh            Message msg = new Message();
2347f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh            msg.arg1 = BluetoothDeviceProfileState.CONNECT_A2DP_OUTGOING;
2348f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh            msg.obj = state;
2349f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh            mA2dpProfileState.sendMessage(msg);
2350f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh            return true;
23519b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        }
23529b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        return false;
23539b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh    }
23549b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh
23559b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh    public boolean disconnectSink(String address) {
235696a79830ea1ae3ab3d6d3cce2bd1397fcd40ea0eJaikumar Ganesh        if (getBondState(address) != BluetoothDevice.BOND_BONDED) return false;
235796a79830ea1ae3ab3d6d3cce2bd1397fcd40ea0eJaikumar Ganesh
2358f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh        BluetoothDeviceProfileState state = mDeviceProfileState.get(address);
23599b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        if (state != null) {
2360f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh            Message msg = new Message();
2361f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh            msg.arg1 = BluetoothDeviceProfileState.DISCONNECT_A2DP_OUTGOING;
2362f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh            msg.obj = state;
2363f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh            mA2dpProfileState.sendMessage(msg);
23649b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh            return true;
23659b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        }
23669b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        return false;
23679b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh    }
23689b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh
2369d3728cb32fbd0a85bc77f4bcbdfea386ede2c75fJaikumar Ganesh    BluetoothDeviceProfileState addProfileState(String address, boolean setTrust) {
2370f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh        BluetoothDeviceProfileState state = mDeviceProfileState.get(address);
23719b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        if (state != null) return state;
23729b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh
2373d3728cb32fbd0a85bc77f4bcbdfea386ede2c75fJaikumar Ganesh        state = new BluetoothDeviceProfileState(mContext, address, this, mA2dpService, setTrust);
2374f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh        mDeviceProfileState.put(address, state);
23759b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        state.start();
23769b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        return state;
23779b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh    }
23789b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh
23799a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    void removeProfileState(String address) {
2380f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh        mDeviceProfileState.remove(address);
23819b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh    }
23829b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh
23839a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    synchronized String[] getKnownDevices() {
23849a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        String[] bonds = null;
2385a66590ed78d0ec8e837dab80a4376a10d7023e9aJaikumar Ganesh        String val = getProperty("Devices", true);
23869a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        if (val != null) {
23879a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            bonds = val.split(",");
23889a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        }
23899a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        return bonds;
23909a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    }
23919a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby
23929b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh    private void initProfileState() {
23939a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        String[] bonds = null;
2394a66590ed78d0ec8e837dab80a4376a10d7023e9aJaikumar Ganesh        String val = getProperty("Devices", false);
23959b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        if (val != null) {
23969b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh            bonds = val.split(",");
23979b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        }
23989b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        if (bonds == null) {
23999b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh            return;
24009b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        }
24019b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        for (String path : bonds) {
24029b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh            String address = getAddressFromObjectPath(path);
2403d3728cb32fbd0a85bc77f4bcbdfea386ede2c75fJaikumar Ganesh            BluetoothDeviceProfileState state = addProfileState(address, false);
24049a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        }
24059a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    }
24069a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby
24079a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    private void autoConnect() {
24089a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        String[] bonds = getKnownDevices();
24099a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        if (bonds == null) {
24109a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            return;
24119a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        }
24129a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        for (String path : bonds) {
24139a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            String address = getAddressFromObjectPath(path);
24149a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            BluetoothDeviceProfileState state = mDeviceProfileState.get(address);
24159a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            if (state != null) {
24169a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                Message msg = new Message();
24179a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                msg.what = BluetoothDeviceProfileState.AUTO_CONNECT_PROFILES;
24189a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                state.sendMessage(msg);
24199a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            }
24209b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        }
24219b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh    }
24229b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh
24239b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh    public boolean notifyIncomingConnection(String address) {
2424f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh        BluetoothDeviceProfileState state =
2425f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh             mDeviceProfileState.get(address);
24269b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        if (state != null) {
24279b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh            Message msg = new Message();
2428f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh            msg.what = BluetoothDeviceProfileState.CONNECT_HFP_INCOMING;
24299b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh            state.sendMessage(msg);
24309b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh            return true;
24319b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        }
24329b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        return false;
24339b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh    }
24349b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh
24359b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh    /*package*/ boolean notifyIncomingA2dpConnection(String address) {
2436f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh       BluetoothDeviceProfileState state =
2437f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh            mDeviceProfileState.get(address);
24389b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh       if (state != null) {
24399b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh           Message msg = new Message();
2440f1048cdb68d4e4671be2060ca31a3adfc613e88eJaikumar Ganesh           msg.what = BluetoothDeviceProfileState.CONNECT_A2DP_INCOMING;
24419b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh           state.sendMessage(msg);
24429b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh           return true;
24439b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh       }
24449b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh       return false;
24459b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh    }
24469b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh
24479b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh    /*package*/ void setA2dpService(BluetoothA2dpService a2dpService) {
24489b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        mA2dpService = a2dpService;
24499b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh    }
24509b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh
2451a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie    /*package*/ Integer getAuthorizationAgentRequestData(String address) {
2452a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        Integer data = mEventLoop.getAuthorizationAgentRequestData().remove(address);
2453a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        return data;
2454a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie    }
2455a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie
245670a053bf1ba331d727e2fbfca8f39d96b3b324b4Jaikumar Ganesh    public void sendProfileStateMessage(int profile, int cmd) {
245770a053bf1ba331d727e2fbfca8f39d96b3b324b4Jaikumar Ganesh        Message msg = new Message();
245870a053bf1ba331d727e2fbfca8f39d96b3b324b4Jaikumar Ganesh        msg.what = cmd;
245970a053bf1ba331d727e2fbfca8f39d96b3b324b4Jaikumar Ganesh        if (profile == BluetoothProfileState.HFP) {
246070a053bf1ba331d727e2fbfca8f39d96b3b324b4Jaikumar Ganesh            mHfpProfileState.sendMessage(msg);
246170a053bf1ba331d727e2fbfca8f39d96b3b324b4Jaikumar Ganesh        } else if (profile == BluetoothProfileState.A2DP) {
246270a053bf1ba331d727e2fbfca8f39d96b3b324b4Jaikumar Ganesh            mA2dpProfileState.sendMessage(msg);
246370a053bf1ba331d727e2fbfca8f39d96b3b324b4Jaikumar Ganesh        }
246470a053bf1ba331d727e2fbfca8f39d96b3b324b4Jaikumar Ganesh    }
246570a053bf1ba331d727e2fbfca8f39d96b3b324b4Jaikumar Ganesh
2466c53cab20b225ba69057441ecb06db6478ab8ff9bJaikumar Ganesh    public int getAdapterConnectionState() {
2467c53cab20b225ba69057441ecb06db6478ab8ff9bJaikumar Ganesh        return mAdapterConnectionState;
2468c53cab20b225ba69057441ecb06db6478ab8ff9bJaikumar Ganesh    }
2469c53cab20b225ba69057441ecb06db6478ab8ff9bJaikumar Ganesh
2470cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh    public int getProfileConnectionState(int profile) {
2471cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
2472cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh
2473cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        Pair<Integer, Integer> state = mProfileConnectionState.get(profile);
2474cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        if (state == null) return BluetoothProfile.STATE_DISCONNECTED;
2475cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh
2476cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        return state.first;
2477cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh    }
2478cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh
2479cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh    private void updateProfileConnectionState(int profile, int newState, int oldState) {
2480cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        // mProfileConnectionState is a hashmap -
2481cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        // <Integer, Pair<Integer, Integer>>
2482cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        // The key is the profile, the value is a pair. first element
2483cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        // is the state and the second element is the number of devices
2484cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        // in that state.
2485cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        int numDev = 1;
2486cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        int newHashState = newState;
2487cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        boolean update = true;
2488cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh
2489cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        // The following conditions are considered in this function:
2490cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        // 1. If there is no record of profile and state - update
2491cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        // 2. If a new device's state is current hash state - increment
2492cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        //    number of devices in the state.
2493cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        // 3. If a state change has happened to Connected or Connecting
2494cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        //    (if current state is not connected), update.
2495cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        // 4. If numDevices is 1 and that device state is being updated, update
2496cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        // 5. If numDevices is > 1 and one of the devices is changing state,
2497cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        //    decrement numDevices but maintain oldState if it is Connected or
2498cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        //    Connecting
2499cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        Pair<Integer, Integer> stateNumDev = mProfileConnectionState.get(profile);
2500cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        if (stateNumDev != null) {
2501cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh            int currHashState = stateNumDev.first;
2502cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh            numDev = stateNumDev.second;
2503cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh
2504cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh            if (newState == currHashState) {
2505cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh                numDev ++;
2506cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh            } else if (newState == BluetoothProfile.STATE_CONNECTED ||
2507cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh                   (newState == BluetoothProfile.STATE_CONNECTING &&
2508cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh                    currHashState != BluetoothProfile.STATE_CONNECTED)) {
2509cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh                 numDev = 1;
2510cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh            } else if (numDev == 1 && oldState == currHashState) {
2511cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh                 update = true;
2512cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh            } else if (numDev > 1 && oldState == currHashState) {
2513cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh                 numDev --;
2514cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh
2515cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh                 if (currHashState == BluetoothProfile.STATE_CONNECTED ||
2516cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh                     currHashState == BluetoothProfile.STATE_CONNECTING) {
2517cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh                    newHashState = currHashState;
2518cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh                 }
2519cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh            } else {
2520cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh                 update = false;
2521cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh            }
2522cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        }
2523cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh
2524cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        if (update) {
2525cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh            mProfileConnectionState.put(profile, new Pair<Integer, Integer>(newHashState,
2526cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh                    numDev));
2527cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        }
2528cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh    }
2529cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh
2530cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh    public synchronized void sendConnectionStateChange(BluetoothDevice
2531cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh            device, int profile, int state, int prevState) {
25329a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        // Since this is a binder call check if Bluetooth is on still
25337f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie        if (getBluetoothStateInternal() == BluetoothAdapter.STATE_OFF) return;
25349a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby
2535cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        if (!validateProfileConnectionState(state) ||
2536cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh                !validateProfileConnectionState(prevState)) {
2537cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh            // Previously, an invalid state was broadcast anyway,
2538cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh            // with the invalid state converted to -1 in the intent.
2539cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh            // Better to log an error and not send an intent with
2540cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh            // invalid contents or set mAdapterConnectionState to -1.
2541cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh            Log.e(TAG, "Error in sendConnectionStateChange: "
2542cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh                    + "prevState " + prevState + " state " + state);
2543cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh            return;
2544cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        }
2545c53cab20b225ba69057441ecb06db6478ab8ff9bJaikumar Ganesh
2546cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        updateProfileConnectionState(profile, state, prevState);
2547cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh
2548cb1d354c1e9b458a0426cd08520d938012e32b34Jaikumar Ganesh        if (updateCountersAndCheckForConnectionStateChange(state, prevState)) {
2549c53cab20b225ba69057441ecb06db6478ab8ff9bJaikumar Ganesh            mAdapterConnectionState = state;
2550c53cab20b225ba69057441ecb06db6478ab8ff9bJaikumar Ganesh
25517f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie            if (state == BluetoothProfile.STATE_DISCONNECTED) {
25527f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie                mBluetoothState.sendMessage(BluetoothAdapterStateMachine.ALL_DEVICES_DISCONNECTED);
25537f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie            }
25547f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie
2555a46f2fb152355fe591f9dddeb41f01fef5ccad1dJaikumar Ganesh            Intent intent = new Intent(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
2556a46f2fb152355fe591f9dddeb41f01fef5ccad1dJaikumar Ganesh            intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
25579a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            intent.putExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
25589a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                    convertToAdapterState(state));
25599a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_CONNECTION_STATE,
25609a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                    convertToAdapterState(prevState));
256160b4d2a2bd232a7116fd037501cac704f328c0a1Jaikumar Ganesh            intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
2562a46f2fb152355fe591f9dddeb41f01fef5ccad1dJaikumar Ganesh            mContext.sendBroadcast(intent, BLUETOOTH_PERM);
25639a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            Log.d(TAG, "CONNECTION_STATE_CHANGE: " + device + ": "
25649a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                    + prevState + " -> " + state);
2565a46f2fb152355fe591f9dddeb41f01fef5ccad1dJaikumar Ganesh        }
2566a46f2fb152355fe591f9dddeb41f01fef5ccad1dJaikumar Ganesh    }
2567a46f2fb152355fe591f9dddeb41f01fef5ccad1dJaikumar Ganesh
25689a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    private boolean validateProfileConnectionState(int state) {
25699a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        return (state == BluetoothProfile.STATE_DISCONNECTED ||
25709a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                state == BluetoothProfile.STATE_CONNECTING ||
25719a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                state == BluetoothProfile.STATE_CONNECTED ||
25729a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                state == BluetoothProfile.STATE_DISCONNECTING);
25739a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    }
25749a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby
25759a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    private int convertToAdapterState(int state) {
2576a46f2fb152355fe591f9dddeb41f01fef5ccad1dJaikumar Ganesh        switch (state) {
25779a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            case BluetoothProfile.STATE_DISCONNECTED:
25789a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                return BluetoothAdapter.STATE_DISCONNECTED;
25799a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            case BluetoothProfile.STATE_DISCONNECTING:
25809a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                return BluetoothAdapter.STATE_DISCONNECTING;
25819a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            case BluetoothProfile.STATE_CONNECTED:
25829a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                return BluetoothAdapter.STATE_CONNECTED;
2583a46f2fb152355fe591f9dddeb41f01fef5ccad1dJaikumar Ganesh            case BluetoothProfile.STATE_CONNECTING:
2584a46f2fb152355fe591f9dddeb41f01fef5ccad1dJaikumar Ganesh                return BluetoothAdapter.STATE_CONNECTING;
25859a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        }
25869a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        Log.e(TAG, "Error in convertToAdapterState");
25879a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        return -1;
25889a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    }
25899a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby
25909a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    private boolean updateCountersAndCheckForConnectionStateChange(int state, int prevState) {
25919a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby        switch (prevState) {
25929a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            case BluetoothProfile.STATE_CONNECTING:
25939a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                mProfilesConnecting--;
25949a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                break;
25959a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby
2596a46f2fb152355fe591f9dddeb41f01fef5ccad1dJaikumar Ganesh            case BluetoothProfile.STATE_CONNECTED:
25979a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                mProfilesConnected--;
25989a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                break;
25999a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby
2600a46f2fb152355fe591f9dddeb41f01fef5ccad1dJaikumar Ganesh            case BluetoothProfile.STATE_DISCONNECTING:
26019a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                mProfilesDisconnecting--;
26029a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                break;
2603a46f2fb152355fe591f9dddeb41f01fef5ccad1dJaikumar Ganesh        }
2604a46f2fb152355fe591f9dddeb41f01fef5ccad1dJaikumar Ganesh
2605a46f2fb152355fe591f9dddeb41f01fef5ccad1dJaikumar Ganesh        switch (state) {
2606a46f2fb152355fe591f9dddeb41f01fef5ccad1dJaikumar Ganesh            case BluetoothProfile.STATE_CONNECTING:
2607a46f2fb152355fe591f9dddeb41f01fef5ccad1dJaikumar Ganesh                mProfilesConnecting++;
26089a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                return (mProfilesConnected == 0 && mProfilesConnecting == 1);
2609a46f2fb152355fe591f9dddeb41f01fef5ccad1dJaikumar Ganesh
2610a46f2fb152355fe591f9dddeb41f01fef5ccad1dJaikumar Ganesh            case BluetoothProfile.STATE_CONNECTED:
2611a46f2fb152355fe591f9dddeb41f01fef5ccad1dJaikumar Ganesh                mProfilesConnected++;
26129a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                return (mProfilesConnected == 1);
2613a46f2fb152355fe591f9dddeb41f01fef5ccad1dJaikumar Ganesh
2614a46f2fb152355fe591f9dddeb41f01fef5ccad1dJaikumar Ganesh            case BluetoothProfile.STATE_DISCONNECTING:
2615a46f2fb152355fe591f9dddeb41f01fef5ccad1dJaikumar Ganesh                mProfilesDisconnecting++;
26169a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                return (mProfilesConnected == 0 && mProfilesDisconnecting == 1);
2617a46f2fb152355fe591f9dddeb41f01fef5ccad1dJaikumar Ganesh
2618a46f2fb152355fe591f9dddeb41f01fef5ccad1dJaikumar Ganesh            case BluetoothProfile.STATE_DISCONNECTED:
26199a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                return (mProfilesConnected == 0 && mProfilesConnecting == 0);
2620a46f2fb152355fe591f9dddeb41f01fef5ccad1dJaikumar Ganesh
26219a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby            default:
26229a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby                return true;
2623a46f2fb152355fe591f9dddeb41f01fef5ccad1dJaikumar Ganesh        }
26249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2625d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh
2626a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie    private void createIncomingConnectionStateFile() {
2627a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        File f = new File(INCOMING_CONNECTION_FILE);
2628a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        if (!f.exists()) {
2629a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie            try {
2630a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                f.createNewFile();
2631a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie            } catch (IOException e) {
2632a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                Log.e(TAG, "IOException: cannot create file");
2633a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie            }
2634a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        }
2635a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie    }
2636a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie
2637a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie    /** @hide */
2638a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie    public Pair<Integer, String> getIncomingState(String address) {
2639a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        if (mIncomingConnections.isEmpty()) {
2640a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie            createIncomingConnectionStateFile();
2641a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie            readIncomingConnectionState();
2642a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        }
2643a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        return mIncomingConnections.get(address);
2644a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie    }
2645a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie
2646a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie    private void readIncomingConnectionState() {
2647a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        synchronized(mIncomingConnections) {
2648a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie            FileInputStream fstream = null;
2649a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie            try {
2650a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie              fstream = new FileInputStream(INCOMING_CONNECTION_FILE);
2651a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie              DataInputStream in = new DataInputStream(fstream);
2652a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie              BufferedReader file = new BufferedReader(new InputStreamReader(in));
2653a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie              String line;
2654a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie              while((line = file.readLine()) != null) {
2655a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                  line = line.trim();
2656a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                  if (line.length() == 0) continue;
2657a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                  String[] value = line.split(",");
2658a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                  if (value != null && value.length == 3) {
2659a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                      Integer val1 = Integer.parseInt(value[1]);
2660a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                      Pair<Integer, String> val = new Pair(val1, value[2]);
2661a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                      mIncomingConnections.put(value[0], val);
2662a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                  }
2663a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie              }
2664a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie            } catch (FileNotFoundException e) {
2665a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                log("FileNotFoundException: readIncomingConnectionState" + e.toString());
2666a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie            } catch (IOException e) {
2667a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                log("IOException: readIncomingConnectionState" + e.toString());
2668a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie            } finally {
2669a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                if (fstream != null) {
2670a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                    try {
2671a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                        fstream.close();
2672a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                    } catch (IOException e) {
2673a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                        // Ignore
2674a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                    }
2675a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                }
2676a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie            }
2677a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        }
2678a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie    }
2679a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie
2680a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie    private void truncateIncomingConnectionFile() {
2681a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        RandomAccessFile r = null;
2682a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        try {
2683a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie            r = new RandomAccessFile(INCOMING_CONNECTION_FILE, "rw");
2684a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie            r.setLength(0);
2685a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        } catch (FileNotFoundException e) {
2686a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie            log("FileNotFoundException: truncateIncomingConnectionState" + e.toString());
2687a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        } catch (IOException e) {
2688a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie            log("IOException: truncateIncomingConnectionState" + e.toString());
2689a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        } finally {
2690a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie            if (r != null) {
2691a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                try {
2692a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                    r.close();
2693a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                } catch (IOException e) {
2694a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                    // ignore
2695a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                 }
2696a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie            }
2697a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        }
2698a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie    }
2699a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie
2700a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie    /** @hide */
2701a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie    public void writeIncomingConnectionState(String address, Pair<Integer, String> data) {
2702a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        synchronized(mIncomingConnections) {
2703a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie            mIncomingConnections.put(address, data);
2704a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie
2705a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie            truncateIncomingConnectionFile();
2706a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie            BufferedWriter out = null;
2707a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie            StringBuilder value = new StringBuilder();
2708a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie            try {
2709a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                out = new BufferedWriter(new FileWriter(INCOMING_CONNECTION_FILE, true));
2710a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                for (String devAddress: mIncomingConnections.keySet()) {
2711a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                  Pair<Integer, String> val = mIncomingConnections.get(devAddress);
2712a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                  value.append(devAddress);
2713a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                  value.append(",");
2714a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                  value.append(val.first.toString());
2715a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                  value.append(",");
2716a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                  value.append(val.second);
2717a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                  value.append("\n");
2718a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                }
2719a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                out.write(value.toString());
2720a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie            } catch (FileNotFoundException e) {
2721a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                log("FileNotFoundException: writeIncomingConnectionState" + e.toString());
2722a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie            } catch (IOException e) {
2723a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                log("IOException: writeIncomingConnectionState" + e.toString());
2724a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie            } finally {
2725a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                if (out != null) {
2726a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                    try {
2727a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                        out.close();
2728a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                    } catch (IOException e) {
2729a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                        // Ignore
2730a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                    }
2731a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                }
2732a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie            }
2733a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        }
2734a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie    }
2735a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie
2736a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie    private static void log(String msg) {
2737a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        Log.d(TAG, msg);
2738a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie    }
2739a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie
2740d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    private native static void classInitNative();
2741d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    private native void initializeNativeDataNative();
2742d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    private native boolean setupNativeDataNative();
2743d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    private native boolean tearDownNativeDataNative();
2744d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    private native void cleanupNativeDataNative();
27459a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    /*package*/ native String getAdapterPathNative();
2746d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh
2747d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    private native int isEnabledNative();
27487f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie    /*package*/ native int enableNative();
27497f9ecca8f2dc288f785b37d2478e89b80fc3cefcMatthew Xie    /*package*/ native int disableNative();
2750d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh
27519a62c9cd6585656f4e29ba971b1f88a510d674bdJake Hamby    /*package*/ native Object[] getAdapterPropertiesNative();
2752d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    private native Object[] getDevicePropertiesNative(String objectPath);
2753d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    private native boolean setAdapterPropertyStringNative(String key, String value);
2754d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    private native boolean setAdapterPropertyIntegerNative(String key, int value);
2755d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    private native boolean setAdapterPropertyBooleanNative(String key, int value);
2756d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh
2757d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    private native boolean startDiscoveryNative();
2758d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    private native boolean stopDiscoveryNative();
2759d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh
2760d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    private native boolean createPairedDeviceNative(String address, int timeout_ms);
2761cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh    private native boolean createPairedDeviceOutOfBandNative(String address, int timeout_ms);
2762cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh    private native byte[] readAdapterOutOfBandDataNative();
2763cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh
2764d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    private native boolean cancelDeviceCreationNative(String address);
2765d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    private native boolean removeDeviceNative(String objectPath);
2766d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    private native int getDeviceServiceChannelNative(String objectPath, String uuid,
2767d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh            int attributeId);
2768d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh
2769b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh    private native boolean cancelPairingUserInputNative(String address, int nativeData);
2770d5ac1ae36b4e096eb97984334f86d0c68abea2f7Jaikumar Ganesh    private native boolean setPinNative(String address, String pin, int nativeData);
2771b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh    private native boolean setPasskeyNative(String address, int passkey, int nativeData);
2772b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh    private native boolean setPairingConfirmationNative(String address, boolean confirm,
2773b0eca41de0bb6747d8648b134912782e45e4cbefJaikumar Ganesh            int nativeData);
2774cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh    private native boolean setRemoteOutOfBandDataNative(String address, byte[] hash,
2775cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh                                                        byte[] randomizer, int nativeData);
2776cc5494c9996f809e36539b24e8b6b67683383d29Jaikumar Ganesh
277724bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly    private native boolean setDevicePropertyBooleanNative(String objectPath, String key,
277824bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            int value);
2779269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie    private native boolean setDevicePropertyStringNative(String objectPath, String key,
2780269e81a563cfe080d7f241d0d46411d3c946c111Matthew Xie            String value);
27811caa6d111eff6814760ec156b14adc29aa3aae6cJaikumar Ganesh    private native boolean createDeviceNative(String address);
278216fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly    /*package*/ native boolean discoverServicesNative(String objectPath, String pattern);
278310eac971b3a6e5f34a420dd68ebfa796553ad2b9Jaikumar Ganesh
278424bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly    private native int addRfcommServiceRecordNative(String name, long uuidMsb, long uuidLsb,
278524bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            short channel);
278624bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly    private native boolean removeServiceRecordNative(int handle);
2787b7e029d03c115ed65cdea9b2bba307e882c308e2Jaikumar Ganesh    private native boolean setLinkTimeoutNative(String path, int num_slots);
2788a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie
278930b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh    native boolean connectInputDeviceNative(String path);
279030b8cbe059b9addff643242e8a1fc86ca72ba54eJaikumar Ganesh    native boolean disconnectInputDeviceNative(String path);
27916fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang
2792a8571f15c39da2b7c6e672fef26106f5ac60dea0Jaikumar Ganesh    native boolean setBluetoothTetheringNative(boolean value, String nap, String bridge);
2793a8571f15c39da2b7c6e672fef26106f5ac60dea0Jaikumar Ganesh    native boolean connectPanDeviceNative(String path, String dstRole);
2794a8571f15c39da2b7c6e672fef26106f5ac60dea0Jaikumar Ganesh    native boolean disconnectPanDeviceNative(String path);
2795a8571f15c39da2b7c6e672fef26106f5ac60dea0Jaikumar Ganesh    native boolean disconnectPanServerDeviceNative(String path,
2796a44a1e77ef435942b2ff60e9e60d3a64b43d775bJaikumar Ganesh            String address, String iface);
279784690c88f37f395094147d27ace8319a2803a522Jaikumar Ganesh
279884690c88f37f395094147d27ace8319a2803a522Jaikumar Ganesh    private native int[] addReservedServiceRecordsNative(int[] uuuids);
279984690c88f37f395094147d27ace8319a2803a522Jaikumar Ganesh    private native boolean removeReservedServiceRecordsNative(int[] handles);
28002ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh
28012ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh    // Health API
28022ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh    native String registerHealthApplicationNative(int dataType, String role, String name,
28032ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh            String channelType);
28042ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh    native String registerHealthApplicationNative(int dataType, String role, String name);
28052ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh    native boolean unregisterHealthApplicationNative(String path);
2806b5d2d4526cd2c0117b7a33b1297ac683c37ac5c7Jaikumar Ganesh    native boolean createChannelNative(String devicePath, String appPath, String channelType,
2807b5d2d4526cd2c0117b7a33b1297ac683c37ac5c7Jaikumar Ganesh                                       int code);
2808b5d2d4526cd2c0117b7a33b1297ac683c37ac5c7Jaikumar Ganesh    native boolean destroyChannelNative(String devicePath, String channelpath, int code);
28092ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh    native String getMainChannelNative(String path);
28102ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh    native String getChannelApplicationNative(String channelPath);
28112ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh    native ParcelFileDescriptor getChannelFdNative(String channelPath);
28122ea1e85dcb57d17f5782dbafa1d25eb51c630e4bJaikumar Ganesh    native boolean releaseChannelFdNative(String channelPath);
2813a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie    native boolean setAuthorizationNative(String address, boolean value, int data);
28149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2815