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