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