The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /** |
| 18 | * TODO: Move this to |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 19 | * java/services/com/android/server/BluetoothService.java |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | * and make the contructor package private again. |
| 21 | * |
| 22 | * @hide |
| 23 | */ |
| 24 | |
| 25 | package android.server; |
| 26 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 27 | import android.bluetooth.BluetoothAdapter; |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 28 | import android.bluetooth.BluetoothClass; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.bluetooth.BluetoothDevice; |
Jaikumar Ganesh | f1048cd | 2010-06-02 20:30:34 -0700 | [diff] [blame] | 30 | import android.bluetooth.BluetoothDeviceProfileState; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 31 | import android.bluetooth.BluetoothHeadset; |
Jaikumar Ganesh | 2ea1e85 | 2011-04-01 16:33:09 -0700 | [diff] [blame] | 32 | import android.bluetooth.BluetoothHealthAppConfiguration; |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 33 | import android.bluetooth.BluetoothInputDevice; |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 34 | import android.bluetooth.BluetoothPan; |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 35 | import android.bluetooth.BluetoothProfile; |
Jaikumar Ganesh | f1048cd | 2010-06-02 20:30:34 -0700 | [diff] [blame] | 36 | import android.bluetooth.BluetoothProfileState; |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 37 | import android.bluetooth.BluetoothSocket; |
Jaikumar Ganesh | 10eac97 | 2009-09-21 12:48:51 -0700 | [diff] [blame] | 38 | import android.bluetooth.BluetoothUuid; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 39 | import android.bluetooth.IBluetooth; |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 40 | import android.bluetooth.IBluetoothCallback; |
Jaikumar Ganesh | fb658c7 | 2011-07-06 17:37:02 -0700 | [diff] [blame] | 41 | import android.bluetooth.IBluetoothHealthCallback; |
Jaikumar Ganesh | ef2cb7c | 2011-07-21 18:13:38 -0700 | [diff] [blame^] | 42 | import android.bluetooth.IBluetoothStateChangeCallback; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import android.content.BroadcastReceiver; |
| 44 | import android.content.ContentResolver; |
| 45 | import android.content.Context; |
| 46 | import android.content.Intent; |
| 47 | import android.content.IntentFilter; |
Jaikumar Ganesh | 6e9c443 | 2009-12-09 12:09:21 -0800 | [diff] [blame] | 48 | import android.content.SharedPreferences; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import android.os.Binder; |
| 50 | import android.os.Handler; |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 51 | import android.os.IBinder; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | import android.os.Message; |
Jaikumar Ganesh | 2ea1e85 | 2011-04-01 16:33:09 -0700 | [diff] [blame] | 53 | import android.os.ParcelFileDescriptor; |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 54 | import android.os.ParcelUuid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | import android.os.RemoteException; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 56 | import android.os.ServiceManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | import android.provider.Settings; |
| 58 | import android.util.Log; |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 59 | import android.util.Pair; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | |
Jaikumar Ganesh | fb658c7 | 2011-07-06 17:37:02 -0700 | [diff] [blame] | 61 | import com.android.internal.app.IBatteryStats; |
| 62 | |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 63 | import java.io.BufferedInputStream; |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 64 | import java.io.BufferedReader; |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 65 | import java.io.BufferedWriter; |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 66 | import java.io.DataInputStream; |
| 67 | import java.io.File; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | import java.io.FileDescriptor; |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 69 | import java.io.FileInputStream; |
| 70 | import java.io.FileNotFoundException; |
| 71 | import java.io.FileWriter; |
| 72 | import java.io.IOException; |
Jaikumar Ganesh | ef2cb7c | 2011-07-21 18:13:38 -0700 | [diff] [blame^] | 73 | import java.io.InputStreamReader; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | import java.io.PrintWriter; |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 75 | import java.io.RandomAccessFile; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | import java.io.UnsupportedEncodingException; |
| 77 | import java.util.ArrayList; |
| 78 | import java.util.Arrays; |
Jaikumar Ganesh | ef2cb7c | 2011-07-21 18:13:38 -0700 | [diff] [blame^] | 79 | import java.util.Collection; |
| 80 | import java.util.Collections; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | import java.util.HashMap; |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 82 | import java.util.Iterator; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 83 | import java.util.List; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | import java.util.Map; |
| 85 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 86 | public class BluetoothService extends IBluetooth.Stub { |
| 87 | private static final String TAG = "BluetoothService"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | private static final boolean DBG = true; |
| 89 | |
| 90 | private int mNativeData; |
| 91 | private BluetoothEventLoop mEventLoop; |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 92 | private BluetoothHeadset mBluetoothHeadset; |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 93 | private BluetoothInputDevice mInputDevice; |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 94 | private BluetoothPan mPan; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | private boolean mIsAirplaneSensitive; |
Jeff Sharkey | 4430392 | 2009-12-01 18:25:02 -0800 | [diff] [blame] | 96 | private boolean mIsAirplaneToggleable; |
Matthew Xie | 7f9ecca8 | 2011-07-15 13:03:58 -0700 | [diff] [blame] | 97 | private BluetoothAdapterStateMachine mBluetoothState; |
Nick Pelly | 997c761 | 2009-03-24 20:44:48 -0700 | [diff] [blame] | 98 | private boolean mRestart = false; // need to call enable() after disable() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | private boolean mIsDiscovering; |
Jaikumar Ganesh | 84690c8 | 2010-12-10 12:48:58 -0800 | [diff] [blame] | 100 | private int[] mAdapterSdpHandles; |
Jaikumar Ganesh | 50b40ce | 2011-02-02 14:44:49 -0800 | [diff] [blame] | 101 | private ParcelUuid[] mAdapterUuids; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 103 | private BluetoothAdapter mAdapter; // constant after init() |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 104 | private final BluetoothBondState mBondState; // local cache of bondings |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 105 | private final IBatteryStats mBatteryStats; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | private final Context mContext; |
Jaikumar Ganesh | ef2cb7c | 2011-07-21 18:13:38 -0700 | [diff] [blame^] | 107 | private Map<Integer, IBluetoothStateChangeCallback> mStateChangeTracker = |
| 108 | Collections.synchronizedMap(new HashMap<Integer, IBluetoothStateChangeCallback>()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | |
| 110 | private static final String BLUETOOTH_ADMIN_PERM = android.Manifest.permission.BLUETOOTH_ADMIN; |
Jaikumar Ganesh | a8571f1 | 2011-02-11 15:46:54 -0800 | [diff] [blame] | 111 | static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 113 | private static final String DOCK_ADDRESS_PATH = "/sys/class/switch/dock/bt_addr"; |
| 114 | private static final String DOCK_PIN_PATH = "/sys/class/switch/dock/bt_pin"; |
| 115 | |
Jaikumar Ganesh | 6e9c443 | 2009-12-09 12:09:21 -0800 | [diff] [blame] | 116 | private static final String SHARED_PREFERENCE_DOCK_ADDRESS = "dock_bluetooth_address"; |
| 117 | private static final String SHARED_PREFERENCES_NAME = "bluetooth_service_settings"; |
| 118 | |
Matthew Xie | 7f9ecca8 | 2011-07-15 13:03:58 -0700 | [diff] [blame] | 119 | private static final int MESSAGE_UUID_INTENT = 1; |
| 120 | private static final int MESSAGE_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY = 2; |
Jaikumar Ganesh | a224f70 | 2010-09-10 15:09:54 -0700 | [diff] [blame] | 121 | |
Jaikumar Ganesh | ef2cb7c | 2011-07-21 18:13:38 -0700 | [diff] [blame^] | 122 | private static final int RFCOMM_RECORD_REAPER = 10; |
| 123 | private static final int STATE_CHANGE_REAPER = 11; |
| 124 | |
Jaikumar Ganesh | a224f70 | 2010-09-10 15:09:54 -0700 | [diff] [blame] | 125 | // The time (in millisecs) to delay the pairing attempt after the first |
| 126 | // auto pairing attempt fails. We use an exponential delay with |
| 127 | // INIT_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY as the initial value and |
| 128 | // MAX_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY as the max value. |
| 129 | private static final long INIT_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY = 3000; |
| 130 | private static final long MAX_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY = 12000; |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 131 | |
| 132 | // The timeout used to sent the UUIDs Intent |
| 133 | // This timeout should be greater than the page timeout |
| 134 | private static final int UUID_INTENT_DELAY = 6000; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 135 | |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 136 | /** Always retrieve RFCOMM channel for these SDP UUIDs */ |
| 137 | private static final ParcelUuid[] RFCOMM_UUIDS = { |
| 138 | BluetoothUuid.Handsfree, |
| 139 | BluetoothUuid.HSP, |
| 140 | BluetoothUuid.ObexObjectPush }; |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 141 | |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 142 | private final BluetoothAdapterProperties mAdapterProperties; |
| 143 | private final BluetoothDeviceProperties mDeviceProperties; |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 144 | |
| 145 | private final HashMap<String, Map<ParcelUuid, Integer>> mDeviceServiceChannelCache; |
| 146 | private final ArrayList<String> mUuidIntentTracker; |
| 147 | private final HashMap<RemoteService, IBluetoothCallback> mUuidCallbackTracker; |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 148 | |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 149 | private final HashMap<Integer, Integer> mServiceRecordToPid; |
| 150 | |
Jaikumar Ganesh | f1048cd | 2010-06-02 20:30:34 -0700 | [diff] [blame] | 151 | private final HashMap<String, BluetoothDeviceProfileState> mDeviceProfileState; |
| 152 | private final BluetoothProfileState mA2dpProfileState; |
| 153 | private final BluetoothProfileState mHfpProfileState; |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 154 | |
| 155 | private BluetoothA2dpService mA2dpService; |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 156 | private final HashMap<String, Pair<byte[], byte[]>> mDeviceOobData; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 157 | |
Jaikumar Ganesh | a46f2fb | 2010-10-21 14:55:05 -0700 | [diff] [blame] | 158 | private int mProfilesConnected = 0, mProfilesConnecting = 0, mProfilesDisconnecting = 0; |
Jaikumar Ganesh | 707952e | 2010-09-13 19:04:54 -0700 | [diff] [blame] | 159 | |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 160 | private static String mDockAddress; |
| 161 | private String mDockPin; |
| 162 | |
Jaikumar Ganesh | c53cab2 | 2010-10-26 16:02:26 -0700 | [diff] [blame] | 163 | private int mAdapterConnectionState = BluetoothAdapter.STATE_DISCONNECTED; |
Jaikumar Ganesh | a8571f1 | 2011-02-11 15:46:54 -0800 | [diff] [blame] | 164 | private BluetoothPanProfileHandler mBluetoothPanProfileHandler; |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 165 | private BluetoothInputProfileHandler mBluetoothInputProfileHandler; |
Jaikumar Ganesh | 2ea1e85 | 2011-04-01 16:33:09 -0700 | [diff] [blame] | 166 | private BluetoothHealthProfileHandler mBluetoothHealthProfileHandler; |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 167 | private static final String INCOMING_CONNECTION_FILE = |
| 168 | "/data/misc/bluetooth/incoming_connection.conf"; |
| 169 | private HashMap<String, Pair<Integer, String>> mIncomingConnections; |
Jaikumar Ganesh | c53cab2 | 2010-10-26 16:02:26 -0700 | [diff] [blame] | 170 | |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 171 | private static class RemoteService { |
| 172 | public String address; |
| 173 | public ParcelUuid uuid; |
| 174 | public RemoteService(String address, ParcelUuid uuid) { |
| 175 | this.address = address; |
| 176 | this.uuid = uuid; |
| 177 | } |
| 178 | @Override |
| 179 | public boolean equals(Object o) { |
| 180 | if (o instanceof RemoteService) { |
| 181 | RemoteService service = (RemoteService)o; |
| 182 | return address.equals(service.address) && uuid.equals(service.uuid); |
| 183 | } |
| 184 | return false; |
| 185 | } |
Kenny Root | 5f61416 | 2010-02-17 12:00:37 -0800 | [diff] [blame] | 186 | |
| 187 | @Override |
| 188 | public int hashCode() { |
| 189 | int hash = 1; |
| 190 | hash = hash * 31 + (address == null ? 0 : address.hashCode()); |
| 191 | hash = hash * 31 + (uuid == null ? 0 : uuid.hashCode()); |
| 192 | return hash; |
| 193 | } |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 194 | } |
| 195 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | static { |
| 197 | classInitNative(); |
| 198 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 200 | public BluetoothService(Context context) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | mContext = context; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 202 | |
| 203 | // Need to do this in place of: |
| 204 | // mBatteryStats = BatteryStatsService.getService(); |
| 205 | // Since we can not import BatteryStatsService from here. This class really needs to be |
| 206 | // moved to java/services/com/android/server/ |
| 207 | mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService("batteryinfo")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | initializeNativeDataNative(); |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 210 | |
| 211 | if (isEnabledNative() == 1) { |
| 212 | Log.w(TAG, "Bluetooth daemons already running - runtime restart? "); |
| 213 | disableNative(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | } |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 215 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | mIsDiscovering = false; |
Jaikumar Ganesh | a8571f1 | 2011-02-11 15:46:54 -0800 | [diff] [blame] | 217 | |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 218 | mBondState = new BluetoothBondState(context, this); |
| 219 | mAdapterProperties = new BluetoothAdapterProperties(context, this); |
| 220 | mDeviceProperties = new BluetoothDeviceProperties(this); |
Jaikumar Ganesh | 10eac97 | 2009-09-21 12:48:51 -0700 | [diff] [blame] | 221 | |
| 222 | mDeviceServiceChannelCache = new HashMap<String, Map<ParcelUuid, Integer>>(); |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 223 | mDeviceOobData = new HashMap<String, Pair<byte[], byte[]>>(); |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 224 | mUuidIntentTracker = new ArrayList<String>(); |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 225 | mUuidCallbackTracker = new HashMap<RemoteService, IBluetoothCallback>(); |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 226 | mServiceRecordToPid = new HashMap<Integer, Integer>(); |
Jaikumar Ganesh | f1048cd | 2010-06-02 20:30:34 -0700 | [diff] [blame] | 227 | mDeviceProfileState = new HashMap<String, BluetoothDeviceProfileState>(); |
| 228 | mA2dpProfileState = new BluetoothProfileState(mContext, BluetoothProfileState.A2DP); |
| 229 | mHfpProfileState = new BluetoothProfileState(mContext, BluetoothProfileState.HFP); |
| 230 | |
| 231 | mHfpProfileState.start(); |
| 232 | mA2dpProfileState.start(); |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 233 | |
| 234 | IntentFilter filter = new IntentFilter(); |
Jaikumar Ganesh | 6e9c443 | 2009-12-09 12:09:21 -0800 | [diff] [blame] | 235 | registerForAirplaneMode(filter); |
| 236 | |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 237 | filter.addAction(Intent.ACTION_DOCK_EVENT); |
Jaikumar Ganesh | 6e9c443 | 2009-12-09 12:09:21 -0800 | [diff] [blame] | 238 | mContext.registerReceiver(mReceiver, filter); |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 239 | mBluetoothInputProfileHandler = BluetoothInputProfileHandler.getInstance(mContext, this); |
Jaikumar Ganesh | a8571f1 | 2011-02-11 15:46:54 -0800 | [diff] [blame] | 240 | mBluetoothPanProfileHandler = BluetoothPanProfileHandler.getInstance(mContext, this); |
Jaikumar Ganesh | 2ea1e85 | 2011-04-01 16:33:09 -0700 | [diff] [blame] | 241 | mBluetoothHealthProfileHandler = BluetoothHealthProfileHandler.getInstance(mContext, this); |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 242 | mIncomingConnections = new HashMap<String, Pair<Integer, String>>(); |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 243 | } |
| 244 | |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 245 | public static synchronized String readDockBluetoothAddress() { |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 246 | if (mDockAddress != null) return mDockAddress; |
| 247 | |
| 248 | BufferedInputStream file = null; |
| 249 | String dockAddress; |
| 250 | try { |
| 251 | file = new BufferedInputStream(new FileInputStream(DOCK_ADDRESS_PATH)); |
| 252 | byte[] address = new byte[17]; |
| 253 | file.read(address); |
| 254 | dockAddress = new String(address); |
| 255 | dockAddress = dockAddress.toUpperCase(); |
| 256 | if (BluetoothAdapter.checkBluetoothAddress(dockAddress)) { |
| 257 | mDockAddress = dockAddress; |
| 258 | return mDockAddress; |
| 259 | } else { |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 260 | Log.e(TAG, "CheckBluetoothAddress failed for car dock address: " |
| 261 | + dockAddress); |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 262 | } |
| 263 | } catch (FileNotFoundException e) { |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 264 | Log.e(TAG, "FileNotFoundException while trying to read dock address"); |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 265 | } catch (IOException e) { |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 266 | Log.e(TAG, "IOException while trying to read dock address"); |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 267 | } finally { |
| 268 | if (file != null) { |
| 269 | try { |
| 270 | file.close(); |
| 271 | } catch (IOException e) { |
| 272 | // Ignore |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | mDockAddress = null; |
| 277 | return null; |
| 278 | } |
| 279 | |
| 280 | private synchronized boolean writeDockPin() { |
| 281 | BufferedWriter out = null; |
| 282 | try { |
| 283 | out = new BufferedWriter(new FileWriter(DOCK_PIN_PATH)); |
| 284 | |
| 285 | // Generate a random 4 digit pin between 0000 and 9999 |
| 286 | // This is not truly random but good enough for our purposes. |
| 287 | int pin = (int) Math.floor(Math.random() * 10000); |
| 288 | |
| 289 | mDockPin = String.format("%04d", pin); |
| 290 | out.write(mDockPin); |
| 291 | return true; |
| 292 | } catch (FileNotFoundException e) { |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 293 | Log.e(TAG, "FileNotFoundException while trying to write dock pairing pin"); |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 294 | } catch (IOException e) { |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 295 | Log.e(TAG, "IOException while while trying to write dock pairing pin"); |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 296 | } finally { |
| 297 | if (out != null) { |
| 298 | try { |
| 299 | out.close(); |
| 300 | } catch (IOException e) { |
| 301 | // Ignore |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | mDockPin = null; |
| 306 | return false; |
| 307 | } |
| 308 | |
| 309 | /*package*/ synchronized String getDockPin() { |
| 310 | return mDockPin; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | public synchronized void initAfterRegistration() { |
Nick Pelly | f242b7b | 2009-10-08 00:12:45 +0200 | [diff] [blame] | 314 | mAdapter = BluetoothAdapter.getDefaultAdapter(); |
Matthew Xie | 7f9ecca8 | 2011-07-15 13:03:58 -0700 | [diff] [blame] | 315 | mBluetoothState = new BluetoothAdapterStateMachine(mContext, this, mAdapter); |
| 316 | mBluetoothState.start(); |
| 317 | mEventLoop = mBluetoothState.getBluetoothEventLoop(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | |
Jaikumar Ganesh | 7d0548d | 2010-10-18 15:29:09 -0700 | [diff] [blame] | 320 | public synchronized void initAfterA2dpRegistration() { |
| 321 | mEventLoop.getProfileProxy(); |
| 322 | } |
| 323 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | @Override |
| 325 | protected void finalize() throws Throwable { |
Jaikumar Ganesh | 6e9c443 | 2009-12-09 12:09:21 -0800 | [diff] [blame] | 326 | mContext.unregisterReceiver(mReceiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | try { |
| 328 | cleanupNativeDataNative(); |
| 329 | } finally { |
| 330 | super.finalize(); |
| 331 | } |
| 332 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | |
| 334 | public boolean isEnabled() { |
| 335 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Jaikumar Ganesh | 8c9dd7d | 2009-11-16 16:23:20 -0800 | [diff] [blame] | 336 | return isEnabledInternal(); |
| 337 | } |
| 338 | |
| 339 | private boolean isEnabledInternal() { |
Matthew Xie | 7f9ecca8 | 2011-07-15 13:03:58 -0700 | [diff] [blame] | 340 | return (getBluetoothStateInternal() == BluetoothAdapter.STATE_ON); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 341 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 342 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 343 | public int getBluetoothState() { |
| 344 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Matthew Xie | 7f9ecca8 | 2011-07-15 13:03:58 -0700 | [diff] [blame] | 345 | return getBluetoothStateInternal(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 348 | int getBluetoothStateInternal() { |
Matthew Xie | 7f9ecca8 | 2011-07-15 13:03:58 -0700 | [diff] [blame] | 349 | return mBluetoothState.getBluetoothAdapterState(); |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 350 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 351 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 352 | /** |
| 353 | * Bring down bluetooth and disable BT in settings. Returns true on success. |
| 354 | */ |
| 355 | public boolean disable() { |
| 356 | return disable(true); |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Bring down bluetooth. Returns true on success. |
| 361 | * |
Nick Pelly | e6ee3be | 2009-10-08 23:27:28 +0200 | [diff] [blame] | 362 | * @param saveSetting If true, persist the new setting |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | */ |
| 364 | public synchronized boolean disable(boolean saveSetting) { |
Nick Pelly | e6ee3be | 2009-10-08 23:27:28 +0200 | [diff] [blame] | 365 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, "Need BLUETOOTH_ADMIN permission"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 366 | |
Matthew Xie | 7f9ecca8 | 2011-07-15 13:03:58 -0700 | [diff] [blame] | 367 | int adapterState = getBluetoothStateInternal(); |
| 368 | |
| 369 | switch (adapterState) { |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 370 | case BluetoothAdapter.STATE_OFF: |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 371 | return true; |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 372 | case BluetoothAdapter.STATE_ON: |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 373 | break; |
| 374 | default: |
| 375 | return false; |
| 376 | } |
Jaikumar Ganesh | 84690c8 | 2010-12-10 12:48:58 -0800 | [diff] [blame] | 377 | |
Matthew Xie | 7f9ecca8 | 2011-07-15 13:03:58 -0700 | [diff] [blame] | 378 | mBluetoothState.sendMessage(BluetoothAdapterStateMachine.USER_TURN_OFF, saveSetting); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 379 | return true; |
| 380 | } |
| 381 | |
Matthew Xie | 7f9ecca8 | 2011-07-15 13:03:58 -0700 | [diff] [blame] | 382 | synchronized void disconnectDevices() { |
Jaikumar Ganesh | 7a0f816 | 2010-11-01 14:57:36 -0700 | [diff] [blame] | 383 | // Disconnect devices handled by BluetoothService. |
| 384 | for (BluetoothDevice device: getConnectedInputDevices()) { |
| 385 | disconnectInputDevice(device); |
| 386 | } |
| 387 | |
| 388 | for (BluetoothDevice device: getConnectedPanDevices()) { |
| 389 | disconnectPanDevice(device); |
| 390 | } |
| 391 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 392 | |
Matthew Xie | 7f9ecca8 | 2011-07-15 13:03:58 -0700 | [diff] [blame] | 393 | /** |
| 394 | * The Bluetooth has been turned off, but hot. Do bonding, profile, |
| 395 | * and internal cleanup |
| 396 | */ |
| 397 | synchronized void finishDisable() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 398 | // mark in progress bondings as cancelled |
| 399 | for (String address : mBondState.listInState(BluetoothDevice.BOND_BONDING)) { |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 400 | mBondState.setBondState(address, BluetoothDevice.BOND_NONE, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | BluetoothDevice.UNBOND_REASON_AUTH_CANCELED); |
| 402 | } |
| 403 | |
Jaikumar Ganesh | 2d1fc4e | 2010-12-13 15:54:33 -0800 | [diff] [blame] | 404 | // Stop the profile state machine for bonded devices. |
| 405 | for (String address : mBondState.listInState(BluetoothDevice.BOND_BONDED)) { |
| 406 | removeProfileState(address); |
| 407 | } |
| 408 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 409 | // update mode |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 410 | Intent intent = new Intent(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED); |
| 411 | intent.putExtra(BluetoothAdapter.EXTRA_SCAN_MODE, BluetoothAdapter.SCAN_MODE_NONE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 412 | mContext.sendBroadcast(intent, BLUETOOTH_PERM); |
| 413 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 414 | mIsDiscovering = false; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 415 | mAdapterProperties.clear(); |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 416 | mServiceRecordToPid.clear(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 417 | |
Jaikumar Ganesh | a46f2fb | 2010-10-21 14:55:05 -0700 | [diff] [blame] | 418 | mProfilesConnected = 0; |
| 419 | mProfilesConnecting = 0; |
| 420 | mProfilesDisconnecting = 0; |
Jaikumar Ganesh | c53cab2 | 2010-10-26 16:02:26 -0700 | [diff] [blame] | 421 | mAdapterConnectionState = BluetoothAdapter.STATE_DISCONNECTED; |
Jaikumar Ganesh | 50b40ce | 2011-02-02 14:44:49 -0800 | [diff] [blame] | 422 | mAdapterUuids = null; |
| 423 | mAdapterSdpHandles = null; |
Jaikumar Ganesh | a46f2fb | 2010-10-21 14:55:05 -0700 | [diff] [blame] | 424 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 425 | // Log bluetooth off to battery stats. |
| 426 | long ident = Binder.clearCallingIdentity(); |
| 427 | try { |
| 428 | mBatteryStats.noteBluetoothOff(); |
| 429 | } catch (RemoteException e) { |
| 430 | } finally { |
| 431 | Binder.restoreCallingIdentity(ident); |
| 432 | } |
Nick Pelly | 997c761 | 2009-03-24 20:44:48 -0700 | [diff] [blame] | 433 | |
| 434 | if (mRestart) { |
| 435 | mRestart = false; |
| 436 | enable(); |
| 437 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 438 | } |
| 439 | |
Matthew Xie | 7f9ecca8 | 2011-07-15 13:03:58 -0700 | [diff] [blame] | 440 | /** |
| 441 | * power off Bluetooth |
| 442 | */ |
| 443 | synchronized void shutoffBluetooth() { |
| 444 | tearDownNativeDataNative(); |
| 445 | disableNative(); |
| 446 | if (mRestart) { |
| 447 | mRestart = false; |
| 448 | enable(); |
| 449 | } |
| 450 | } |
| 451 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 452 | /** Bring up BT and persist BT on in settings */ |
| 453 | public boolean enable() { |
| 454 | return enable(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | /** |
| 458 | * Enable this Bluetooth device, asynchronously. |
| 459 | * This turns on/off the underlying hardware. |
| 460 | * |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 461 | * @param saveSetting If true, persist the new state of BT in settings |
| 462 | * @return True on success (so far) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | */ |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 464 | public synchronized boolean enable(boolean saveSetting) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 465 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 466 | "Need BLUETOOTH_ADMIN permission"); |
| 467 | |
| 468 | // Airplane mode can prevent Bluetooth radio from being turned on. |
Jeff Sharkey | 4430392 | 2009-12-01 18:25:02 -0800 | [diff] [blame] | 469 | if (mIsAirplaneSensitive && isAirplaneModeOn() && !mIsAirplaneToggleable) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 470 | return false; |
| 471 | } |
Matthew Xie | 7f9ecca8 | 2011-07-15 13:03:58 -0700 | [diff] [blame] | 472 | mBluetoothState.sendMessage(BluetoothAdapterStateMachine.USER_TURN_ON, saveSetting); |
| 473 | return true; |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * Turn on Bluetooth Module, Load firmware, and do all the preparation |
| 478 | * needed to get the Bluetooth Module ready but keep it not discoverable |
| 479 | * and not connectable. |
| 480 | */ |
| 481 | /* package */ synchronized boolean prepareBluetooth() { |
| 482 | if (!setupNativeDataNative()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 483 | return false; |
| 484 | } |
Matthew Xie | 7f9ecca8 | 2011-07-15 13:03:58 -0700 | [diff] [blame] | 485 | mIsDiscovering = false; |
| 486 | |
| 487 | switchConnectable(false); |
| 488 | updateSdpRecords(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 489 | return true; |
| 490 | } |
| 491 | |
Nick Pelly | 997c761 | 2009-03-24 20:44:48 -0700 | [diff] [blame] | 492 | /** Forcibly restart Bluetooth if it is on */ |
| 493 | /* package */ synchronized void restart() { |
Matthew Xie | 7f9ecca8 | 2011-07-15 13:03:58 -0700 | [diff] [blame] | 494 | if (getBluetoothStateInternal() != BluetoothAdapter.STATE_ON) { |
Nick Pelly | 997c761 | 2009-03-24 20:44:48 -0700 | [diff] [blame] | 495 | return; |
| 496 | } |
| 497 | mRestart = true; |
| 498 | if (!disable(false)) { |
| 499 | mRestart = false; |
| 500 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 501 | } |
Nick Pelly | 997c761 | 2009-03-24 20:44:48 -0700 | [diff] [blame] | 502 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 503 | private final Handler mHandler = new Handler() { |
| 504 | @Override |
| 505 | public void handleMessage(Message msg) { |
| 506 | switch (msg.what) { |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 507 | case MESSAGE_UUID_INTENT: |
| 508 | String address = (String)msg.obj; |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 509 | if (address != null) { |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 510 | sendUuidIntent(address); |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 511 | makeServiceChannelCallbacks(address); |
| 512 | } |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 513 | break; |
Jaikumar Ganesh | a224f70 | 2010-09-10 15:09:54 -0700 | [diff] [blame] | 514 | case MESSAGE_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY: |
| 515 | address = (String)msg.obj; |
Jaikumar Ganesh | 8adcacb | 2011-07-21 13:49:54 -0700 | [diff] [blame] | 516 | if (address == null) return; |
| 517 | int attempt = mBondState.getAttempt(address); |
| 518 | |
| 519 | // Try only if attemps are in progress and cap it 2 attempts |
| 520 | // The 2 attempts cap is a fail safe if the stack returns |
| 521 | // an incorrect error code for bonding failures and if the pin |
| 522 | // is entered wrongly twice we should abort. |
| 523 | if (attempt > 0 && attempt <= 2) { |
| 524 | mBondState.attempt(address); |
Jaikumar Ganesh | a224f70 | 2010-09-10 15:09:54 -0700 | [diff] [blame] | 525 | createBond(address); |
| 526 | return; |
| 527 | } |
Jaikumar Ganesh | 8adcacb | 2011-07-21 13:49:54 -0700 | [diff] [blame] | 528 | if (attempt > 0) mBondState.clearPinAttempts(address); |
Jaikumar Ganesh | a224f70 | 2010-09-10 15:09:54 -0700 | [diff] [blame] | 529 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | }; |
| 533 | |
Jaikumar Ganesh | 50b40ce | 2011-02-02 14:44:49 -0800 | [diff] [blame] | 534 | private synchronized void addReservedSdpRecords(final ArrayList<ParcelUuid> uuids) { |
| 535 | //Register SDP records. |
| 536 | int[] svcIdentifiers = new int[uuids.size()]; |
| 537 | for (int i = 0; i < uuids.size(); i++) { |
| 538 | svcIdentifiers[i] = BluetoothUuid.getServiceIdentifierFromParcelUuid(uuids.get(i)); |
| 539 | } |
| 540 | mAdapterSdpHandles = addReservedServiceRecordsNative(svcIdentifiers); |
| 541 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 542 | |
Jaikumar Ganesh | 50b40ce | 2011-02-02 14:44:49 -0800 | [diff] [blame] | 543 | private synchronized void updateSdpRecords() { |
| 544 | ArrayList<ParcelUuid> uuids = new ArrayList<ParcelUuid>(); |
| 545 | |
| 546 | // Add the default records |
| 547 | uuids.add(BluetoothUuid.HSP_AG); |
| 548 | uuids.add(BluetoothUuid.ObexObjectPush); |
| 549 | |
| 550 | if (mContext.getResources(). |
| 551 | getBoolean(com.android.internal.R.bool.config_voice_capable)) { |
| 552 | uuids.add(BluetoothUuid.Handsfree_AG); |
| 553 | uuids.add(BluetoothUuid.PBAP_PSE); |
| 554 | } |
| 555 | |
| 556 | // Add SDP records for profiles maintained by Android userspace |
| 557 | addReservedSdpRecords(uuids); |
| 558 | |
| 559 | // Enable profiles maintained by Bluez userspace. |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 560 | setBluetoothTetheringNative(true, BluetoothPanProfileHandler.NAP_ROLE, |
| 561 | BluetoothPanProfileHandler.NAP_BRIDGE); |
Jaikumar Ganesh | 50b40ce | 2011-02-02 14:44:49 -0800 | [diff] [blame] | 562 | |
| 563 | // Add SDP records for profiles maintained by Bluez userspace |
| 564 | uuids.add(BluetoothUuid.AudioSource); |
| 565 | uuids.add(BluetoothUuid.AvrcpTarget); |
| 566 | uuids.add(BluetoothUuid.NAP); |
| 567 | |
| 568 | // Cannot cast uuids.toArray directly since ParcelUuid is parcelable |
| 569 | mAdapterUuids = new ParcelUuid[uuids.size()]; |
| 570 | for (int i = 0; i < uuids.size(); i++) { |
| 571 | mAdapterUuids[i] = uuids.get(i); |
| 572 | } |
| 573 | } |
| 574 | |
Jaikumar Ganesh | 9efb134 | 2011-02-11 18:10:31 -0800 | [diff] [blame] | 575 | /** |
| 576 | * This function is called from Bluetooth Event Loop when onPropertyChanged |
| 577 | * for adapter comes in with UUID property. |
| 578 | * @param uuidsThe uuids of adapter as reported by Bluez. |
| 579 | */ |
Matthew Xie | 7f9ecca8 | 2011-07-15 13:03:58 -0700 | [diff] [blame] | 580 | /*package*/ synchronized void updateBluetoothState(String uuids) { |
| 581 | ParcelUuid[] adapterUuids = convertStringToParcelUuid(uuids); |
Jaikumar Ganesh | 50b40ce | 2011-02-02 14:44:49 -0800 | [diff] [blame] | 582 | |
Matthew Xie | 7f9ecca8 | 2011-07-15 13:03:58 -0700 | [diff] [blame] | 583 | if (mAdapterUuids != null && |
| 584 | BluetoothUuid.containsAllUuids(adapterUuids, mAdapterUuids)) { |
| 585 | mBluetoothState.sendMessage(BluetoothAdapterStateMachine.SERVICE_RECORD_LOADED); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | |
Matthew Xie | 7f9ecca8 | 2011-07-15 13:03:58 -0700 | [diff] [blame] | 589 | /** |
| 590 | * This method is called immediately after Bluetooth module is turned on. |
| 591 | * It starts auto-connection and places bluetooth on sign onto the battery |
| 592 | * stats |
| 593 | */ |
| 594 | /*package*/ void runBluetooth() { |
| 595 | mIsDiscovering = false; |
| 596 | mBondState.readAutoPairingData(); |
| 597 | mBondState.initBondState(); |
| 598 | initProfileState(); |
| 599 | |
| 600 | autoConnect(); |
| 601 | |
| 602 | // Log bluetooth on to battery stats. |
| 603 | long ident = Binder.clearCallingIdentity(); |
| 604 | try { |
| 605 | mBatteryStats.noteBluetoothOn(); |
| 606 | } catch (RemoteException e) { |
| 607 | Log.e(TAG, "", e); |
| 608 | } finally { |
| 609 | Binder.restoreCallingIdentity(ident); |
| 610 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | } |
| 612 | |
Jaikumar Ganesh | a224f70 | 2010-09-10 15:09:54 -0700 | [diff] [blame] | 613 | /*package*/ synchronized boolean attemptAutoPair(String address) { |
| 614 | if (!mBondState.hasAutoPairingFailed(address) && |
| 615 | !mBondState.isAutoPairingBlacklisted(address)) { |
| 616 | mBondState.attempt(address); |
| 617 | setPin(address, BluetoothDevice.convertPinToBytes("0000")); |
| 618 | return true; |
| 619 | } |
| 620 | return false; |
| 621 | } |
| 622 | |
Jaikumar Ganesh | f487d72 | 2011-01-12 10:12:01 -0800 | [diff] [blame] | 623 | /*package*/ synchronized boolean isFixedPinZerosAutoPairKeyboard(String address) { |
| 624 | // Check for keyboards which have fixed PIN 0000 as the pairing pin |
| 625 | return mBondState.isFixedPinZerosAutoPairKeyboard(address); |
| 626 | } |
| 627 | |
Jaikumar Ganesh | a224f70 | 2010-09-10 15:09:54 -0700 | [diff] [blame] | 628 | /*package*/ synchronized void onCreatePairedDeviceResult(String address, int result) { |
| 629 | if (result == BluetoothDevice.BOND_SUCCESS) { |
| 630 | setBondState(address, BluetoothDevice.BOND_BONDED); |
| 631 | if (mBondState.isAutoPairingAttemptsInProgress(address)) { |
| 632 | mBondState.clearPinAttempts(address); |
| 633 | } |
| 634 | } else if (result == BluetoothDevice.UNBOND_REASON_AUTH_FAILED && |
| 635 | mBondState.getAttempt(address) == 1) { |
| 636 | mBondState.addAutoPairingFailure(address); |
| 637 | pairingAttempt(address, result); |
| 638 | } else if (result == BluetoothDevice.UNBOND_REASON_REMOTE_DEVICE_DOWN && |
| 639 | mBondState.isAutoPairingAttemptsInProgress(address)) { |
| 640 | pairingAttempt(address, result); |
| 641 | } else { |
| 642 | setBondState(address, BluetoothDevice.BOND_NONE, result); |
| 643 | if (mBondState.isAutoPairingAttemptsInProgress(address)) { |
| 644 | mBondState.clearPinAttempts(address); |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | /*package*/ synchronized String getPendingOutgoingBonding() { |
| 650 | return mBondState.getPendingOutgoingBonding(); |
| 651 | } |
| 652 | |
| 653 | private void pairingAttempt(String address, int result) { |
| 654 | // This happens when our initial guess of "0000" as the pass key |
| 655 | // fails. Try to create the bond again and display the pin dialog |
| 656 | // to the user. Use back-off while posting the delayed |
| 657 | // message. The initial value is |
| 658 | // INIT_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY and the max value is |
| 659 | // MAX_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY. If the max value is |
| 660 | // reached, display an error to the user. |
| 661 | int attempt = mBondState.getAttempt(address); |
| 662 | if (attempt * INIT_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY > |
| 663 | MAX_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY) { |
| 664 | mBondState.clearPinAttempts(address); |
| 665 | setBondState(address, BluetoothDevice.BOND_NONE, result); |
| 666 | return; |
| 667 | } |
| 668 | |
| 669 | Message message = mHandler.obtainMessage(MESSAGE_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY); |
| 670 | message.obj = address; |
| 671 | boolean postResult = mHandler.sendMessageDelayed(message, |
| 672 | attempt * INIT_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY); |
| 673 | if (!postResult) { |
| 674 | mBondState.clearPinAttempts(address); |
| 675 | setBondState(address, |
| 676 | BluetoothDevice.BOND_NONE, result); |
| 677 | return; |
| 678 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 679 | } |
| 680 | |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 681 | /*package*/ BluetoothDevice getRemoteDevice(String address) { |
| 682 | return mAdapter.getRemoteDevice(address); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 683 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 684 | |
| 685 | private static String toBondStateString(int bondState) { |
| 686 | switch (bondState) { |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 687 | case BluetoothDevice.BOND_NONE: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 688 | return "not bonded"; |
| 689 | case BluetoothDevice.BOND_BONDING: |
| 690 | return "bonding"; |
| 691 | case BluetoothDevice.BOND_BONDED: |
| 692 | return "bonded"; |
| 693 | default: |
| 694 | return "??????"; |
| 695 | } |
| 696 | } |
| 697 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 698 | public synchronized boolean setName(String name) { |
| 699 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 700 | "Need BLUETOOTH_ADMIN permission"); |
| 701 | if (name == null) { |
| 702 | return false; |
| 703 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 704 | return setPropertyString("Name", name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 705 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 706 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 707 | //TODO(): setPropertyString, setPropertyInteger, setPropertyBoolean |
| 708 | // Either have a single property function with Object as the parameter |
| 709 | // or have a function for each property and then obfuscate in the JNI layer. |
| 710 | // The following looks dirty. |
| 711 | private boolean setPropertyString(String key, String value) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 712 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Jaikumar Ganesh | 8c9dd7d | 2009-11-16 16:23:20 -0800 | [diff] [blame] | 713 | if (!isEnabledInternal()) return false; |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 714 | return setAdapterPropertyStringNative(key, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 715 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 716 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 717 | private boolean setPropertyInteger(String key, int value) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 718 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Jaikumar Ganesh | 8c9dd7d | 2009-11-16 16:23:20 -0800 | [diff] [blame] | 719 | if (!isEnabledInternal()) return false; |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 720 | return setAdapterPropertyIntegerNative(key, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 721 | } |
| 722 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 723 | private boolean setPropertyBoolean(String key, boolean value) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 724 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Jaikumar Ganesh | 8c9dd7d | 2009-11-16 16:23:20 -0800 | [diff] [blame] | 725 | if (!isEnabledInternal()) return false; |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 726 | return setAdapterPropertyBooleanNative(key, value ? 1 : 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 727 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 728 | |
| 729 | /** |
| 730 | * Set the discoverability window for the device. A timeout of zero |
| 731 | * makes the device permanently discoverable (if the device is |
| 732 | * discoverable). Setting the timeout to a nonzero value does not make |
| 733 | * a device discoverable; you need to call setMode() to make the device |
| 734 | * explicitly discoverable. |
| 735 | * |
Jake Hamby | f51eada | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 736 | * @param timeout The discoverable timeout in seconds. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 737 | */ |
| 738 | public synchronized boolean setDiscoverableTimeout(int timeout) { |
| 739 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 740 | "Need BLUETOOTH_ADMIN permission"); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 741 | return setPropertyInteger("DiscoverableTimeout", timeout); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 742 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 743 | |
Nick Pelly | 1283547 | 2009-09-25 15:00:29 -0700 | [diff] [blame] | 744 | public synchronized boolean setScanMode(int mode, int duration) { |
Nick Pelly | 18b1e79 | 2009-09-24 11:14:15 -0700 | [diff] [blame] | 745 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS, |
| 746 | "Need WRITE_SECURE_SETTINGS permission"); |
Matthew Xie | 7f9ecca8 | 2011-07-15 13:03:58 -0700 | [diff] [blame] | 747 | return setScanMode(mode, duration, true); |
| 748 | } |
| 749 | |
| 750 | /** |
| 751 | * @param on true set the local Bluetooth module to be connectable |
| 752 | * but not dicoverable |
| 753 | * false set the local Bluetooth module to be not connectable |
| 754 | * and not dicoverable |
| 755 | */ |
| 756 | /*package*/ synchronized void switchConnectable(boolean on) { |
| 757 | if (on) { |
| 758 | // 0 is a dummy value, does not apply for SCAN_MODE_CONNECTABLE |
| 759 | setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE, 0, false); |
| 760 | } else { |
| 761 | // 0 is a dummy value, does not apply for SCAN_MODE_NONE |
| 762 | setScanMode(BluetoothAdapter.SCAN_MODE_NONE, 0, false); |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | private synchronized boolean setScanMode(int mode, int duration, boolean allowOnlyInOnState) { |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 767 | boolean pairable; |
| 768 | boolean discoverable; |
Nick Pelly | 1283547 | 2009-09-25 15:00:29 -0700 | [diff] [blame] | 769 | |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 770 | switch (mode) { |
| 771 | case BluetoothAdapter.SCAN_MODE_NONE: |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 772 | pairable = false; |
| 773 | discoverable = false; |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 774 | break; |
| 775 | case BluetoothAdapter.SCAN_MODE_CONNECTABLE: |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 776 | pairable = true; |
| 777 | discoverable = false; |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 778 | break; |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 779 | case BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE: |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 780 | pairable = true; |
| 781 | discoverable = true; |
Nick Pelly | 1283547 | 2009-09-25 15:00:29 -0700 | [diff] [blame] | 782 | if (DBG) Log.d(TAG, "BT Discoverable for " + duration + " seconds"); |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 783 | break; |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 784 | default: |
| 785 | Log.w(TAG, "Requested invalid scan mode " + mode); |
| 786 | return false; |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 787 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 788 | |
Matthew Xie | 7f9ecca8 | 2011-07-15 13:03:58 -0700 | [diff] [blame] | 789 | if (allowOnlyInOnState) { |
| 790 | setPropertyBoolean("Pairable", pairable); |
| 791 | setPropertyBoolean("Discoverable", discoverable); |
| 792 | } else { |
| 793 | // allowed to set the property through native layer directly |
| 794 | setAdapterPropertyBooleanNative("Pairable", pairable ? 1 : 0); |
| 795 | setAdapterPropertyBooleanNative("Discoverable", discoverable ? 1 : 0); |
| 796 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 797 | return true; |
| 798 | } |
| 799 | |
Jaikumar Ganesh | b148bc8 | 2009-11-20 13:41:07 -0800 | [diff] [blame] | 800 | /*package*/ synchronized String getProperty(String name) { |
| 801 | if (!isEnabledInternal()) return null; |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 802 | return mAdapterProperties.getProperty(name); |
Jaikumar Ganesh | b148bc8 | 2009-11-20 13:41:07 -0800 | [diff] [blame] | 803 | } |
| 804 | |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 805 | BluetoothAdapterProperties getAdapterProperties() { |
| 806 | return mAdapterProperties; |
| 807 | } |
| 808 | |
| 809 | BluetoothDeviceProperties getDeviceProperties() { |
| 810 | return mDeviceProperties; |
| 811 | } |
| 812 | |
| 813 | boolean isRemoteDeviceInCache(String address) { |
| 814 | return mDeviceProperties.isInCache(address); |
| 815 | } |
| 816 | |
| 817 | void setRemoteDeviceProperty(String address, String name, String value) { |
| 818 | mDeviceProperties.setProperty(address, name, value); |
| 819 | } |
| 820 | |
| 821 | void updateRemoteDevicePropertiesCache(String address) { |
| 822 | mDeviceProperties.updateCache(address); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | public synchronized String getAddress() { |
| 826 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
| 827 | return getProperty("Address"); |
| 828 | } |
| 829 | |
| 830 | public synchronized String getName() { |
| 831 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
| 832 | return getProperty("Name"); |
| 833 | } |
| 834 | |
Jaikumar Ganesh | 58b93c3 | 2010-11-23 20:03:10 -0800 | [diff] [blame] | 835 | public synchronized ParcelUuid[] getUuids() { |
| 836 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
| 837 | String value = getProperty("UUIDs"); |
| 838 | if (value == null) return null; |
Jaikumar Ganesh | 50b40ce | 2011-02-02 14:44:49 -0800 | [diff] [blame] | 839 | return convertStringToParcelUuid(value); |
| 840 | } |
Jaikumar Ganesh | 58b93c3 | 2010-11-23 20:03:10 -0800 | [diff] [blame] | 841 | |
Jaikumar Ganesh | 50b40ce | 2011-02-02 14:44:49 -0800 | [diff] [blame] | 842 | private synchronized ParcelUuid[] convertStringToParcelUuid(String value) { |
Jaikumar Ganesh | 58b93c3 | 2010-11-23 20:03:10 -0800 | [diff] [blame] | 843 | String[] uuidStrings = null; |
| 844 | // The UUIDs are stored as a "," separated string. |
| 845 | uuidStrings = value.split(","); |
| 846 | ParcelUuid[] uuids = new ParcelUuid[uuidStrings.length]; |
| 847 | |
| 848 | for (int i = 0; i < uuidStrings.length; i++) { |
| 849 | uuids[i] = ParcelUuid.fromString(uuidStrings[i]); |
| 850 | } |
| 851 | return uuids; |
Jaikumar Ganesh | 58b93c3 | 2010-11-23 20:03:10 -0800 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 855 | /** |
| 856 | * Returns the user-friendly name of a remote device. This value is |
| 857 | * returned from our local cache, which is updated when onPropertyChange |
| 858 | * event is received. |
| 859 | * Do not expect to retrieve the updated remote name immediately after |
| 860 | * changing the name on the remote device. |
| 861 | * |
| 862 | * @param address Bluetooth address of remote device. |
| 863 | * |
| 864 | * @return The user-friendly name of the specified remote device. |
| 865 | */ |
| 866 | public synchronized String getRemoteName(String address) { |
| 867 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 868 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 869 | return null; |
| 870 | } |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 871 | return mDeviceProperties.getProperty(address, "Name"); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 872 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 873 | |
| 874 | /** |
| 875 | * Get the discoverability window for the device. A timeout of zero |
| 876 | * means that the device is permanently discoverable (if the device is |
| 877 | * in the discoverable mode). |
| 878 | * |
| 879 | * @return The discoverability window of the device, in seconds. A negative |
| 880 | * value indicates an error. |
| 881 | */ |
| 882 | public synchronized int getDiscoverableTimeout() { |
| 883 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 884 | String timeout = getProperty("DiscoverableTimeout"); |
| 885 | if (timeout != null) |
| 886 | return Integer.valueOf(timeout); |
| 887 | else |
| 888 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 889 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 890 | |
| 891 | public synchronized int getScanMode() { |
| 892 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Jaikumar Ganesh | 8c9dd7d | 2009-11-16 16:23:20 -0800 | [diff] [blame] | 893 | if (!isEnabledInternal()) |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 894 | return BluetoothAdapter.SCAN_MODE_NONE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 895 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 896 | boolean pairable = getProperty("Pairable").equals("true"); |
| 897 | boolean discoverable = getProperty("Discoverable").equals("true"); |
| 898 | return bluezStringToScanMode (pairable, discoverable); |
| 899 | } |
| 900 | |
| 901 | public synchronized boolean startDiscovery() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 902 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 903 | "Need BLUETOOTH_ADMIN permission"); |
Jaikumar Ganesh | 8c9dd7d | 2009-11-16 16:23:20 -0800 | [diff] [blame] | 904 | if (!isEnabledInternal()) return false; |
| 905 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 906 | return startDiscoveryNative(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 907 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 908 | |
| 909 | public synchronized boolean cancelDiscovery() { |
| 910 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 911 | "Need BLUETOOTH_ADMIN permission"); |
Jaikumar Ganesh | 8c9dd7d | 2009-11-16 16:23:20 -0800 | [diff] [blame] | 912 | if (!isEnabledInternal()) return false; |
| 913 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 914 | return stopDiscoveryNative(); |
| 915 | } |
| 916 | |
| 917 | public synchronized boolean isDiscovering() { |
| 918 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
| 919 | return mIsDiscovering; |
| 920 | } |
| 921 | |
| 922 | /* package */ void setIsDiscovering(boolean isDiscovering) { |
| 923 | mIsDiscovering = isDiscovering; |
| 924 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 925 | |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 926 | private boolean isBondingFeasible(String address) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 927 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 928 | "Need BLUETOOTH_ADMIN permission"); |
Jaikumar Ganesh | 8c9dd7d | 2009-11-16 16:23:20 -0800 | [diff] [blame] | 929 | if (!isEnabledInternal()) return false; |
| 930 | |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 931 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 932 | return false; |
| 933 | } |
| 934 | address = address.toUpperCase(); |
| 935 | |
Jaikumar Ganesh | 2092361 | 2009-09-20 12:56:21 -0700 | [diff] [blame] | 936 | if (mBondState.getPendingOutgoingBonding() != null) { |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 937 | Log.d(TAG, "Ignoring createBond(): another device is bonding"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 938 | // a different device is currently bonding, fail |
| 939 | return false; |
| 940 | } |
| 941 | |
| 942 | // Check for bond state only if we are not performing auto |
| 943 | // pairing exponential back-off attempts. |
| 944 | if (!mBondState.isAutoPairingAttemptsInProgress(address) && |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 945 | mBondState.getBondState(address) != BluetoothDevice.BOND_NONE) { |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 946 | Log.d(TAG, "Ignoring createBond(): this device is already bonding or bonded"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 947 | return false; |
| 948 | } |
| 949 | |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 950 | if (address.equals(mDockAddress)) { |
| 951 | if (!writeDockPin()) { |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 952 | Log.e(TAG, "Error while writing Pin for the dock"); |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 953 | return false; |
| 954 | } |
| 955 | } |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 956 | return true; |
| 957 | } |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 958 | |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 959 | public synchronized boolean createBond(String address) { |
| 960 | if (!isBondingFeasible(address)) return false; |
| 961 | |
| 962 | if (!createPairedDeviceNative(address, 60000 /*1 minute*/ )) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 963 | return false; |
| 964 | } |
| 965 | |
Jaikumar Ganesh | 2092361 | 2009-09-20 12:56:21 -0700 | [diff] [blame] | 966 | mBondState.setPendingOutgoingBonding(address); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 967 | mBondState.setBondState(address, BluetoothDevice.BOND_BONDING); |
Jaikumar Ganesh | 2092361 | 2009-09-20 12:56:21 -0700 | [diff] [blame] | 968 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 969 | return true; |
| 970 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 971 | |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 972 | public synchronized boolean createBondOutOfBand(String address, byte[] hash, |
| 973 | byte[] randomizer) { |
| 974 | if (!isBondingFeasible(address)) return false; |
| 975 | |
| 976 | if (!createPairedDeviceOutOfBandNative(address, 60000 /* 1 minute */)) { |
| 977 | return false; |
| 978 | } |
| 979 | |
| 980 | setDeviceOutOfBandData(address, hash, randomizer); |
| 981 | mBondState.setPendingOutgoingBonding(address); |
| 982 | mBondState.setBondState(address, BluetoothDevice.BOND_BONDING); |
| 983 | |
| 984 | return true; |
| 985 | } |
| 986 | |
| 987 | public synchronized boolean setDeviceOutOfBandData(String address, byte[] hash, |
| 988 | byte[] randomizer) { |
| 989 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 990 | "Need BLUETOOTH_ADMIN permission"); |
| 991 | if (!isEnabledInternal()) return false; |
| 992 | |
| 993 | Pair <byte[], byte[]> value = new Pair<byte[], byte[]>(hash, randomizer); |
| 994 | |
| 995 | if (DBG) { |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 996 | Log.d(TAG, "Setting out of band data for: " + address + ":" + |
| 997 | Arrays.toString(hash) + ":" + Arrays.toString(randomizer)); |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | mDeviceOobData.put(address, value); |
| 1001 | return true; |
| 1002 | } |
| 1003 | |
| 1004 | Pair<byte[], byte[]> getDeviceOutOfBandData(BluetoothDevice device) { |
| 1005 | return mDeviceOobData.get(device.getAddress()); |
| 1006 | } |
| 1007 | |
| 1008 | |
| 1009 | public synchronized byte[] readOutOfBandData() { |
| 1010 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 1011 | "Need BLUETOOTH permission"); |
| 1012 | if (!isEnabledInternal()) return null; |
| 1013 | |
| 1014 | return readAdapterOutOfBandDataNative(); |
| 1015 | } |
| 1016 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1017 | public synchronized boolean cancelBondProcess(String address) { |
| 1018 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 1019 | "Need BLUETOOTH_ADMIN permission"); |
Jaikumar Ganesh | 8c9dd7d | 2009-11-16 16:23:20 -0800 | [diff] [blame] | 1020 | if (!isEnabledInternal()) return false; |
| 1021 | |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1022 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1023 | return false; |
| 1024 | } |
| 1025 | address = address.toUpperCase(); |
| 1026 | if (mBondState.getBondState(address) != BluetoothDevice.BOND_BONDING) { |
| 1027 | return false; |
| 1028 | } |
| 1029 | |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1030 | mBondState.setBondState(address, BluetoothDevice.BOND_NONE, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1031 | BluetoothDevice.UNBOND_REASON_AUTH_CANCELED); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 1032 | cancelDeviceCreationNative(address); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1033 | return true; |
| 1034 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1035 | |
| 1036 | public synchronized boolean removeBond(String address) { |
| 1037 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 1038 | "Need BLUETOOTH_ADMIN permission"); |
Jaikumar Ganesh | 8c9dd7d | 2009-11-16 16:23:20 -0800 | [diff] [blame] | 1039 | if (!isEnabledInternal()) return false; |
| 1040 | |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1041 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1042 | return false; |
| 1043 | } |
Jaikumar Ganesh | f1048cd | 2010-06-02 20:30:34 -0700 | [diff] [blame] | 1044 | BluetoothDeviceProfileState state = mDeviceProfileState.get(address); |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 1045 | if (state != null) { |
Jaikumar Ganesh | f1048cd | 2010-06-02 20:30:34 -0700 | [diff] [blame] | 1046 | state.sendMessage(BluetoothDeviceProfileState.UNPAIR); |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 1047 | return true; |
| 1048 | } else { |
| 1049 | return false; |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | public synchronized boolean removeBondInternal(String address) { |
Jaikumar Ganesh | cc2c066 | 2011-02-25 12:27:48 -0800 | [diff] [blame] | 1054 | // Unset the trusted device state and then unpair |
| 1055 | setTrust(address, false); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 1056 | return removeDeviceNative(getObjectPathFromAddress(address)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1057 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1058 | |
| 1059 | public synchronized String[] listBonds() { |
| 1060 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
| 1061 | return mBondState.listInState(BluetoothDevice.BOND_BONDED); |
| 1062 | } |
| 1063 | |
Jaikumar Ganesh | a224f70 | 2010-09-10 15:09:54 -0700 | [diff] [blame] | 1064 | /*package*/ synchronized String[] listInState(int state) { |
| 1065 | return mBondState.listInState(state); |
| 1066 | } |
| 1067 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1068 | public synchronized int getBondState(String address) { |
| 1069 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1070 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 1071 | return BluetoothDevice.ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1072 | } |
| 1073 | return mBondState.getBondState(address.toUpperCase()); |
| 1074 | } |
| 1075 | |
Jaikumar Ganesh | a224f70 | 2010-09-10 15:09:54 -0700 | [diff] [blame] | 1076 | /*package*/ synchronized boolean setBondState(String address, int state) { |
| 1077 | return setBondState(address, state, 0); |
| 1078 | } |
| 1079 | |
| 1080 | /*package*/ synchronized boolean setBondState(String address, int state, int reason) { |
Arek Lichwa | ae5fbb0 | 2011-02-23 11:52:34 +0100 | [diff] [blame] | 1081 | mBondState.setBondState(address.toUpperCase(), state, reason); |
Jaikumar Ganesh | a224f70 | 2010-09-10 15:09:54 -0700 | [diff] [blame] | 1082 | return true; |
| 1083 | } |
| 1084 | |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 1085 | public synchronized boolean isBluetoothDock(String address) { |
Jaikumar Ganesh | 6e9c443 | 2009-12-09 12:09:21 -0800 | [diff] [blame] | 1086 | SharedPreferences sp = mContext.getSharedPreferences(SHARED_PREFERENCES_NAME, |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 1087 | Context.MODE_PRIVATE); |
Jaikumar Ganesh | 6e9c443 | 2009-12-09 12:09:21 -0800 | [diff] [blame] | 1088 | |
| 1089 | return sp.contains(SHARED_PREFERENCE_DOCK_ADDRESS + address); |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 1090 | } |
| 1091 | |
Jaikumar Ganesh | 9488cbd | 2009-08-03 17:17:37 -0700 | [diff] [blame] | 1092 | /*package*/ String[] getRemoteDeviceProperties(String address) { |
Jaikumar Ganesh | 8c9dd7d | 2009-11-16 16:23:20 -0800 | [diff] [blame] | 1093 | if (!isEnabledInternal()) return null; |
| 1094 | |
Jaikumar Ganesh | 9488cbd | 2009-08-03 17:17:37 -0700 | [diff] [blame] | 1095 | String objectPath = getObjectPathFromAddress(address); |
| 1096 | return (String [])getDevicePropertiesNative(objectPath); |
| 1097 | } |
| 1098 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1099 | /** |
Lixin Yue | efa1dd7 | 2009-08-31 15:55:13 +0800 | [diff] [blame] | 1100 | * Sets the remote device trust state. |
| 1101 | * |
| 1102 | * @return boolean to indicate operation success or fail |
| 1103 | */ |
| 1104 | public synchronized boolean setTrust(String address, boolean value) { |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1105 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
Nick Pelly | e6ee3be | 2009-10-08 23:27:28 +0200 | [diff] [blame] | 1106 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 1107 | "Need BLUETOOTH_ADMIN permission"); |
Lixin Yue | efa1dd7 | 2009-08-31 15:55:13 +0800 | [diff] [blame] | 1108 | return false; |
| 1109 | } |
| 1110 | |
Jaikumar Ganesh | 8c9dd7d | 2009-11-16 16:23:20 -0800 | [diff] [blame] | 1111 | if (!isEnabledInternal()) return false; |
| 1112 | |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 1113 | return setDevicePropertyBooleanNative( |
| 1114 | getObjectPathFromAddress(address), "Trusted", value ? 1 : 0); |
Lixin Yue | efa1dd7 | 2009-08-31 15:55:13 +0800 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | /** |
| 1118 | * Gets the remote device trust state as boolean. |
| 1119 | * Note: this value may be |
| 1120 | * retrieved from cache if we retrieved the data before * |
| 1121 | * |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 1122 | * @return boolean to indicate trusted or untrusted state |
Lixin Yue | efa1dd7 | 2009-08-31 15:55:13 +0800 | [diff] [blame] | 1123 | */ |
| 1124 | public synchronized boolean getTrustState(String address) { |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1125 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
Lixin Yue | efa1dd7 | 2009-08-31 15:55:13 +0800 | [diff] [blame] | 1126 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
| 1127 | return false; |
| 1128 | } |
| 1129 | |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 1130 | String val = mDeviceProperties.getProperty(address, "Trusted"); |
Lixin Yue | efa1dd7 | 2009-08-31 15:55:13 +0800 | [diff] [blame] | 1131 | if (val == null) { |
| 1132 | return false; |
| 1133 | } else { |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 1134 | return val.equals("true"); |
Lixin Yue | efa1dd7 | 2009-08-31 15:55:13 +0800 | [diff] [blame] | 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | /** |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 1139 | * Gets the remote major, minor classes encoded as a 32-bit |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1140 | * integer. |
| 1141 | * |
| 1142 | * Note: this value is retrieved from cache, because we get it during |
| 1143 | * remote-device discovery. |
| 1144 | * |
| 1145 | * @return 32-bit integer encoding the remote major, minor, and service |
| 1146 | * classes. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1147 | */ |
| 1148 | public synchronized int getRemoteClass(String address) { |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1149 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
Nick Pelly | ea600cc | 2009-03-31 14:56:26 -0700 | [diff] [blame] | 1150 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
| 1151 | return BluetoothClass.ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1152 | } |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 1153 | String val = mDeviceProperties.getProperty(address, "Class"); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 1154 | if (val == null) |
| 1155 | return BluetoothClass.ERROR; |
| 1156 | else { |
| 1157 | return Integer.valueOf(val); |
| 1158 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1159 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 1160 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1161 | |
| 1162 | /** |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 1163 | * Gets the UUIDs supported by the remote device |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1164 | * |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 1165 | * @return array of 128bit ParcelUuids |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1166 | */ |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 1167 | public synchronized ParcelUuid[] getRemoteUuids(String address) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1168 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1169 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1170 | return null; |
| 1171 | } |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 1172 | return getUuidFromCache(address); |
| 1173 | } |
| 1174 | |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 1175 | ParcelUuid[] getUuidFromCache(String address) { |
| 1176 | String value = mDeviceProperties.getProperty(address, "UUIDs"); |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 1177 | if (value == null) return null; |
| 1178 | |
| 1179 | String[] uuidStrings = null; |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 1180 | // The UUIDs are stored as a "," separated string. |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 1181 | uuidStrings = value.split(","); |
| 1182 | ParcelUuid[] uuids = new ParcelUuid[uuidStrings.length]; |
| 1183 | |
| 1184 | for (int i = 0; i < uuidStrings.length; i++) { |
| 1185 | uuids[i] = ParcelUuid.fromString(uuidStrings[i]); |
| 1186 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 1187 | return uuids; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1188 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1189 | |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1190 | /** |
| 1191 | * Connect and fetch new UUID's using SDP. |
| 1192 | * The UUID's found are broadcast as intents. |
| 1193 | * Optionally takes a uuid and callback to fetch the RFCOMM channel for the |
| 1194 | * a given uuid. |
| 1195 | * TODO: Don't wait UUID_INTENT_DELAY to broadcast UUID intents on success |
| 1196 | * TODO: Don't wait UUID_INTENT_DELAY to handle the failure case for |
| 1197 | * callback and broadcast intents. |
| 1198 | */ |
| 1199 | public synchronized boolean fetchRemoteUuids(String address, ParcelUuid uuid, |
| 1200 | IBluetoothCallback callback) { |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 1201 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Jaikumar Ganesh | 8c9dd7d | 2009-11-16 16:23:20 -0800 | [diff] [blame] | 1202 | if (!isEnabledInternal()) return false; |
| 1203 | |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 1204 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 1205 | return false; |
| 1206 | } |
| 1207 | |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1208 | RemoteService service = new RemoteService(address, uuid); |
| 1209 | if (uuid != null && mUuidCallbackTracker.get(service) != null) { |
| 1210 | // An SDP query for this address & uuid is already in progress |
| 1211 | // Do not add this callback for the uuid |
| 1212 | return false; |
| 1213 | } |
| 1214 | |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 1215 | if (mUuidIntentTracker.contains(address)) { |
| 1216 | // An SDP query for this address is already in progress |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1217 | // Add this uuid onto the in-progress SDP query |
| 1218 | if (uuid != null) { |
| 1219 | mUuidCallbackTracker.put(new RemoteService(address, uuid), callback); |
| 1220 | } |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 1221 | return true; |
| 1222 | } |
| 1223 | |
Jaikumar Ganesh | 421f010 | 2011-06-06 17:31:58 +0900 | [diff] [blame] | 1224 | // If the device is already created, we will |
| 1225 | // do the SDP on the callback of createDeviceNative. |
| 1226 | boolean ret= createDeviceNative(address); |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 1227 | |
| 1228 | mUuidIntentTracker.add(address); |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1229 | if (uuid != null) { |
| 1230 | mUuidCallbackTracker.put(new RemoteService(address, uuid), callback); |
| 1231 | } |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 1232 | |
| 1233 | Message message = mHandler.obtainMessage(MESSAGE_UUID_INTENT); |
| 1234 | message.obj = address; |
| 1235 | mHandler.sendMessageDelayed(message, UUID_INTENT_DELAY); |
| 1236 | return ret; |
| 1237 | } |
| 1238 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1239 | /** |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 1240 | * Gets the rfcomm channel associated with the UUID. |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1241 | * Pulls records from the cache only. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1242 | * |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 1243 | * @param address Address of the remote device |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 1244 | * @param uuid ParcelUuid of the service attribute |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1245 | * |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 1246 | * @return rfcomm channel associated with the service attribute |
Jaikumar Ganesh | 10eac97 | 2009-09-21 12:48:51 -0700 | [diff] [blame] | 1247 | * -1 on error |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1248 | */ |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 1249 | public int getRemoteServiceChannel(String address, ParcelUuid uuid) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1250 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Jaikumar Ganesh | 8c9dd7d | 2009-11-16 16:23:20 -0800 | [diff] [blame] | 1251 | if (!isEnabledInternal()) return -1; |
| 1252 | |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1253 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 1254 | return BluetoothDevice.ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1255 | } |
Jaikumar Ganesh | 10eac97 | 2009-09-21 12:48:51 -0700 | [diff] [blame] | 1256 | // Check if we are recovering from a crash. |
| 1257 | if (mDeviceProperties.isEmpty()) { |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 1258 | if (mDeviceProperties.updateCache(address) == null) |
Jaikumar Ganesh | 10eac97 | 2009-09-21 12:48:51 -0700 | [diff] [blame] | 1259 | return -1; |
| 1260 | } |
| 1261 | |
| 1262 | Map<ParcelUuid, Integer> value = mDeviceServiceChannelCache.get(address); |
| 1263 | if (value != null && value.containsKey(uuid)) |
| 1264 | return value.get(uuid); |
| 1265 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1266 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1267 | |
| 1268 | public synchronized boolean setPin(String address, byte[] pin) { |
| 1269 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 1270 | "Need BLUETOOTH_ADMIN permission"); |
Jaikumar Ganesh | 8c9dd7d | 2009-11-16 16:23:20 -0800 | [diff] [blame] | 1271 | if (!isEnabledInternal()) return false; |
| 1272 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1273 | if (pin == null || pin.length <= 0 || pin.length > 16 || |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1274 | !BluetoothAdapter.checkBluetoothAddress(address)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1275 | return false; |
| 1276 | } |
| 1277 | address = address.toUpperCase(); |
| 1278 | Integer data = mEventLoop.getPasskeyAgentRequestData().remove(address); |
| 1279 | if (data == null) { |
| 1280 | Log.w(TAG, "setPin(" + address + ") called but no native data available, " + |
| 1281 | "ignoring. Maybe the PasskeyAgent Request was cancelled by the remote device" + |
| 1282 | " or by bluez.\n"); |
| 1283 | return false; |
| 1284 | } |
| 1285 | // bluez API wants pin as a string |
| 1286 | String pinString; |
| 1287 | try { |
| 1288 | pinString = new String(pin, "UTF8"); |
| 1289 | } catch (UnsupportedEncodingException uee) { |
| 1290 | Log.e(TAG, "UTF8 not supported?!?"); |
| 1291 | return false; |
| 1292 | } |
| 1293 | return setPinNative(address, pinString, data.intValue()); |
| 1294 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1295 | |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 1296 | public synchronized boolean setPasskey(String address, int passkey) { |
| 1297 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 1298 | "Need BLUETOOTH_ADMIN permission"); |
Jaikumar Ganesh | 8c9dd7d | 2009-11-16 16:23:20 -0800 | [diff] [blame] | 1299 | if (!isEnabledInternal()) return false; |
| 1300 | |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1301 | if (passkey < 0 || passkey > 999999 || !BluetoothAdapter.checkBluetoothAddress(address)) { |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 1302 | return false; |
| 1303 | } |
| 1304 | address = address.toUpperCase(); |
| 1305 | Integer data = mEventLoop.getPasskeyAgentRequestData().remove(address); |
| 1306 | if (data == null) { |
| 1307 | Log.w(TAG, "setPasskey(" + address + ") called but no native data available, " + |
| 1308 | "ignoring. Maybe the PasskeyAgent Request was cancelled by the remote device" + |
| 1309 | " or by bluez.\n"); |
| 1310 | return false; |
| 1311 | } |
| 1312 | return setPasskeyNative(address, passkey, data.intValue()); |
| 1313 | } |
| 1314 | |
| 1315 | public synchronized boolean setPairingConfirmation(String address, boolean confirm) { |
| 1316 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 1317 | "Need BLUETOOTH_ADMIN permission"); |
Jaikumar Ganesh | 8c9dd7d | 2009-11-16 16:23:20 -0800 | [diff] [blame] | 1318 | if (!isEnabledInternal()) return false; |
| 1319 | |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 1320 | address = address.toUpperCase(); |
| 1321 | Integer data = mEventLoop.getPasskeyAgentRequestData().remove(address); |
| 1322 | if (data == null) { |
| 1323 | Log.w(TAG, "setPasskey(" + address + ") called but no native data available, " + |
| 1324 | "ignoring. Maybe the PasskeyAgent Request was cancelled by the remote device" + |
| 1325 | " or by bluez.\n"); |
| 1326 | return false; |
| 1327 | } |
| 1328 | return setPairingConfirmationNative(address, confirm, data.intValue()); |
| 1329 | } |
| 1330 | |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 1331 | public synchronized boolean setRemoteOutOfBandData(String address) { |
| 1332 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 1333 | "Need BLUETOOTH_ADMIN permission"); |
| 1334 | if (!isEnabledInternal()) return false; |
| 1335 | address = address.toUpperCase(); |
| 1336 | Integer data = mEventLoop.getPasskeyAgentRequestData().remove(address); |
| 1337 | if (data == null) { |
| 1338 | Log.w(TAG, "setRemoteOobData(" + address + ") called but no native data available, " + |
| 1339 | "ignoring. Maybe the PasskeyAgent Request was cancelled by the remote device" + |
| 1340 | " or by bluez.\n"); |
| 1341 | return false; |
| 1342 | } |
| 1343 | |
| 1344 | Pair<byte[], byte[]> val = mDeviceOobData.get(address); |
| 1345 | byte[] hash, randomizer; |
| 1346 | if (val == null) { |
| 1347 | // TODO: check what should be passed in this case. |
| 1348 | hash = new byte[16]; |
| 1349 | randomizer = new byte[16]; |
| 1350 | } else { |
| 1351 | hash = val.first; |
| 1352 | randomizer = val.second; |
| 1353 | } |
| 1354 | return setRemoteOutOfBandDataNative(address, hash, randomizer, data.intValue()); |
| 1355 | } |
| 1356 | |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 1357 | public synchronized boolean cancelPairingUserInput(String address) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1358 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 1359 | "Need BLUETOOTH_ADMIN permission"); |
Jaikumar Ganesh | 8c9dd7d | 2009-11-16 16:23:20 -0800 | [diff] [blame] | 1360 | if (!isEnabledInternal()) return false; |
| 1361 | |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1362 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1363 | return false; |
| 1364 | } |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1365 | mBondState.setBondState(address, BluetoothDevice.BOND_NONE, |
Jaikumar Ganesh | 397d8f4 | 2009-08-21 11:50:17 -0700 | [diff] [blame] | 1366 | BluetoothDevice.UNBOND_REASON_AUTH_CANCELED); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1367 | address = address.toUpperCase(); |
| 1368 | Integer data = mEventLoop.getPasskeyAgentRequestData().remove(address); |
| 1369 | if (data == null) { |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 1370 | Log.w(TAG, "cancelUserInputNative(" + address + ") called but no native data " + |
| 1371 | "available, ignoring. Maybe the PasskeyAgent Request was already cancelled " + |
| 1372 | "by the remote or by bluez.\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1373 | return false; |
| 1374 | } |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 1375 | return cancelPairingUserInputNative(address, data.intValue()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1376 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1377 | |
Jaikumar Ganesh | 8c9dd7d | 2009-11-16 16:23:20 -0800 | [diff] [blame] | 1378 | /*package*/ void updateDeviceServiceChannelCache(String address) { |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 1379 | if (DBG) Log.d(TAG, "updateDeviceServiceChannelCache(" + address + ")"); |
| 1380 | |
Jaikumar Ganesh | 10eac97 | 2009-09-21 12:48:51 -0700 | [diff] [blame] | 1381 | // We are storing the rfcomm channel numbers only for the uuids |
| 1382 | // we are interested in. |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 1383 | ParcelUuid[] deviceUuids = getRemoteUuids(address); |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1384 | |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 1385 | ArrayList<ParcelUuid> applicationUuids = new ArrayList<ParcelUuid>(); |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1386 | |
| 1387 | synchronized (this) { |
| 1388 | for (RemoteService service : mUuidCallbackTracker.keySet()) { |
| 1389 | if (service.address.equals(address)) { |
| 1390 | applicationUuids.add(service.uuid); |
| 1391 | } |
| 1392 | } |
| 1393 | } |
Jaikumar Ganesh | 10eac97 | 2009-09-21 12:48:51 -0700 | [diff] [blame] | 1394 | |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 1395 | Map <ParcelUuid, Integer> uuidToChannelMap = new HashMap<ParcelUuid, Integer>(); |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1396 | |
| 1397 | // Retrieve RFCOMM channel for default uuids |
| 1398 | for (ParcelUuid uuid : RFCOMM_UUIDS) { |
Jaikumar Ganesh | 10eac97 | 2009-09-21 12:48:51 -0700 | [diff] [blame] | 1399 | if (BluetoothUuid.isUuidPresent(deviceUuids, uuid)) { |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 1400 | int channel = getDeviceServiceChannelForUuid(address, uuid); |
| 1401 | uuidToChannelMap.put(uuid, channel); |
| 1402 | if (DBG) Log.d(TAG, "\tuuid(system): " + uuid + " " + channel); |
Jaikumar Ganesh | 10eac97 | 2009-09-21 12:48:51 -0700 | [diff] [blame] | 1403 | } |
| 1404 | } |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1405 | // Retrieve RFCOMM channel for application requested uuids |
| 1406 | for (ParcelUuid uuid : applicationUuids) { |
| 1407 | if (BluetoothUuid.isUuidPresent(deviceUuids, uuid)) { |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 1408 | int channel = getDeviceServiceChannelForUuid(address, uuid); |
| 1409 | uuidToChannelMap.put(uuid, channel); |
| 1410 | if (DBG) Log.d(TAG, "\tuuid(application): " + uuid + " " + channel); |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | synchronized (this) { |
| 1415 | // Make application callbacks |
| 1416 | for (Iterator<RemoteService> iter = mUuidCallbackTracker.keySet().iterator(); |
| 1417 | iter.hasNext();) { |
| 1418 | RemoteService service = iter.next(); |
| 1419 | if (service.address.equals(address)) { |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 1420 | if (uuidToChannelMap.containsKey(service.uuid)) { |
| 1421 | int channel = uuidToChannelMap.get(service.uuid); |
| 1422 | |
| 1423 | if (DBG) Log.d(TAG, "Making callback for " + service.uuid + |
| 1424 | " with result " + channel); |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1425 | IBluetoothCallback callback = mUuidCallbackTracker.get(service); |
| 1426 | if (callback != null) { |
| 1427 | try { |
| 1428 | callback.onRfcommChannelFound(channel); |
| 1429 | } catch (RemoteException e) {Log.e(TAG, "", e);} |
| 1430 | } |
| 1431 | |
| 1432 | iter.remove(); |
| 1433 | } |
| 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | // Update cache |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 1438 | mDeviceServiceChannelCache.put(address, uuidToChannelMap); |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1439 | } |
Jaikumar Ganesh | 10eac97 | 2009-09-21 12:48:51 -0700 | [diff] [blame] | 1440 | } |
| 1441 | |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 1442 | private int getDeviceServiceChannelForUuid(String address, |
| 1443 | ParcelUuid uuid) { |
| 1444 | return getDeviceServiceChannelNative(getObjectPathFromAddress(address), |
| 1445 | uuid.toString(), 0x0004); |
| 1446 | } |
| 1447 | |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 1448 | /** |
| 1449 | * b is a handle to a Binder instance, so that this service can be notified |
| 1450 | * for Applications that terminate unexpectedly, to clean there service |
| 1451 | * records |
| 1452 | */ |
| 1453 | public synchronized int addRfcommServiceRecord(String serviceName, ParcelUuid uuid, |
| 1454 | int channel, IBinder b) { |
Jaikumar Ganesh | 8c9dd7d | 2009-11-16 16:23:20 -0800 | [diff] [blame] | 1455 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
| 1456 | if (!isEnabledInternal()) return -1; |
| 1457 | |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 1458 | if (serviceName == null || uuid == null || channel < 1 || |
| 1459 | channel > BluetoothSocket.MAX_RFCOMM_CHANNEL) { |
| 1460 | return -1; |
| 1461 | } |
| 1462 | if (BluetoothUuid.isUuidPresent(BluetoothUuid.RESERVED_UUIDS, uuid)) { |
| 1463 | Log.w(TAG, "Attempted to register a reserved UUID: " + uuid); |
| 1464 | return -1; |
| 1465 | } |
| 1466 | int handle = addRfcommServiceRecordNative(serviceName, |
| 1467 | uuid.getUuid().getMostSignificantBits(), uuid.getUuid().getLeastSignificantBits(), |
| 1468 | (short)channel); |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 1469 | if (DBG) Log.d(TAG, "new handle " + Integer.toHexString(handle)); |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 1470 | if (handle == -1) { |
| 1471 | return -1; |
| 1472 | } |
| 1473 | |
| 1474 | int pid = Binder.getCallingPid(); |
| 1475 | mServiceRecordToPid.put(new Integer(handle), new Integer(pid)); |
| 1476 | try { |
Jaikumar Ganesh | ef2cb7c | 2011-07-21 18:13:38 -0700 | [diff] [blame^] | 1477 | b.linkToDeath(new Reaper(handle, pid, RFCOMM_RECORD_REAPER), 0); |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 1478 | } catch (RemoteException e) {} |
| 1479 | return handle; |
| 1480 | } |
| 1481 | |
| 1482 | public void removeServiceRecord(int handle) { |
| 1483 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 1484 | "Need BLUETOOTH permission"); |
| 1485 | checkAndRemoveRecord(handle, Binder.getCallingPid()); |
| 1486 | } |
| 1487 | |
| 1488 | private synchronized void checkAndRemoveRecord(int handle, int pid) { |
| 1489 | Integer handleInt = new Integer(handle); |
| 1490 | Integer owner = mServiceRecordToPid.get(handleInt); |
| 1491 | if (owner != null && pid == owner.intValue()) { |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 1492 | if (DBG) Log.d(TAG, "Removing service record " + |
| 1493 | Integer.toHexString(handle) + " for pid " + pid); |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 1494 | mServiceRecordToPid.remove(handleInt); |
| 1495 | removeServiceRecordNative(handle); |
| 1496 | } |
| 1497 | } |
| 1498 | |
| 1499 | private class Reaper implements IBinder.DeathRecipient { |
Jaikumar Ganesh | ef2cb7c | 2011-07-21 18:13:38 -0700 | [diff] [blame^] | 1500 | int mPid; |
| 1501 | int mHandle; |
| 1502 | int mType; |
| 1503 | |
| 1504 | Reaper(int handle, int pid, int type) { |
| 1505 | mPid = pid; |
| 1506 | mHandle = handle; |
| 1507 | mType = type; |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 1508 | } |
Jaikumar Ganesh | ef2cb7c | 2011-07-21 18:13:38 -0700 | [diff] [blame^] | 1509 | |
| 1510 | Reaper(int pid, int type) { |
| 1511 | mPid = pid; |
| 1512 | mType = type; |
| 1513 | } |
| 1514 | |
| 1515 | @Override |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 1516 | public void binderDied() { |
| 1517 | synchronized (BluetoothService.this) { |
Jaikumar Ganesh | ef2cb7c | 2011-07-21 18:13:38 -0700 | [diff] [blame^] | 1518 | if (DBG) Log.d(TAG, "Tracked app " + mPid + " died" + "Type:" + mType); |
| 1519 | if (mType == RFCOMM_RECORD_REAPER) { |
| 1520 | checkAndRemoveRecord(mHandle, mPid); |
| 1521 | } else if (mType == STATE_CHANGE_REAPER) { |
| 1522 | mStateChangeTracker.remove(mPid); |
| 1523 | } |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 1524 | } |
| 1525 | } |
| 1526 | } |
| 1527 | |
Jaikumar Ganesh | ef2cb7c | 2011-07-21 18:13:38 -0700 | [diff] [blame^] | 1528 | |
| 1529 | @Override |
| 1530 | public boolean changeApplicationBluetoothState(boolean on, |
| 1531 | IBluetoothStateChangeCallback callback, IBinder binder) { |
| 1532 | int pid = Binder.getCallingPid(); |
| 1533 | //mStateChangeTracker is a synchronized map |
| 1534 | if (!mStateChangeTracker.containsKey(pid)) { |
| 1535 | if (on) { |
| 1536 | mStateChangeTracker.put(pid, callback); |
| 1537 | } else { |
| 1538 | return false; |
| 1539 | } |
| 1540 | } else if (!on) { |
| 1541 | mStateChangeTracker.remove(pid); |
| 1542 | } |
| 1543 | |
| 1544 | if (binder != null) { |
| 1545 | try { |
| 1546 | binder.linkToDeath(new Reaper(pid, STATE_CHANGE_REAPER), 0); |
| 1547 | } catch (RemoteException e) { |
| 1548 | return false; |
| 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | int type; |
| 1553 | if (on) { |
| 1554 | type = BluetoothAdapterStateMachine.PER_PROCESS_TURN_ON; |
| 1555 | } else { |
| 1556 | type = BluetoothAdapterStateMachine.PER_PROCESS_TURN_OFF; |
| 1557 | } |
| 1558 | |
| 1559 | mBluetoothState.sendMessage(type, callback); |
| 1560 | return true; |
| 1561 | } |
| 1562 | |
| 1563 | boolean isApplicationStateChangeTrackerEmpty() { |
| 1564 | return mStateChangeTracker.isEmpty(); |
| 1565 | } |
| 1566 | |
| 1567 | void clearApplicationStateChangeTracker() { |
| 1568 | mStateChangeTracker.clear(); |
| 1569 | } |
| 1570 | |
| 1571 | Collection<IBluetoothStateChangeCallback> getApplicationStateChangeCallbacks() { |
| 1572 | return mStateChangeTracker.values(); |
| 1573 | } |
| 1574 | |
| 1575 | int getNumberOfApplicationStateChangeTrackers() { |
| 1576 | return mStateChangeTracker.size(); |
| 1577 | } |
| 1578 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1579 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 1580 | @Override |
| 1581 | public void onReceive(Context context, Intent intent) { |
Jaikumar Ganesh | 6e9c443 | 2009-12-09 12:09:21 -0800 | [diff] [blame] | 1582 | if (intent == null) return; |
| 1583 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1584 | String action = intent.getAction(); |
| 1585 | if (action.equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)) { |
| 1586 | ContentResolver resolver = context.getContentResolver(); |
| 1587 | // Query the airplane mode from Settings.System just to make sure that |
| 1588 | // some random app is not sending this intent and disabling bluetooth |
Matthew Xie | 7f9ecca8 | 2011-07-15 13:03:58 -0700 | [diff] [blame] | 1589 | if (isAirplaneModeOn()) { |
| 1590 | mBluetoothState.sendMessage(BluetoothAdapterStateMachine.AIRPLANE_MODE_ON); |
| 1591 | } else { |
| 1592 | mBluetoothState.sendMessage(BluetoothAdapterStateMachine.AIRPLANE_MODE_OFF); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1593 | } |
Jaikumar Ganesh | 6e9c443 | 2009-12-09 12:09:21 -0800 | [diff] [blame] | 1594 | } else if (Intent.ACTION_DOCK_EVENT.equals(action)) { |
| 1595 | int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, |
| 1596 | Intent.EXTRA_DOCK_STATE_UNDOCKED); |
| 1597 | if (DBG) Log.v(TAG, "Received ACTION_DOCK_EVENT with State:" + state); |
| 1598 | if (state == Intent.EXTRA_DOCK_STATE_UNDOCKED) { |
| 1599 | mDockAddress = null; |
| 1600 | mDockPin = null; |
| 1601 | } else { |
| 1602 | SharedPreferences.Editor editor = |
| 1603 | mContext.getSharedPreferences(SHARED_PREFERENCES_NAME, |
| 1604 | mContext.MODE_PRIVATE).edit(); |
| 1605 | editor.putBoolean(SHARED_PREFERENCE_DOCK_ADDRESS + mDockAddress, true); |
Brad Fitzpatrick | 66fce50 | 2010-08-30 18:10:49 -0700 | [diff] [blame] | 1606 | editor.apply(); |
Jaikumar Ganesh | 6e9c443 | 2009-12-09 12:09:21 -0800 | [diff] [blame] | 1607 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1608 | } |
| 1609 | } |
| 1610 | }; |
| 1611 | |
Jaikumar Ganesh | 6e9c443 | 2009-12-09 12:09:21 -0800 | [diff] [blame] | 1612 | private void registerForAirplaneMode(IntentFilter filter) { |
Jeff Sharkey | 4430392 | 2009-12-01 18:25:02 -0800 | [diff] [blame] | 1613 | final ContentResolver resolver = mContext.getContentResolver(); |
| 1614 | final String airplaneModeRadios = Settings.System.getString(resolver, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1615 | Settings.System.AIRPLANE_MODE_RADIOS); |
Jeff Sharkey | 4430392 | 2009-12-01 18:25:02 -0800 | [diff] [blame] | 1616 | final String toggleableRadios = Settings.System.getString(resolver, |
| 1617 | Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS); |
| 1618 | |
| 1619 | mIsAirplaneSensitive = airplaneModeRadios == null ? true : |
| 1620 | airplaneModeRadios.contains(Settings.System.RADIO_BLUETOOTH); |
| 1621 | mIsAirplaneToggleable = toggleableRadios == null ? false : |
| 1622 | toggleableRadios.contains(Settings.System.RADIO_BLUETOOTH); |
| 1623 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1624 | if (mIsAirplaneSensitive) { |
Jaikumar Ganesh | 6e9c443 | 2009-12-09 12:09:21 -0800 | [diff] [blame] | 1625 | filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1626 | } |
| 1627 | } |
| 1628 | |
| 1629 | /* Returns true if airplane mode is currently on */ |
| 1630 | private final boolean isAirplaneModeOn() { |
| 1631 | return Settings.System.getInt(mContext.getContentResolver(), |
| 1632 | Settings.System.AIRPLANE_MODE_ON, 0) == 1; |
| 1633 | } |
| 1634 | |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 1635 | /* Broadcast the Uuid intent */ |
| 1636 | /*package*/ synchronized void sendUuidIntent(String address) { |
Jaikumar Ganesh | 6179965 | 2009-09-20 16:01:21 -0700 | [diff] [blame] | 1637 | ParcelUuid[] uuid = getUuidFromCache(address); |
| 1638 | Intent intent = new Intent(BluetoothDevice.ACTION_UUID); |
Jaikumar Ganesh | 2d3b98d | 2009-09-21 16:11:01 -0700 | [diff] [blame] | 1639 | intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mAdapter.getRemoteDevice(address)); |
Jaikumar Ganesh | 6179965 | 2009-09-20 16:01:21 -0700 | [diff] [blame] | 1640 | intent.putExtra(BluetoothDevice.EXTRA_UUID, uuid); |
| 1641 | mContext.sendBroadcast(intent, BLUETOOTH_ADMIN_PERM); |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 1642 | mUuidIntentTracker.remove(address); |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | /*package*/ synchronized void makeServiceChannelCallbacks(String address) { |
| 1646 | for (Iterator<RemoteService> iter = mUuidCallbackTracker.keySet().iterator(); |
| 1647 | iter.hasNext();) { |
| 1648 | RemoteService service = iter.next(); |
| 1649 | if (service.address.equals(address)) { |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 1650 | if (DBG) Log.d(TAG, "Cleaning up failed UUID channel lookup: " |
| 1651 | + service.address + " " + service.uuid); |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1652 | IBluetoothCallback callback = mUuidCallbackTracker.get(service); |
| 1653 | if (callback != null) { |
| 1654 | try { |
| 1655 | callback.onRfcommChannelFound(-1); |
| 1656 | } catch (RemoteException e) {Log.e(TAG, "", e);} |
| 1657 | } |
| 1658 | |
| 1659 | iter.remove(); |
| 1660 | } |
| 1661 | } |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 1662 | } |
| 1663 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1664 | @Override |
| 1665 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Matthew Xie | 7f9ecca8 | 2011-07-15 13:03:58 -0700 | [diff] [blame] | 1666 | if (getBluetoothStateInternal() != BluetoothAdapter.STATE_ON) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1667 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1668 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1669 | |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 1670 | pw.println("mIsAirplaneSensitive = " + mIsAirplaneSensitive); |
Jeff Sharkey | 4430392 | 2009-12-01 18:25:02 -0800 | [diff] [blame] | 1671 | pw.println("mIsAirplaneToggleable = " + mIsAirplaneToggleable); |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 1672 | |
| 1673 | pw.println("Local address = " + getAddress()); |
| 1674 | pw.println("Local name = " + getName()); |
| 1675 | pw.println("isDiscovering() = " + isDiscovering()); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1676 | |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 1677 | mAdapter.getProfileProxy(mContext, |
| 1678 | mBluetoothProfileServiceListener, BluetoothProfile.HEADSET); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 1679 | mAdapter.getProfileProxy(mContext, |
| 1680 | mBluetoothProfileServiceListener, BluetoothProfile.INPUT_DEVICE); |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 1681 | mAdapter.getProfileProxy(mContext, |
| 1682 | mBluetoothProfileServiceListener, BluetoothProfile.PAN); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1683 | |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 1684 | dumpKnownDevices(pw); |
| 1685 | dumpAclConnectedDevices(pw); |
| 1686 | dumpHeadsetService(pw); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 1687 | dumpInputDeviceProfile(pw); |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 1688 | dumpPanProfile(pw); |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 1689 | dumpApplicationServiceRecords(pw); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 1690 | } |
| 1691 | |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 1692 | private void dumpHeadsetService(PrintWriter pw) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1693 | pw.println("\n--Headset Service--"); |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 1694 | if (mBluetoothHeadset != null) { |
Jaikumar Ganesh | bb0773f | 2010-11-11 15:10:46 -0800 | [diff] [blame] | 1695 | List<BluetoothDevice> deviceList = mBluetoothHeadset.getConnectedDevices(); |
| 1696 | if (deviceList.size() == 0) { |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 1697 | pw.println("No headsets connected"); |
Jaikumar Ganesh | bb0773f | 2010-11-11 15:10:46 -0800 | [diff] [blame] | 1698 | } else { |
| 1699 | BluetoothDevice device = deviceList.get(0); |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 1700 | pw.println("\ngetConnectedDevices[0] = " + device); |
| 1701 | dumpHeadsetConnectionState(pw, device); |
Jaikumar Ganesh | bb0773f | 2010-11-11 15:10:46 -0800 | [diff] [blame] | 1702 | pw.println("getBatteryUsageHint() = " + |
| 1703 | mBluetoothHeadset.getBatteryUsageHint(device)); |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 1704 | } |
| 1705 | |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 1706 | deviceList.clear(); |
| 1707 | deviceList = mBluetoothHeadset.getDevicesMatchingConnectionStates(new int[] { |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 1708 | BluetoothProfile.STATE_CONNECTED, BluetoothProfile.STATE_DISCONNECTED}); |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 1709 | pw.println("--Connected and Disconnected Headsets"); |
Jaikumar Ganesh | bb0773f | 2010-11-11 15:10:46 -0800 | [diff] [blame] | 1710 | for (BluetoothDevice device: deviceList) { |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 1711 | pw.println(device); |
| 1712 | if (mBluetoothHeadset.isAudioConnected(device)) { |
| 1713 | pw.println("SCO audio connected to device:" + device); |
| 1714 | } |
| 1715 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1716 | } |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 1717 | mAdapter.closeProfileProxy(BluetoothProfile.HEADSET, mBluetoothHeadset); |
| 1718 | } |
Nick Pelly | 6c901db | 2009-06-19 10:08:09 -0700 | [diff] [blame] | 1719 | |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 1720 | private void dumpInputDeviceProfile(PrintWriter pw) { |
| 1721 | pw.println("\n--Bluetooth Service- Input Device Profile"); |
| 1722 | if (mInputDevice != null) { |
| 1723 | List<BluetoothDevice> deviceList = mInputDevice.getConnectedDevices(); |
| 1724 | if (deviceList.size() == 0) { |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 1725 | pw.println("No input devices connected"); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 1726 | } else { |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 1727 | pw.println("Number of connected devices:" + deviceList.size()); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 1728 | BluetoothDevice device = deviceList.get(0); |
| 1729 | pw.println("getConnectedDevices[0] = " + device); |
| 1730 | pw.println("Priority of Connected device = " + mInputDevice.getPriority(device)); |
| 1731 | |
| 1732 | switch (mInputDevice.getConnectionState(device)) { |
| 1733 | case BluetoothInputDevice.STATE_CONNECTING: |
| 1734 | pw.println("getConnectionState() = STATE_CONNECTING"); |
| 1735 | break; |
| 1736 | case BluetoothInputDevice.STATE_CONNECTED: |
| 1737 | pw.println("getConnectionState() = STATE_CONNECTED"); |
| 1738 | break; |
| 1739 | case BluetoothInputDevice.STATE_DISCONNECTING: |
| 1740 | pw.println("getConnectionState() = STATE_DISCONNECTING"); |
| 1741 | break; |
| 1742 | } |
| 1743 | } |
| 1744 | deviceList.clear(); |
| 1745 | deviceList = mInputDevice.getDevicesMatchingConnectionStates(new int[] { |
| 1746 | BluetoothProfile.STATE_CONNECTED, BluetoothProfile.STATE_DISCONNECTED}); |
| 1747 | pw.println("--Connected and Disconnected input devices"); |
| 1748 | for (BluetoothDevice device: deviceList) { |
| 1749 | pw.println(device); |
| 1750 | } |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 1751 | } |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 1752 | mAdapter.closeProfileProxy(BluetoothProfile.INPUT_DEVICE, mBluetoothHeadset); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1753 | } |
| 1754 | |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 1755 | private void dumpPanProfile(PrintWriter pw) { |
| 1756 | pw.println("\n--Bluetooth Service- Pan Profile"); |
| 1757 | if (mPan != null) { |
| 1758 | List<BluetoothDevice> deviceList = mPan.getConnectedDevices(); |
| 1759 | if (deviceList.size() == 0) { |
| 1760 | pw.println("No Pan devices connected"); |
| 1761 | } else { |
| 1762 | pw.println("Number of connected devices:" + deviceList.size()); |
| 1763 | BluetoothDevice device = deviceList.get(0); |
| 1764 | pw.println("getConnectedDevices[0] = " + device); |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 1765 | |
| 1766 | switch (mPan.getConnectionState(device)) { |
| 1767 | case BluetoothInputDevice.STATE_CONNECTING: |
| 1768 | pw.println("getConnectionState() = STATE_CONNECTING"); |
| 1769 | break; |
| 1770 | case BluetoothInputDevice.STATE_CONNECTED: |
| 1771 | pw.println("getConnectionState() = STATE_CONNECTED"); |
| 1772 | break; |
| 1773 | case BluetoothInputDevice.STATE_DISCONNECTING: |
| 1774 | pw.println("getConnectionState() = STATE_DISCONNECTING"); |
| 1775 | break; |
| 1776 | } |
| 1777 | } |
| 1778 | deviceList.clear(); |
| 1779 | deviceList = mPan.getDevicesMatchingConnectionStates(new int[] { |
| 1780 | BluetoothProfile.STATE_CONNECTED, BluetoothProfile.STATE_DISCONNECTED}); |
| 1781 | pw.println("--Connected and Disconnected Pan devices"); |
| 1782 | for (BluetoothDevice device: deviceList) { |
| 1783 | pw.println(device); |
| 1784 | } |
| 1785 | } |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 1786 | } |
| 1787 | |
| 1788 | private void dumpHeadsetConnectionState(PrintWriter pw, |
| 1789 | BluetoothDevice device) { |
| 1790 | switch (mBluetoothHeadset.getConnectionState(device)) { |
| 1791 | case BluetoothHeadset.STATE_CONNECTING: |
| 1792 | pw.println("getConnectionState() = STATE_CONNECTING"); |
| 1793 | break; |
| 1794 | case BluetoothHeadset.STATE_CONNECTED: |
| 1795 | pw.println("getConnectionState() = STATE_CONNECTED"); |
| 1796 | break; |
| 1797 | case BluetoothHeadset.STATE_DISCONNECTING: |
| 1798 | pw.println("getConnectionState() = STATE_DISCONNECTING"); |
| 1799 | break; |
| 1800 | case BluetoothHeadset.STATE_AUDIO_CONNECTED: |
| 1801 | pw.println("getConnectionState() = STATE_AUDIO_CONNECTED"); |
| 1802 | break; |
| 1803 | } |
| 1804 | } |
| 1805 | |
| 1806 | private void dumpApplicationServiceRecords(PrintWriter pw) { |
| 1807 | pw.println("\n--Application Service Records--"); |
| 1808 | for (Integer handle : mServiceRecordToPid.keySet()) { |
| 1809 | Integer pid = mServiceRecordToPid.get(handle); |
| 1810 | pw.println("\tpid " + pid + " handle " + Integer.toHexString(handle)); |
| 1811 | } |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 1812 | mAdapter.closeProfileProxy(BluetoothProfile.PAN, mBluetoothHeadset); |
| 1813 | } |
| 1814 | |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 1815 | private void dumpAclConnectedDevices(PrintWriter pw) { |
| 1816 | String[] devicesObjectPath = getKnownDevices(); |
| 1817 | pw.println("\n--ACL connected devices--"); |
| 1818 | if (devicesObjectPath != null) { |
| 1819 | for (String device : devicesObjectPath) { |
| 1820 | pw.println(getAddressFromObjectPath(device)); |
| 1821 | } |
| 1822 | } |
| 1823 | } |
| 1824 | |
| 1825 | private void dumpKnownDevices(PrintWriter pw) { |
| 1826 | pw.println("\n--Known devices--"); |
| 1827 | for (String address : mDeviceProperties.keySet()) { |
| 1828 | int bondState = mBondState.getBondState(address); |
| 1829 | pw.printf("%s %10s (%d) %s\n", address, |
| 1830 | toBondStateString(bondState), |
| 1831 | mBondState.getAttempt(address), |
| 1832 | getRemoteName(address)); |
| 1833 | |
| 1834 | Map<ParcelUuid, Integer> uuidChannels = mDeviceServiceChannelCache.get(address); |
| 1835 | if (uuidChannels == null) { |
| 1836 | pw.println("\tuuids = null"); |
| 1837 | } else { |
| 1838 | for (ParcelUuid uuid : uuidChannels.keySet()) { |
| 1839 | Integer channel = uuidChannels.get(uuid); |
| 1840 | if (channel == null) { |
| 1841 | pw.println("\t" + uuid); |
| 1842 | } else { |
| 1843 | pw.println("\t" + uuid + " RFCOMM channel = " + channel); |
| 1844 | } |
| 1845 | } |
| 1846 | } |
| 1847 | for (RemoteService service : mUuidCallbackTracker.keySet()) { |
| 1848 | if (service.address.equals(address)) { |
| 1849 | pw.println("\tPENDING CALLBACK: " + service.uuid); |
| 1850 | } |
| 1851 | } |
| 1852 | } |
| 1853 | } |
| 1854 | |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 1855 | private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener = |
| 1856 | new BluetoothProfile.ServiceListener() { |
| 1857 | public void onServiceConnected(int profile, BluetoothProfile proxy) { |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 1858 | if (profile == BluetoothProfile.HEADSET) { |
| 1859 | mBluetoothHeadset = (BluetoothHeadset) proxy; |
| 1860 | } else if (profile == BluetoothProfile.INPUT_DEVICE) { |
| 1861 | mInputDevice = (BluetoothInputDevice) proxy; |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 1862 | } else if (profile == BluetoothProfile.PAN) { |
| 1863 | mPan = (BluetoothPan) proxy; |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 1864 | } |
| 1865 | } |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 1866 | public void onServiceDisconnected(int profile) { |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 1867 | if (profile == BluetoothProfile.HEADSET) { |
| 1868 | mBluetoothHeadset = null; |
| 1869 | } else if (profile == BluetoothProfile.INPUT_DEVICE) { |
| 1870 | mInputDevice = null; |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 1871 | } else if (profile == BluetoothProfile.PAN) { |
| 1872 | mPan = null; |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 1873 | } |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 1874 | } |
| 1875 | }; |
| 1876 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 1877 | /* package */ static int bluezStringToScanMode(boolean pairable, boolean discoverable) { |
| 1878 | if (pairable && discoverable) |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1879 | return BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE; |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 1880 | else if (pairable && !discoverable) |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1881 | return BluetoothAdapter.SCAN_MODE_CONNECTABLE; |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 1882 | else |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1883 | return BluetoothAdapter.SCAN_MODE_NONE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1884 | } |
| 1885 | |
| 1886 | /* package */ static String scanModeToBluezString(int mode) { |
| 1887 | switch (mode) { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1888 | case BluetoothAdapter.SCAN_MODE_NONE: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1889 | return "off"; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1890 | case BluetoothAdapter.SCAN_MODE_CONNECTABLE: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1891 | return "connectable"; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1892 | case BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1893 | return "discoverable"; |
| 1894 | } |
| 1895 | return null; |
| 1896 | } |
| 1897 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 1898 | /*package*/ String getAddressFromObjectPath(String objectPath) { |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 1899 | String adapterObjectPath = mAdapterProperties.getObjectPath(); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 1900 | if (adapterObjectPath == null || objectPath == null) { |
Jake Hamby | f51eada | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 1901 | Log.e(TAG, "getAddressFromObjectPath: AdapterObjectPath:" + adapterObjectPath + |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 1902 | " or deviceObjectPath:" + objectPath + " is null"); |
| 1903 | return null; |
| 1904 | } |
| 1905 | if (!objectPath.startsWith(adapterObjectPath)) { |
Jake Hamby | f51eada | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 1906 | Log.e(TAG, "getAddressFromObjectPath: AdapterObjectPath:" + adapterObjectPath + |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 1907 | " is not a prefix of deviceObjectPath:" + objectPath + |
| 1908 | "bluetoothd crashed ?"); |
| 1909 | return null; |
| 1910 | } |
| 1911 | String address = objectPath.substring(adapterObjectPath.length()); |
| 1912 | if (address != null) return address.replace('_', ':'); |
| 1913 | |
| 1914 | Log.e(TAG, "getAddressFromObjectPath: Address being returned is null"); |
| 1915 | return null; |
| 1916 | } |
| 1917 | |
| 1918 | /*package*/ String getObjectPathFromAddress(String address) { |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 1919 | String path = mAdapterProperties.getObjectPath(); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 1920 | if (path == null) { |
| 1921 | Log.e(TAG, "Error: Object Path is null"); |
| 1922 | return null; |
| 1923 | } |
| 1924 | path = path + address.replace(":", "_"); |
| 1925 | return path; |
| 1926 | } |
| 1927 | |
Jaikumar Ganesh | b7e029d | 2010-03-09 15:31:24 -0800 | [diff] [blame] | 1928 | /*package */ void setLinkTimeout(String address, int num_slots) { |
| 1929 | String path = getObjectPathFromAddress(address); |
| 1930 | boolean result = setLinkTimeoutNative(path, num_slots); |
| 1931 | |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 1932 | if (!result) Log.d(TAG, "Set Link Timeout to " + num_slots + " slots failed"); |
Jaikumar Ganesh | b7e029d | 2010-03-09 15:31:24 -0800 | [diff] [blame] | 1933 | } |
| 1934 | |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 1935 | /**** Handlers for PAN Profile ****/ |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 1936 | // TODO: This needs to be converted to a state machine. |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 1937 | |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 1938 | public boolean isTetheringOn() { |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 1939 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 1940 | synchronized (mBluetoothPanProfileHandler) { |
| 1941 | return mBluetoothPanProfileHandler.isTetheringOn(); |
| 1942 | } |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 1943 | } |
| 1944 | |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 1945 | /*package*/boolean allowIncomingTethering() { |
| 1946 | synchronized (mBluetoothPanProfileHandler) { |
| 1947 | return mBluetoothPanProfileHandler.allowIncomingTethering(); |
| 1948 | } |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 1949 | } |
| 1950 | |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 1951 | public void setBluetoothTethering(boolean value) { |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 1952 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 1953 | synchronized (mBluetoothPanProfileHandler) { |
| 1954 | mBluetoothPanProfileHandler.setBluetoothTethering(value); |
| 1955 | } |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 1956 | } |
| 1957 | |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 1958 | public int getPanDeviceConnectionState(BluetoothDevice device) { |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 1959 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 1960 | synchronized (mBluetoothPanProfileHandler) { |
| 1961 | return mBluetoothPanProfileHandler.getPanDeviceConnectionState(device); |
| 1962 | } |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 1963 | } |
| 1964 | |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 1965 | public boolean connectPanDevice(BluetoothDevice device) { |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 1966 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 1967 | "Need BLUETOOTH_ADMIN permission"); |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 1968 | synchronized (mBluetoothPanProfileHandler) { |
| 1969 | return mBluetoothPanProfileHandler.connectPanDevice(device); |
| 1970 | } |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 1971 | } |
| 1972 | |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 1973 | public List<BluetoothDevice> getConnectedPanDevices() { |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 1974 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 1975 | synchronized (mBluetoothPanProfileHandler) { |
| 1976 | return mBluetoothPanProfileHandler.getConnectedPanDevices(); |
| 1977 | } |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 1978 | } |
| 1979 | |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 1980 | public List<BluetoothDevice> getPanDevicesMatchingConnectionStates( |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 1981 | int[] states) { |
| 1982 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 1983 | synchronized (mBluetoothPanProfileHandler) { |
| 1984 | return mBluetoothPanProfileHandler.getPanDevicesMatchingConnectionStates(states); |
| 1985 | } |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 1986 | } |
| 1987 | |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 1988 | public boolean disconnectPanDevice(BluetoothDevice device) { |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 1989 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 1990 | "Need BLUETOOTH_ADMIN permission"); |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 1991 | synchronized (mBluetoothPanProfileHandler) { |
| 1992 | return mBluetoothPanProfileHandler.disconnectPanDevice(device); |
| 1993 | } |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 1994 | } |
| 1995 | |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 1996 | /*package*/void handlePanDeviceStateChange(BluetoothDevice device, |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 1997 | String iface, |
| 1998 | int state, |
| 1999 | int role) { |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 2000 | synchronized (mBluetoothPanProfileHandler) { |
| 2001 | mBluetoothPanProfileHandler.handlePanDeviceStateChange(device, iface, state, role); |
| 2002 | } |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 2003 | } |
| 2004 | |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 2005 | /*package*/void handlePanDeviceStateChange(BluetoothDevice device, |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 2006 | int state, int role) { |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 2007 | synchronized (mBluetoothPanProfileHandler) { |
| 2008 | mBluetoothPanProfileHandler.handlePanDeviceStateChange(device, null, state, role); |
| 2009 | } |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 2010 | } |
| 2011 | |
| 2012 | /**** Handlers for Input Device Profile ****/ |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 2013 | // This needs to be converted to state machine |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 2014 | |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 2015 | public boolean connectInputDevice(BluetoothDevice device) { |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 2016 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 2017 | "Need BLUETOOTH_ADMIN permission"); |
| 2018 | BluetoothDeviceProfileState state = mDeviceProfileState.get(device.getAddress()); |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 2019 | synchronized (mBluetoothInputProfileHandler) { |
| 2020 | return mBluetoothInputProfileHandler.connectInputDevice(device, state); |
| 2021 | } |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 2022 | } |
| 2023 | |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 2024 | public boolean connectInputDeviceInternal(BluetoothDevice device) { |
| 2025 | synchronized (mBluetoothInputProfileHandler) { |
| 2026 | return mBluetoothInputProfileHandler.connectInputDeviceInternal(device); |
| 2027 | } |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 2028 | } |
| 2029 | |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 2030 | public boolean disconnectInputDevice(BluetoothDevice device) { |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 2031 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 2032 | "Need BLUETOOTH_ADMIN permission"); |
| 2033 | BluetoothDeviceProfileState state = mDeviceProfileState.get(device.getAddress()); |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 2034 | synchronized (mBluetoothInputProfileHandler) { |
| 2035 | return mBluetoothInputProfileHandler.disconnectInputDevice(device, state); |
| 2036 | } |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 2037 | } |
| 2038 | |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 2039 | public boolean disconnectInputDeviceInternal(BluetoothDevice device) { |
| 2040 | synchronized (mBluetoothInputProfileHandler) { |
| 2041 | return mBluetoothInputProfileHandler.disconnectInputDeviceInternal(device); |
| 2042 | } |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 2043 | } |
| 2044 | |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 2045 | public int getInputDeviceConnectionState(BluetoothDevice device) { |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 2046 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 2047 | synchronized (mBluetoothInputProfileHandler) { |
| 2048 | return mBluetoothInputProfileHandler.getInputDeviceConnectionState(device); |
| 2049 | } |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 2050 | } |
| 2051 | |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 2052 | public List<BluetoothDevice> getConnectedInputDevices() { |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 2053 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 2054 | synchronized (mBluetoothInputProfileHandler) { |
| 2055 | return mBluetoothInputProfileHandler.getConnectedInputDevices(); |
| 2056 | } |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 2057 | } |
| 2058 | |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 2059 | public List<BluetoothDevice> getInputDevicesMatchingConnectionStates( |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 2060 | int[] states) { |
| 2061 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 2062 | synchronized (mBluetoothInputProfileHandler) { |
| 2063 | return mBluetoothInputProfileHandler.getInputDevicesMatchingConnectionStates(states); |
| 2064 | } |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 2065 | } |
| 2066 | |
| 2067 | |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 2068 | public int getInputDevicePriority(BluetoothDevice device) { |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 2069 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 2070 | synchronized (mBluetoothInputProfileHandler) { |
| 2071 | return mBluetoothInputProfileHandler.getInputDevicePriority(device); |
| 2072 | } |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 2073 | } |
| 2074 | |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 2075 | public boolean setInputDevicePriority(BluetoothDevice device, int priority) { |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 2076 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 2077 | "Need BLUETOOTH_ADMIN permission"); |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 2078 | synchronized (mBluetoothInputProfileHandler) { |
| 2079 | return mBluetoothInputProfileHandler.setInputDevicePriority(device, priority); |
| 2080 | } |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 2081 | } |
| 2082 | |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 2083 | public boolean allowIncomingHidConnect(BluetoothDevice device, boolean allow) { |
| 2084 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 2085 | "Need BLUETOOTH_ADMIN permission"); |
| 2086 | String address = device.getAddress(); |
| 2087 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 2088 | return false; |
| 2089 | } |
| 2090 | |
| 2091 | Integer data = getAuthorizationAgentRequestData(address); |
| 2092 | if (data == null) { |
| 2093 | Log.w(TAG, "allowIncomingHidConnect(" + device + |
| 2094 | ") called but no native data available"); |
| 2095 | return false; |
| 2096 | } |
| 2097 | if (DBG) log("allowIncomingHidConnect: " + device + " : " + allow + " : " + data); |
| 2098 | return setAuthorizationNative(address, allow, data.intValue()); |
| 2099 | } |
| 2100 | |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 2101 | /*package*/List<BluetoothDevice> lookupInputDevicesMatchingStates(int[] states) { |
| 2102 | synchronized (mBluetoothInputProfileHandler) { |
| 2103 | return mBluetoothInputProfileHandler.lookupInputDevicesMatchingStates(states); |
| 2104 | } |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 2105 | } |
| 2106 | |
Jaikumar Ganesh | 6003fe9 | 2011-04-08 10:32:48 -0700 | [diff] [blame] | 2107 | /*package*/void handleInputDevicePropertyChange(String address, boolean connected) { |
| 2108 | synchronized (mBluetoothInputProfileHandler) { |
| 2109 | mBluetoothInputProfileHandler.handleInputDevicePropertyChange(address, connected); |
| 2110 | } |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 2111 | } |
| 2112 | |
Jaikumar Ganesh | 2ea1e85 | 2011-04-01 16:33:09 -0700 | [diff] [blame] | 2113 | /**** Handlers for Health Device Profile ****/ |
| 2114 | // TODO: All these need to be converted to a state machine. |
| 2115 | |
Jaikumar Ganesh | fb658c7 | 2011-07-06 17:37:02 -0700 | [diff] [blame] | 2116 | public boolean registerAppConfiguration(BluetoothHealthAppConfiguration config, |
| 2117 | IBluetoothHealthCallback callback) { |
Jaikumar Ganesh | 2ea1e85 | 2011-04-01 16:33:09 -0700 | [diff] [blame] | 2118 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 2119 | "Need BLUETOOTH permission"); |
| 2120 | synchronized (mBluetoothHealthProfileHandler) { |
Jaikumar Ganesh | fb658c7 | 2011-07-06 17:37:02 -0700 | [diff] [blame] | 2121 | return mBluetoothHealthProfileHandler.registerAppConfiguration(config, callback); |
Jaikumar Ganesh | 2ea1e85 | 2011-04-01 16:33:09 -0700 | [diff] [blame] | 2122 | } |
| 2123 | } |
| 2124 | |
| 2125 | public boolean unregisterAppConfiguration(BluetoothHealthAppConfiguration config) { |
| 2126 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 2127 | "Need BLUETOOTH permission"); |
| 2128 | synchronized (mBluetoothHealthProfileHandler) { |
| 2129 | return mBluetoothHealthProfileHandler.unregisterAppConfiguration(config); |
| 2130 | } |
| 2131 | } |
| 2132 | |
| 2133 | |
| 2134 | public boolean connectChannelToSource(BluetoothDevice device, |
| 2135 | BluetoothHealthAppConfiguration config) { |
| 2136 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 2137 | "Need BLUETOOTH permission"); |
| 2138 | synchronized (mBluetoothHealthProfileHandler) { |
| 2139 | return mBluetoothHealthProfileHandler.connectChannelToSource(device, |
| 2140 | config); |
| 2141 | } |
| 2142 | } |
| 2143 | |
| 2144 | public boolean connectChannelToSink(BluetoothDevice device, |
| 2145 | BluetoothHealthAppConfiguration config, int channelType) { |
| 2146 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 2147 | "Need BLUETOOTH permission"); |
| 2148 | synchronized (mBluetoothHealthProfileHandler) { |
| 2149 | return mBluetoothHealthProfileHandler.connectChannel(device, config, |
| 2150 | channelType); |
| 2151 | } |
| 2152 | } |
| 2153 | |
| 2154 | public boolean disconnectChannel(BluetoothDevice device, |
| 2155 | BluetoothHealthAppConfiguration config, ParcelFileDescriptor fd) { |
| 2156 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 2157 | "Need BLUETOOTH permission"); |
| 2158 | synchronized (mBluetoothHealthProfileHandler) { |
| 2159 | return mBluetoothHealthProfileHandler.disconnectChannel(device, config, fd); |
| 2160 | } |
| 2161 | } |
| 2162 | |
| 2163 | public ParcelFileDescriptor getMainChannelFd(BluetoothDevice device, |
| 2164 | BluetoothHealthAppConfiguration config) { |
| 2165 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 2166 | "Need BLUETOOTH permission"); |
| 2167 | synchronized (mBluetoothHealthProfileHandler) { |
| 2168 | return mBluetoothHealthProfileHandler.getMainChannelFd(device, config); |
| 2169 | } |
| 2170 | } |
| 2171 | |
| 2172 | /*package*/ void onHealthDevicePropertyChanged(String devicePath, |
| 2173 | String channelPath) { |
| 2174 | synchronized (mBluetoothHealthProfileHandler) { |
| 2175 | mBluetoothHealthProfileHandler.onHealthDevicePropertyChanged(devicePath, |
| 2176 | channelPath); |
| 2177 | } |
| 2178 | } |
| 2179 | |
| 2180 | /*package*/ void onHealthDeviceChannelChanged(String devicePath, |
| 2181 | String channelPath, boolean exists) { |
| 2182 | synchronized(mBluetoothHealthProfileHandler) { |
| 2183 | mBluetoothHealthProfileHandler.onHealthDeviceChannelChanged(devicePath, |
| 2184 | channelPath, exists); |
| 2185 | } |
| 2186 | } |
| 2187 | |
| 2188 | public int getHealthDeviceConnectionState(BluetoothDevice device) { |
| 2189 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 2190 | "Need BLUETOOTH permission"); |
| 2191 | synchronized (mBluetoothHealthProfileHandler) { |
| 2192 | return mBluetoothHealthProfileHandler.getHealthDeviceConnectionState(device); |
| 2193 | } |
| 2194 | } |
| 2195 | |
| 2196 | public List<BluetoothDevice> getConnectedHealthDevices() { |
| 2197 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 2198 | "Need BLUETOOTH permission"); |
| 2199 | synchronized (mBluetoothHealthProfileHandler) { |
| 2200 | return mBluetoothHealthProfileHandler.getConnectedHealthDevices(); |
| 2201 | } |
| 2202 | } |
| 2203 | |
| 2204 | public List<BluetoothDevice> getHealthDevicesMatchingConnectionStates( |
| 2205 | int[] states) { |
| 2206 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 2207 | "Need BLUETOOTH permission"); |
| 2208 | synchronized (mBluetoothHealthProfileHandler) { |
| 2209 | return mBluetoothHealthProfileHandler. |
| 2210 | getHealthDevicesMatchingConnectionStates(states); |
| 2211 | } |
| 2212 | } |
| 2213 | |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 2214 | /*package*/boolean notifyIncomingHidConnection(String address) { |
| 2215 | BluetoothDeviceProfileState state = mDeviceProfileState.get(address); |
| 2216 | if (state == null) { |
| 2217 | return false; |
| 2218 | } |
| 2219 | Message msg = new Message(); |
| 2220 | msg.what = BluetoothDeviceProfileState.CONNECT_HID_INCOMING; |
| 2221 | state.sendMessage(msg); |
| 2222 | return true; |
| 2223 | } |
| 2224 | |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 2225 | public boolean connectHeadset(String address) { |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 2226 | if (getBondState(address) != BluetoothDevice.BOND_BONDED) return false; |
| 2227 | |
Jaikumar Ganesh | f1048cd | 2010-06-02 20:30:34 -0700 | [diff] [blame] | 2228 | BluetoothDeviceProfileState state = mDeviceProfileState.get(address); |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 2229 | if (state != null) { |
Jaikumar Ganesh | f1048cd | 2010-06-02 20:30:34 -0700 | [diff] [blame] | 2230 | Message msg = new Message(); |
| 2231 | msg.arg1 = BluetoothDeviceProfileState.CONNECT_HFP_OUTGOING; |
| 2232 | msg.obj = state; |
| 2233 | mHfpProfileState.sendMessage(msg); |
| 2234 | return true; |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 2235 | } |
| 2236 | return false; |
| 2237 | } |
| 2238 | |
| 2239 | public boolean disconnectHeadset(String address) { |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 2240 | if (getBondState(address) != BluetoothDevice.BOND_BONDED) return false; |
| 2241 | |
Jaikumar Ganesh | f1048cd | 2010-06-02 20:30:34 -0700 | [diff] [blame] | 2242 | BluetoothDeviceProfileState state = mDeviceProfileState.get(address); |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 2243 | if (state != null) { |
Jaikumar Ganesh | f1048cd | 2010-06-02 20:30:34 -0700 | [diff] [blame] | 2244 | Message msg = new Message(); |
| 2245 | msg.arg1 = BluetoothDeviceProfileState.DISCONNECT_HFP_OUTGOING; |
| 2246 | msg.obj = state; |
| 2247 | mHfpProfileState.sendMessage(msg); |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 2248 | return true; |
| 2249 | } |
| 2250 | return false; |
| 2251 | } |
| 2252 | |
| 2253 | public boolean connectSink(String address) { |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 2254 | if (getBondState(address) != BluetoothDevice.BOND_BONDED) return false; |
| 2255 | |
Jaikumar Ganesh | f1048cd | 2010-06-02 20:30:34 -0700 | [diff] [blame] | 2256 | BluetoothDeviceProfileState state = mDeviceProfileState.get(address); |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 2257 | if (state != null) { |
Jaikumar Ganesh | f1048cd | 2010-06-02 20:30:34 -0700 | [diff] [blame] | 2258 | Message msg = new Message(); |
| 2259 | msg.arg1 = BluetoothDeviceProfileState.CONNECT_A2DP_OUTGOING; |
| 2260 | msg.obj = state; |
| 2261 | mA2dpProfileState.sendMessage(msg); |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 2262 | return true; |
| 2263 | } |
| 2264 | return false; |
| 2265 | } |
| 2266 | |
Jaikumar Ganesh | f1048cd | 2010-06-02 20:30:34 -0700 | [diff] [blame] | 2267 | public boolean disconnectSink(String address) { |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 2268 | if (getBondState(address) != BluetoothDevice.BOND_BONDED) return false; |
| 2269 | |
Jaikumar Ganesh | f1048cd | 2010-06-02 20:30:34 -0700 | [diff] [blame] | 2270 | BluetoothDeviceProfileState state = mDeviceProfileState.get(address); |
| 2271 | if (state != null) { |
| 2272 | Message msg = new Message(); |
| 2273 | msg.arg1 = BluetoothDeviceProfileState.DISCONNECT_A2DP_OUTGOING; |
| 2274 | msg.obj = state; |
| 2275 | mA2dpProfileState.sendMessage(msg); |
| 2276 | return true; |
| 2277 | } |
| 2278 | return false; |
| 2279 | } |
| 2280 | |
Jaikumar Ganesh | d3728cb | 2011-07-19 15:16:18 -0700 | [diff] [blame] | 2281 | BluetoothDeviceProfileState addProfileState(String address, boolean setTrust) { |
Jaikumar Ganesh | f1048cd | 2010-06-02 20:30:34 -0700 | [diff] [blame] | 2282 | BluetoothDeviceProfileState state = mDeviceProfileState.get(address); |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 2283 | if (state != null) return state; |
| 2284 | |
Jaikumar Ganesh | d3728cb | 2011-07-19 15:16:18 -0700 | [diff] [blame] | 2285 | state = new BluetoothDeviceProfileState(mContext, address, this, mA2dpService, setTrust); |
Jaikumar Ganesh | f1048cd | 2010-06-02 20:30:34 -0700 | [diff] [blame] | 2286 | mDeviceProfileState.put(address, state); |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 2287 | state.start(); |
| 2288 | return state; |
| 2289 | } |
| 2290 | |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 2291 | void removeProfileState(String address) { |
Jaikumar Ganesh | f1048cd | 2010-06-02 20:30:34 -0700 | [diff] [blame] | 2292 | mDeviceProfileState.remove(address); |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 2293 | } |
| 2294 | |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 2295 | synchronized String[] getKnownDevices() { |
| 2296 | String[] bonds = null; |
| 2297 | String val = getProperty("Devices"); |
| 2298 | if (val != null) { |
| 2299 | bonds = val.split(","); |
| 2300 | } |
| 2301 | return bonds; |
| 2302 | } |
| 2303 | |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 2304 | private void initProfileState() { |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 2305 | String[] bonds = null; |
| 2306 | String val = mAdapterProperties.getProperty("Devices"); |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 2307 | if (val != null) { |
| 2308 | bonds = val.split(","); |
| 2309 | } |
| 2310 | if (bonds == null) { |
| 2311 | return; |
| 2312 | } |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 2313 | for (String path : bonds) { |
| 2314 | String address = getAddressFromObjectPath(path); |
Jaikumar Ganesh | d3728cb | 2011-07-19 15:16:18 -0700 | [diff] [blame] | 2315 | BluetoothDeviceProfileState state = addProfileState(address, false); |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 2316 | } |
| 2317 | } |
| 2318 | |
| 2319 | private void autoConnect() { |
| 2320 | String[] bonds = getKnownDevices(); |
| 2321 | if (bonds == null) { |
| 2322 | return; |
| 2323 | } |
| 2324 | for (String path : bonds) { |
| 2325 | String address = getAddressFromObjectPath(path); |
| 2326 | BluetoothDeviceProfileState state = mDeviceProfileState.get(address); |
| 2327 | if (state != null) { |
| 2328 | Message msg = new Message(); |
| 2329 | msg.what = BluetoothDeviceProfileState.AUTO_CONNECT_PROFILES; |
| 2330 | state.sendMessage(msg); |
| 2331 | } |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 2332 | } |
| 2333 | } |
| 2334 | |
| 2335 | public boolean notifyIncomingConnection(String address) { |
Jaikumar Ganesh | f1048cd | 2010-06-02 20:30:34 -0700 | [diff] [blame] | 2336 | BluetoothDeviceProfileState state = |
| 2337 | mDeviceProfileState.get(address); |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 2338 | if (state != null) { |
| 2339 | Message msg = new Message(); |
Jaikumar Ganesh | f1048cd | 2010-06-02 20:30:34 -0700 | [diff] [blame] | 2340 | msg.what = BluetoothDeviceProfileState.CONNECT_HFP_INCOMING; |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 2341 | state.sendMessage(msg); |
| 2342 | return true; |
| 2343 | } |
| 2344 | return false; |
| 2345 | } |
| 2346 | |
| 2347 | /*package*/ boolean notifyIncomingA2dpConnection(String address) { |
Jaikumar Ganesh | f1048cd | 2010-06-02 20:30:34 -0700 | [diff] [blame] | 2348 | BluetoothDeviceProfileState state = |
| 2349 | mDeviceProfileState.get(address); |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 2350 | if (state != null) { |
| 2351 | Message msg = new Message(); |
Jaikumar Ganesh | f1048cd | 2010-06-02 20:30:34 -0700 | [diff] [blame] | 2352 | msg.what = BluetoothDeviceProfileState.CONNECT_A2DP_INCOMING; |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 2353 | state.sendMessage(msg); |
| 2354 | return true; |
| 2355 | } |
| 2356 | return false; |
| 2357 | } |
| 2358 | |
| 2359 | /*package*/ void setA2dpService(BluetoothA2dpService a2dpService) { |
| 2360 | mA2dpService = a2dpService; |
| 2361 | } |
| 2362 | |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 2363 | /*package*/ Integer getAuthorizationAgentRequestData(String address) { |
| 2364 | Integer data = mEventLoop.getAuthorizationAgentRequestData().remove(address); |
| 2365 | return data; |
| 2366 | } |
| 2367 | |
Jaikumar Ganesh | 70a053b | 2010-10-13 15:54:30 -0700 | [diff] [blame] | 2368 | public void sendProfileStateMessage(int profile, int cmd) { |
| 2369 | Message msg = new Message(); |
| 2370 | msg.what = cmd; |
| 2371 | if (profile == BluetoothProfileState.HFP) { |
| 2372 | mHfpProfileState.sendMessage(msg); |
| 2373 | } else if (profile == BluetoothProfileState.A2DP) { |
| 2374 | mA2dpProfileState.sendMessage(msg); |
| 2375 | } |
| 2376 | } |
| 2377 | |
Jaikumar Ganesh | c53cab2 | 2010-10-26 16:02:26 -0700 | [diff] [blame] | 2378 | public int getAdapterConnectionState() { |
| 2379 | return mAdapterConnectionState; |
| 2380 | } |
| 2381 | |
Jaikumar Ganesh | a46f2fb | 2010-10-21 14:55:05 -0700 | [diff] [blame] | 2382 | public synchronized void sendConnectionStateChange(BluetoothDevice device, int state, |
| 2383 | int prevState) { |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 2384 | // Since this is a binder call check if Bluetooth is on still |
Matthew Xie | 7f9ecca8 | 2011-07-15 13:03:58 -0700 | [diff] [blame] | 2385 | if (getBluetoothStateInternal() == BluetoothAdapter.STATE_OFF) return; |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 2386 | |
| 2387 | if (updateCountersAndCheckForConnectionStateChange(state, prevState)) { |
| 2388 | if (!validateProfileConnectionState(state) || |
| 2389 | !validateProfileConnectionState(prevState)) { |
| 2390 | // Previously, an invalid state was broadcast anyway, |
| 2391 | // with the invalid state converted to -1 in the intent. |
| 2392 | // Better to log an error and not send an intent with |
| 2393 | // invalid contents or set mAdapterConnectionState to -1. |
| 2394 | Log.e(TAG, "Error in sendConnectionStateChange: " |
| 2395 | + "prevState " + prevState + " state " + state); |
| 2396 | return; |
| 2397 | } |
Jaikumar Ganesh | c53cab2 | 2010-10-26 16:02:26 -0700 | [diff] [blame] | 2398 | |
| 2399 | mAdapterConnectionState = state; |
| 2400 | |
Matthew Xie | 7f9ecca8 | 2011-07-15 13:03:58 -0700 | [diff] [blame] | 2401 | if (state == BluetoothProfile.STATE_DISCONNECTED) { |
| 2402 | mBluetoothState.sendMessage(BluetoothAdapterStateMachine.ALL_DEVICES_DISCONNECTED); |
| 2403 | } |
| 2404 | |
Jaikumar Ganesh | a46f2fb | 2010-10-21 14:55:05 -0700 | [diff] [blame] | 2405 | Intent intent = new Intent(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED); |
| 2406 | intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device); |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 2407 | intent.putExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, |
| 2408 | convertToAdapterState(state)); |
| 2409 | intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_CONNECTION_STATE, |
| 2410 | convertToAdapterState(prevState)); |
Jaikumar Ganesh | 60b4d2a | 2011-07-12 14:14:49 -0700 | [diff] [blame] | 2411 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Jaikumar Ganesh | a46f2fb | 2010-10-21 14:55:05 -0700 | [diff] [blame] | 2412 | mContext.sendBroadcast(intent, BLUETOOTH_PERM); |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 2413 | Log.d(TAG, "CONNECTION_STATE_CHANGE: " + device + ": " |
| 2414 | + prevState + " -> " + state); |
Jaikumar Ganesh | a46f2fb | 2010-10-21 14:55:05 -0700 | [diff] [blame] | 2415 | } |
| 2416 | } |
| 2417 | |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 2418 | private boolean validateProfileConnectionState(int state) { |
| 2419 | return (state == BluetoothProfile.STATE_DISCONNECTED || |
| 2420 | state == BluetoothProfile.STATE_CONNECTING || |
| 2421 | state == BluetoothProfile.STATE_CONNECTED || |
| 2422 | state == BluetoothProfile.STATE_DISCONNECTING); |
| 2423 | } |
| 2424 | |
| 2425 | private int convertToAdapterState(int state) { |
Jaikumar Ganesh | a46f2fb | 2010-10-21 14:55:05 -0700 | [diff] [blame] | 2426 | switch (state) { |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 2427 | case BluetoothProfile.STATE_DISCONNECTED: |
| 2428 | return BluetoothAdapter.STATE_DISCONNECTED; |
| 2429 | case BluetoothProfile.STATE_DISCONNECTING: |
| 2430 | return BluetoothAdapter.STATE_DISCONNECTING; |
| 2431 | case BluetoothProfile.STATE_CONNECTED: |
| 2432 | return BluetoothAdapter.STATE_CONNECTED; |
Jaikumar Ganesh | a46f2fb | 2010-10-21 14:55:05 -0700 | [diff] [blame] | 2433 | case BluetoothProfile.STATE_CONNECTING: |
| 2434 | return BluetoothAdapter.STATE_CONNECTING; |
Jaikumar Ganesh | a46f2fb | 2010-10-21 14:55:05 -0700 | [diff] [blame] | 2435 | } |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 2436 | Log.e(TAG, "Error in convertToAdapterState"); |
| 2437 | return -1; |
Jaikumar Ganesh | a46f2fb | 2010-10-21 14:55:05 -0700 | [diff] [blame] | 2438 | } |
| 2439 | |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 2440 | private boolean updateCountersAndCheckForConnectionStateChange(int state, int prevState) { |
| 2441 | switch (prevState) { |
| 2442 | case BluetoothProfile.STATE_CONNECTING: |
| 2443 | mProfilesConnecting--; |
| 2444 | break; |
| 2445 | |
| 2446 | case BluetoothProfile.STATE_CONNECTED: |
| 2447 | mProfilesConnected--; |
| 2448 | break; |
| 2449 | |
| 2450 | case BluetoothProfile.STATE_DISCONNECTING: |
| 2451 | mProfilesDisconnecting--; |
| 2452 | break; |
| 2453 | } |
| 2454 | |
Jaikumar Ganesh | a46f2fb | 2010-10-21 14:55:05 -0700 | [diff] [blame] | 2455 | switch (state) { |
| 2456 | case BluetoothProfile.STATE_CONNECTING: |
| 2457 | mProfilesConnecting++; |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 2458 | return (mProfilesConnected == 0 && mProfilesConnecting == 1); |
Jaikumar Ganesh | a46f2fb | 2010-10-21 14:55:05 -0700 | [diff] [blame] | 2459 | |
Jaikumar Ganesh | a46f2fb | 2010-10-21 14:55:05 -0700 | [diff] [blame] | 2460 | case BluetoothProfile.STATE_CONNECTED: |
Jaikumar Ganesh | a46f2fb | 2010-10-21 14:55:05 -0700 | [diff] [blame] | 2461 | mProfilesConnected++; |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 2462 | return (mProfilesConnected == 1); |
Jaikumar Ganesh | a46f2fb | 2010-10-21 14:55:05 -0700 | [diff] [blame] | 2463 | |
Jaikumar Ganesh | a46f2fb | 2010-10-21 14:55:05 -0700 | [diff] [blame] | 2464 | case BluetoothProfile.STATE_DISCONNECTING: |
| 2465 | mProfilesDisconnecting++; |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 2466 | return (mProfilesConnected == 0 && mProfilesDisconnecting == 1); |
Jaikumar Ganesh | a46f2fb | 2010-10-21 14:55:05 -0700 | [diff] [blame] | 2467 | |
Jaikumar Ganesh | a46f2fb | 2010-10-21 14:55:05 -0700 | [diff] [blame] | 2468 | case BluetoothProfile.STATE_DISCONNECTED: |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 2469 | return (mProfilesConnected == 0 && mProfilesConnecting == 0); |
Jaikumar Ganesh | a46f2fb | 2010-10-21 14:55:05 -0700 | [diff] [blame] | 2470 | |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 2471 | default: |
| 2472 | return true; |
Jaikumar Ganesh | a46f2fb | 2010-10-21 14:55:05 -0700 | [diff] [blame] | 2473 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2474 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 2475 | |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 2476 | private void createIncomingConnectionStateFile() { |
| 2477 | File f = new File(INCOMING_CONNECTION_FILE); |
| 2478 | if (!f.exists()) { |
| 2479 | try { |
| 2480 | f.createNewFile(); |
| 2481 | } catch (IOException e) { |
| 2482 | Log.e(TAG, "IOException: cannot create file"); |
| 2483 | } |
| 2484 | } |
| 2485 | } |
| 2486 | |
| 2487 | /** @hide */ |
| 2488 | public Pair<Integer, String> getIncomingState(String address) { |
| 2489 | if (mIncomingConnections.isEmpty()) { |
| 2490 | createIncomingConnectionStateFile(); |
| 2491 | readIncomingConnectionState(); |
| 2492 | } |
| 2493 | return mIncomingConnections.get(address); |
| 2494 | } |
| 2495 | |
| 2496 | private void readIncomingConnectionState() { |
| 2497 | synchronized(mIncomingConnections) { |
| 2498 | FileInputStream fstream = null; |
| 2499 | try { |
| 2500 | fstream = new FileInputStream(INCOMING_CONNECTION_FILE); |
| 2501 | DataInputStream in = new DataInputStream(fstream); |
| 2502 | BufferedReader file = new BufferedReader(new InputStreamReader(in)); |
| 2503 | String line; |
| 2504 | while((line = file.readLine()) != null) { |
| 2505 | line = line.trim(); |
| 2506 | if (line.length() == 0) continue; |
| 2507 | String[] value = line.split(","); |
| 2508 | if (value != null && value.length == 3) { |
| 2509 | Integer val1 = Integer.parseInt(value[1]); |
| 2510 | Pair<Integer, String> val = new Pair(val1, value[2]); |
| 2511 | mIncomingConnections.put(value[0], val); |
| 2512 | } |
| 2513 | } |
| 2514 | } catch (FileNotFoundException e) { |
| 2515 | log("FileNotFoundException: readIncomingConnectionState" + e.toString()); |
| 2516 | } catch (IOException e) { |
| 2517 | log("IOException: readIncomingConnectionState" + e.toString()); |
| 2518 | } finally { |
| 2519 | if (fstream != null) { |
| 2520 | try { |
| 2521 | fstream.close(); |
| 2522 | } catch (IOException e) { |
| 2523 | // Ignore |
| 2524 | } |
| 2525 | } |
| 2526 | } |
| 2527 | } |
| 2528 | } |
| 2529 | |
| 2530 | private void truncateIncomingConnectionFile() { |
| 2531 | RandomAccessFile r = null; |
| 2532 | try { |
| 2533 | r = new RandomAccessFile(INCOMING_CONNECTION_FILE, "rw"); |
| 2534 | r.setLength(0); |
| 2535 | } catch (FileNotFoundException e) { |
| 2536 | log("FileNotFoundException: truncateIncomingConnectionState" + e.toString()); |
| 2537 | } catch (IOException e) { |
| 2538 | log("IOException: truncateIncomingConnectionState" + e.toString()); |
| 2539 | } finally { |
| 2540 | if (r != null) { |
| 2541 | try { |
| 2542 | r.close(); |
| 2543 | } catch (IOException e) { |
| 2544 | // ignore |
| 2545 | } |
| 2546 | } |
| 2547 | } |
| 2548 | } |
| 2549 | |
| 2550 | /** @hide */ |
| 2551 | public void writeIncomingConnectionState(String address, Pair<Integer, String> data) { |
| 2552 | synchronized(mIncomingConnections) { |
| 2553 | mIncomingConnections.put(address, data); |
| 2554 | |
| 2555 | truncateIncomingConnectionFile(); |
| 2556 | BufferedWriter out = null; |
| 2557 | StringBuilder value = new StringBuilder(); |
| 2558 | try { |
| 2559 | out = new BufferedWriter(new FileWriter(INCOMING_CONNECTION_FILE, true)); |
| 2560 | for (String devAddress: mIncomingConnections.keySet()) { |
| 2561 | Pair<Integer, String> val = mIncomingConnections.get(devAddress); |
| 2562 | value.append(devAddress); |
| 2563 | value.append(","); |
| 2564 | value.append(val.first.toString()); |
| 2565 | value.append(","); |
| 2566 | value.append(val.second); |
| 2567 | value.append("\n"); |
| 2568 | } |
| 2569 | out.write(value.toString()); |
| 2570 | } catch (FileNotFoundException e) { |
| 2571 | log("FileNotFoundException: writeIncomingConnectionState" + e.toString()); |
| 2572 | } catch (IOException e) { |
| 2573 | log("IOException: writeIncomingConnectionState" + e.toString()); |
| 2574 | } finally { |
| 2575 | if (out != null) { |
| 2576 | try { |
| 2577 | out.close(); |
| 2578 | } catch (IOException e) { |
| 2579 | // Ignore |
| 2580 | } |
| 2581 | } |
| 2582 | } |
| 2583 | } |
| 2584 | } |
| 2585 | |
| 2586 | private static void log(String msg) { |
| 2587 | Log.d(TAG, msg); |
| 2588 | } |
| 2589 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 2590 | private native static void classInitNative(); |
| 2591 | private native void initializeNativeDataNative(); |
| 2592 | private native boolean setupNativeDataNative(); |
| 2593 | private native boolean tearDownNativeDataNative(); |
| 2594 | private native void cleanupNativeDataNative(); |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 2595 | /*package*/ native String getAdapterPathNative(); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 2596 | |
| 2597 | private native int isEnabledNative(); |
Matthew Xie | 7f9ecca8 | 2011-07-15 13:03:58 -0700 | [diff] [blame] | 2598 | /*package*/ native int enableNative(); |
| 2599 | /*package*/ native int disableNative(); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 2600 | |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 2601 | /*package*/ native Object[] getAdapterPropertiesNative(); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 2602 | private native Object[] getDevicePropertiesNative(String objectPath); |
| 2603 | private native boolean setAdapterPropertyStringNative(String key, String value); |
| 2604 | private native boolean setAdapterPropertyIntegerNative(String key, int value); |
| 2605 | private native boolean setAdapterPropertyBooleanNative(String key, int value); |
| 2606 | |
| 2607 | private native boolean startDiscoveryNative(); |
| 2608 | private native boolean stopDiscoveryNative(); |
| 2609 | |
| 2610 | private native boolean createPairedDeviceNative(String address, int timeout_ms); |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 2611 | private native boolean createPairedDeviceOutOfBandNative(String address, int timeout_ms); |
| 2612 | private native byte[] readAdapterOutOfBandDataNative(); |
| 2613 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 2614 | private native boolean cancelDeviceCreationNative(String address); |
| 2615 | private native boolean removeDeviceNative(String objectPath); |
| 2616 | private native int getDeviceServiceChannelNative(String objectPath, String uuid, |
| 2617 | int attributeId); |
| 2618 | |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 2619 | private native boolean cancelPairingUserInputNative(String address, int nativeData); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 2620 | private native boolean setPinNative(String address, String pin, int nativeData); |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 2621 | private native boolean setPasskeyNative(String address, int passkey, int nativeData); |
| 2622 | private native boolean setPairingConfirmationNative(String address, boolean confirm, |
| 2623 | int nativeData); |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 2624 | private native boolean setRemoteOutOfBandDataNative(String address, byte[] hash, |
| 2625 | byte[] randomizer, int nativeData); |
| 2626 | |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 2627 | private native boolean setDevicePropertyBooleanNative(String objectPath, String key, |
| 2628 | int value); |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 2629 | private native boolean createDeviceNative(String address); |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 2630 | /*package*/ native boolean discoverServicesNative(String objectPath, String pattern); |
Jaikumar Ganesh | 10eac97 | 2009-09-21 12:48:51 -0700 | [diff] [blame] | 2631 | |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 2632 | private native int addRfcommServiceRecordNative(String name, long uuidMsb, long uuidLsb, |
| 2633 | short channel); |
| 2634 | private native boolean removeServiceRecordNative(int handle); |
Jaikumar Ganesh | b7e029d | 2010-03-09 15:31:24 -0800 | [diff] [blame] | 2635 | private native boolean setLinkTimeoutNative(String path, int num_slots); |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 2636 | |
Jaikumar Ganesh | 30b8cbe | 2011-02-11 16:26:29 -0800 | [diff] [blame] | 2637 | native boolean connectInputDeviceNative(String path); |
| 2638 | native boolean disconnectInputDeviceNative(String path); |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 2639 | |
Jaikumar Ganesh | a8571f1 | 2011-02-11 15:46:54 -0800 | [diff] [blame] | 2640 | native boolean setBluetoothTetheringNative(boolean value, String nap, String bridge); |
| 2641 | native boolean connectPanDeviceNative(String path, String dstRole); |
| 2642 | native boolean disconnectPanDeviceNative(String path); |
| 2643 | native boolean disconnectPanServerDeviceNative(String path, |
Jaikumar Ganesh | a44a1e7 | 2011-02-11 12:40:44 -0800 | [diff] [blame] | 2644 | String address, String iface); |
Jaikumar Ganesh | 84690c8 | 2010-12-10 12:48:58 -0800 | [diff] [blame] | 2645 | |
| 2646 | private native int[] addReservedServiceRecordsNative(int[] uuuids); |
| 2647 | private native boolean removeReservedServiceRecordsNative(int[] handles); |
Jaikumar Ganesh | 2ea1e85 | 2011-04-01 16:33:09 -0700 | [diff] [blame] | 2648 | |
| 2649 | // Health API |
| 2650 | native String registerHealthApplicationNative(int dataType, String role, String name, |
| 2651 | String channelType); |
| 2652 | native String registerHealthApplicationNative(int dataType, String role, String name); |
| 2653 | native boolean unregisterHealthApplicationNative(String path); |
| 2654 | native boolean createChannelNative(String devicePath, String appPath, String channelType); |
| 2655 | native boolean destroyChannelNative(String devicePath, String channelpath); |
| 2656 | native String getMainChannelNative(String path); |
| 2657 | native String getChannelApplicationNative(String channelPath); |
| 2658 | native ParcelFileDescriptor getChannelFdNative(String channelPath); |
| 2659 | native boolean releaseChannelFdNative(String channelPath); |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 2660 | native boolean setAuthorizationNative(String address, boolean value, int data); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2661 | } |