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 | ea600cc | 2009-03-31 14:56:26 -0700 | [diff] [blame] | 27 | import android.bluetooth.BluetoothClass; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 28 | import android.bluetooth.BluetoothAdapter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.bluetooth.BluetoothDevice; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import android.bluetooth.BluetoothHeadset; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 31 | import android.bluetooth.IBluetooth; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.content.BroadcastReceiver; |
| 33 | import android.content.ContentResolver; |
| 34 | import android.content.Context; |
| 35 | import android.content.Intent; |
| 36 | import android.content.IntentFilter; |
| 37 | import android.os.Binder; |
| 38 | import android.os.Handler; |
| 39 | import android.os.Message; |
| 40 | import android.os.RemoteException; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 41 | import android.os.ServiceManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | import android.os.SystemService; |
| 43 | import android.provider.Settings; |
| 44 | import android.util.Log; |
| 45 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 46 | import com.android.internal.app.IBatteryStats; |
| 47 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | import java.io.FileDescriptor; |
| 49 | import java.io.PrintWriter; |
| 50 | import java.io.UnsupportedEncodingException; |
| 51 | import java.util.ArrayList; |
| 52 | import java.util.Arrays; |
| 53 | import java.util.HashMap; |
Nick Pelly | f9204e5 | 2009-03-24 21:15:00 -0700 | [diff] [blame] | 54 | import java.util.Iterator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | import java.util.Map; |
| 56 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 57 | public class BluetoothService extends IBluetooth.Stub { |
| 58 | private static final String TAG = "BluetoothService"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | private static final boolean DBG = true; |
| 60 | |
| 61 | private int mNativeData; |
| 62 | private BluetoothEventLoop mEventLoop; |
| 63 | private IntentFilter mIntentFilter; |
| 64 | private boolean mIsAirplaneSensitive; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 65 | private int mBluetoothState; |
Nick Pelly | 997c761 | 2009-03-24 20:44:48 -0700 | [diff] [blame] | 66 | 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] | 67 | private boolean mIsDiscovering; |
| 68 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 69 | private BluetoothAdapter mAdapter; // constant after init() |
| 70 | private final BondState mBondState = new BondState(); // local cache of bondings |
| 71 | private final IBatteryStats mBatteryStats; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | private final Context mContext; |
| 73 | |
| 74 | private static final String BLUETOOTH_ADMIN_PERM = android.Manifest.permission.BLUETOOTH_ADMIN; |
| 75 | private static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH; |
| 76 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 77 | private static final int MESSAGE_REGISTER_SDP_RECORDS = 1; |
| 78 | private static final int MESSAGE_FINISH_DISABLE = 2; |
| 79 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 80 | private final Map<String, String> mAdapterProperties; |
| 81 | private final HashMap <String, Map<String, String>> mDeviceProperties; |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 82 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | static { |
| 84 | classInitNative(); |
| 85 | } |
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 BluetoothService(Context context) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | mContext = context; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 89 | |
| 90 | // Need to do this in place of: |
| 91 | // mBatteryStats = BatteryStatsService.getService(); |
| 92 | // Since we can not import BatteryStatsService from here. This class really needs to be |
| 93 | // moved to java/services/com/android/server/ |
| 94 | mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService("batteryinfo")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | initializeNativeDataNative(); |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 97 | |
| 98 | if (isEnabledNative() == 1) { |
| 99 | Log.w(TAG, "Bluetooth daemons already running - runtime restart? "); |
| 100 | disableNative(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | } |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 102 | |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 103 | mBluetoothState = BluetoothAdapter.STATE_OFF; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | mIsDiscovering = false; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 105 | mAdapterProperties = new HashMap<String, String>(); |
| 106 | mDeviceProperties = new HashMap<String, Map<String,String>>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | registerForAirplaneMode(); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | public synchronized void initAfterRegistration() { |
| 111 | mAdapter = (BluetoothAdapter) mContext.getSystemService(Context.BLUETOOTH_SERVICE); |
| 112 | mEventLoop = new BluetoothEventLoop(mContext, mAdapter, this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | |
| 115 | @Override |
| 116 | protected void finalize() throws Throwable { |
| 117 | if (mIsAirplaneSensitive) { |
| 118 | mContext.unregisterReceiver(mReceiver); |
| 119 | } |
| 120 | try { |
| 121 | cleanupNativeDataNative(); |
| 122 | } finally { |
| 123 | super.finalize(); |
| 124 | } |
| 125 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | |
| 127 | public boolean isEnabled() { |
| 128 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 129 | return mBluetoothState == BluetoothAdapter.STATE_ON; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 132 | public int getBluetoothState() { |
| 133 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
| 134 | return mBluetoothState; |
| 135 | } |
| 136 | |
| 137 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | /** |
| 139 | * Bring down bluetooth and disable BT in settings. Returns true on success. |
| 140 | */ |
| 141 | public boolean disable() { |
| 142 | return disable(true); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Bring down bluetooth. Returns true on success. |
| 147 | * |
| 148 | * @param saveSetting If true, disable BT in settings |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | */ |
| 150 | public synchronized boolean disable(boolean saveSetting) { |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 151 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 153 | switch (mBluetoothState) { |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 154 | case BluetoothAdapter.STATE_OFF: |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 155 | return true; |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 156 | case BluetoothAdapter.STATE_ON: |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 157 | break; |
| 158 | default: |
| 159 | return false; |
| 160 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | if (mEnableThread != null && mEnableThread.isAlive()) { |
| 162 | return false; |
| 163 | } |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 164 | setBluetoothState(BluetoothAdapter.STATE_TURNING_OFF); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 165 | |
| 166 | // Allow 3 seconds for profiles to gracefully disconnect |
| 167 | // TODO: Introduce a callback mechanism so that each profile can notify |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 168 | // BluetoothService when it is done shutting down |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 169 | mHandler.sendMessageDelayed( |
| 170 | mHandler.obtainMessage(MESSAGE_FINISH_DISABLE, saveSetting ? 1 : 0, 0), 3000); |
| 171 | return true; |
| 172 | } |
| 173 | |
| 174 | |
| 175 | private synchronized void finishDisable(boolean saveSetting) { |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 176 | if (mBluetoothState != BluetoothAdapter.STATE_TURNING_OFF) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 177 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | } |
| 179 | mEventLoop.stop(); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 180 | tearDownNativeDataNative(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | disableNative(); |
| 182 | |
| 183 | // mark in progress bondings as cancelled |
| 184 | for (String address : mBondState.listInState(BluetoothDevice.BOND_BONDING)) { |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 185 | mBondState.setBondState(address, BluetoothDevice.BOND_NONE, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | BluetoothDevice.UNBOND_REASON_AUTH_CANCELED); |
| 187 | } |
| 188 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | // update mode |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 190 | Intent intent = new Intent(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED); |
| 191 | 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] | 192 | mContext.sendBroadcast(intent, BLUETOOTH_PERM); |
| 193 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 194 | mIsDiscovering = false; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 195 | mAdapterProperties.clear(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 196 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | if (saveSetting) { |
| 198 | persistBluetoothOnSetting(false); |
| 199 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 200 | |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 201 | setBluetoothState(BluetoothAdapter.STATE_OFF); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 202 | |
| 203 | // Log bluetooth off to battery stats. |
| 204 | long ident = Binder.clearCallingIdentity(); |
| 205 | try { |
| 206 | mBatteryStats.noteBluetoothOff(); |
| 207 | } catch (RemoteException e) { |
| 208 | } finally { |
| 209 | Binder.restoreCallingIdentity(ident); |
| 210 | } |
Nick Pelly | 997c761 | 2009-03-24 20:44:48 -0700 | [diff] [blame] | 211 | |
| 212 | if (mRestart) { |
| 213 | mRestart = false; |
| 214 | enable(); |
| 215 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | } |
| 217 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 218 | /** Bring up BT and persist BT on in settings */ |
| 219 | public boolean enable() { |
| 220 | return enable(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Enable this Bluetooth device, asynchronously. |
| 225 | * This turns on/off the underlying hardware. |
| 226 | * |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 227 | * @param saveSetting If true, persist the new state of BT in settings |
| 228 | * @return True on success (so far) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | */ |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 230 | public synchronized boolean enable(boolean saveSetting) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 232 | "Need BLUETOOTH_ADMIN permission"); |
| 233 | |
| 234 | // Airplane mode can prevent Bluetooth radio from being turned on. |
| 235 | if (mIsAirplaneSensitive && isAirplaneModeOn()) { |
| 236 | return false; |
| 237 | } |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 238 | if (mBluetoothState != BluetoothAdapter.STATE_OFF) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | return false; |
| 240 | } |
| 241 | if (mEnableThread != null && mEnableThread.isAlive()) { |
| 242 | return false; |
| 243 | } |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 244 | setBluetoothState(BluetoothAdapter.STATE_TURNING_ON); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 245 | mEnableThread = new EnableThread(saveSetting); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | mEnableThread.start(); |
| 247 | return true; |
| 248 | } |
| 249 | |
Nick Pelly | 997c761 | 2009-03-24 20:44:48 -0700 | [diff] [blame] | 250 | /** Forcibly restart Bluetooth if it is on */ |
| 251 | /* package */ synchronized void restart() { |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 252 | if (mBluetoothState != BluetoothAdapter.STATE_ON) { |
Nick Pelly | 997c761 | 2009-03-24 20:44:48 -0700 | [diff] [blame] | 253 | return; |
| 254 | } |
| 255 | mRestart = true; |
| 256 | if (!disable(false)) { |
| 257 | mRestart = false; |
| 258 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 259 | } |
Nick Pelly | 997c761 | 2009-03-24 20:44:48 -0700 | [diff] [blame] | 260 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 261 | private synchronized void setBluetoothState(int state) { |
| 262 | if (state == mBluetoothState) { |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | if (DBG) log("Bluetooth state " + mBluetoothState + " -> " + state); |
| 267 | |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 268 | Intent intent = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED); |
| 269 | intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, mBluetoothState); |
| 270 | intent.putExtra(BluetoothAdapter.EXTRA_STATE, state); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 271 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 272 | |
| 273 | mBluetoothState = state; |
| 274 | |
| 275 | mContext.sendBroadcast(intent, BLUETOOTH_PERM); |
| 276 | } |
| 277 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 278 | private final Handler mHandler = new Handler() { |
| 279 | @Override |
| 280 | public void handleMessage(Message msg) { |
| 281 | switch (msg.what) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 282 | case MESSAGE_REGISTER_SDP_RECORDS: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | //TODO: Don't assume HSP/HFP is running, don't use sdptool, |
| 284 | if (isEnabled()) { |
| 285 | SystemService.start("hsag"); |
| 286 | SystemService.start("hfag"); |
Nick Pelly | 03c707a | 2009-07-14 21:32:14 -0700 | [diff] [blame] | 287 | SystemService.start("opush"); |
Jaikumar Ganesh | 6754296 | 2009-07-23 21:32:45 -0700 | [diff] [blame] | 288 | SystemService.start("pbap"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 290 | break; |
| 291 | case MESSAGE_FINISH_DISABLE: |
| 292 | finishDisable(msg.arg1 != 0); |
| 293 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | }; |
| 297 | |
| 298 | private EnableThread mEnableThread; |
| 299 | |
| 300 | private class EnableThread extends Thread { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | private final boolean mSaveSetting; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 302 | public EnableThread(boolean saveSetting) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 | mSaveSetting = saveSetting; |
| 304 | } |
| 305 | public void run() { |
| 306 | boolean res = (enableNative() == 0); |
| 307 | if (res) { |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 308 | int retryCount = 2; |
| 309 | boolean running = false; |
| 310 | while ((retryCount-- > 0) && !running) { |
| 311 | mEventLoop.start(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 312 | // 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] | 313 | // thing. Check periodically for a while. |
| 314 | int pollCount = 5; |
| 315 | while ((pollCount-- > 0) && !running) { |
| 316 | if (mEventLoop.isEventLoopRunning()) { |
| 317 | running = true; |
| 318 | break; |
| 319 | } |
| 320 | try { |
| 321 | Thread.sleep(100); |
| 322 | } catch (InterruptedException e) {} |
| 323 | } |
| 324 | } |
| 325 | if (!running) { |
| 326 | log("bt EnableThread giving up"); |
| 327 | res = false; |
| 328 | disableNative(); |
| 329 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | } |
| 331 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | |
| 333 | if (res) { |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 334 | if (!setupNativeDataNative()) { |
| 335 | return; |
| 336 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | if (mSaveSetting) { |
| 338 | persistBluetoothOnSetting(true); |
| 339 | } |
| 340 | mIsDiscovering = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 341 | mBondState.loadBondState(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 342 | mHandler.sendMessageDelayed(mHandler.obtainMessage(MESSAGE_REGISTER_SDP_RECORDS), |
| 343 | 3000); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 345 | // Log bluetooth on to battery stats. |
| 346 | long ident = Binder.clearCallingIdentity(); |
| 347 | try { |
| 348 | mBatteryStats.noteBluetoothOn(); |
| 349 | } catch (RemoteException e) { |
| 350 | } finally { |
| 351 | Binder.restoreCallingIdentity(ident); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | mEnableThread = null; |
| 356 | |
| 357 | setBluetoothState(res ? |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 358 | BluetoothAdapter.STATE_ON : |
| 359 | BluetoothAdapter.STATE_OFF); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 360 | |
| 361 | if (res) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 362 | // Update mode |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 363 | String[] propVal = {"Pairable", getProperty("Pairable")}; |
| 364 | mEventLoop.onPropertyChanged(propVal); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 365 | } |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 366 | |
Daisuke Miyakawa | 5c43f73 | 2009-06-08 12:55:56 +0900 | [diff] [blame] | 367 | if (mIsAirplaneSensitive && isAirplaneModeOn()) { |
| 368 | disable(false); |
| 369 | } |
| 370 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
| 374 | private void persistBluetoothOnSetting(boolean bluetoothOn) { |
| 375 | long origCallerIdentityToken = Binder.clearCallingIdentity(); |
| 376 | Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.BLUETOOTH_ON, |
| 377 | bluetoothOn ? 1 : 0); |
| 378 | Binder.restoreCallingIdentity(origCallerIdentityToken); |
| 379 | } |
| 380 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 381 | /* package */ BondState getBondState() { |
| 382 | return mBondState; |
| 383 | } |
| 384 | |
| 385 | /** local cache of bonding state. |
| 386 | /* we keep our own state to track the intermediate state BONDING, which |
| 387 | /* bluez does not track. |
| 388 | * All addreses must be passed in upper case. |
| 389 | */ |
| 390 | public class BondState { |
| 391 | private final HashMap<String, Integer> mState = new HashMap<String, Integer>(); |
| 392 | private final HashMap<String, Integer> mPinAttempt = new HashMap<String, Integer>(); |
| 393 | private final ArrayList<String> mAutoPairingFailures = new ArrayList<String>(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 394 | // List of all the vendor_id prefix of Bluetooth addresses for |
| 395 | // which auto pairing is not attempted. |
| 396 | // The following companies are included in the list below: |
| 397 | // ALPS (lexus), Murata (Prius 2007, Nokia 616), TEMIC SDS (Porsche, Audi), |
| 398 | // Parrot, Zhongshan General K-mate Electronics, Great Well |
| 399 | // Electronics, Flaircomm Electronics, Jatty Electronics, Delphi, |
| 400 | // Clarion, Novero, Denso (Lexus, Toyota), Johnson Controls (Acura), |
Nick Pelly | adbaef2 | 2009-09-14 19:04:47 -0700 | [diff] [blame^] | 401 | // Continental Automotive, Harman/Becker, Panasonic/Kyushu Ten |
Jaikumar Ganesh | 482d54b | 2009-09-14 13:43:09 -0700 | [diff] [blame] | 402 | private final ArrayList<String> mAutoPairingAddressBlacklist = |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 403 | new ArrayList<String>(Arrays.asList( |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 404 | "00:02:C7", "00:16:FE", "00:19:C1", "00:1B:FB", "00:1E:3D", "00:21:4F", |
| 405 | "00:23:06", "00:24:33", "00:A0:79", "00:0E:6D", "00:13:E0", "00:21:E8", |
| 406 | "00:60:57", "00:0E:9F", "00:12:1C", "00:18:91", "00:18:96", "00:13:04", |
| 407 | "00:16:FD", "00:22:A0", "00:0B:4C", "00:60:6F", "00:23:3D", "00:C0:59", |
Nick Pelly | adbaef2 | 2009-09-14 19:04:47 -0700 | [diff] [blame^] | 408 | "00:0A:30", "00:1E:AE", "00:1C:D7", "00:80:F0" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 409 | )); |
| 410 | |
Jaikumar Ganesh | 482d54b | 2009-09-14 13:43:09 -0700 | [diff] [blame] | 411 | // List of names of Bluetooth devices for which auto pairing should be |
| 412 | // disabled. |
| 413 | private final ArrayList<String> mAutoPairingNameBlacklist = |
| 414 | new ArrayList<String>(Arrays.asList( |
| 415 | "Motorola IHF1000", "i.TechBlueBAND", "X5 Stereo v1.3")); |
| 416 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 417 | public synchronized void loadBondState() { |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 418 | if (mBluetoothState != BluetoothAdapter.STATE_TURNING_ON) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 419 | return; |
| 420 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 421 | String []bonds = null; |
| 422 | String val = getProperty("Devices"); |
| 423 | if (val != null) { |
| 424 | bonds = val.split(","); |
| 425 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 426 | if (bonds == null) { |
| 427 | return; |
| 428 | } |
| 429 | mState.clear(); |
| 430 | if (DBG) log("found " + bonds.length + " bonded devices"); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 431 | for (String device : bonds) { |
| 432 | mState.put(getAddressFromObjectPath(device).toUpperCase(), |
| 433 | BluetoothDevice.BOND_BONDED); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
| 437 | public synchronized void setBondState(String address, int state) { |
| 438 | setBondState(address, state, 0); |
| 439 | } |
| 440 | |
| 441 | /** reason is ignored unless state == BOND_NOT_BONDED */ |
| 442 | public synchronized void setBondState(String address, int state, int reason) { |
| 443 | int oldState = getBondState(address); |
| 444 | if (oldState == state) { |
| 445 | return; |
| 446 | } |
| 447 | if (DBG) log(address + " bond state " + oldState + " -> " + state + " (" + |
| 448 | reason + ")"); |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 449 | Intent intent = new Intent(BluetoothDevice.ACTION_BOND_STATE_CHANGED); |
| 450 | intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mAdapter.getRemoteDevice(address)); |
| 451 | intent.putExtra(BluetoothDevice.EXTRA_BOND_STATE, state); |
| 452 | intent.putExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, oldState); |
| 453 | if (state == BluetoothDevice.BOND_NONE) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 454 | if (reason <= 0) { |
| 455 | Log.w(TAG, "setBondState() called to unbond device, but reason code is " + |
| 456 | "invalid. Overriding reason code with BOND_RESULT_REMOVED"); |
| 457 | reason = BluetoothDevice.UNBOND_REASON_REMOVED; |
| 458 | } |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 459 | intent.putExtra(BluetoothDevice.EXTRA_REASON, reason); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 460 | mState.remove(address); |
| 461 | } else { |
| 462 | mState.put(address, state); |
| 463 | } |
| 464 | |
| 465 | mContext.sendBroadcast(intent, BLUETOOTH_PERM); |
| 466 | } |
| 467 | |
| 468 | public boolean isAutoPairingBlacklisted(String address) { |
Jaikumar Ganesh | 482d54b | 2009-09-14 13:43:09 -0700 | [diff] [blame] | 469 | for (String blacklistAddress : mAutoPairingAddressBlacklist) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 470 | if (address.startsWith(blacklistAddress)) return true; |
| 471 | } |
Jaikumar Ganesh | 482d54b | 2009-09-14 13:43:09 -0700 | [diff] [blame] | 472 | |
| 473 | String name = getRemoteName(address); |
| 474 | if (name != null) { |
| 475 | for (String blacklistName : mAutoPairingNameBlacklist) { |
| 476 | if (name.equals(blacklistName)) return true; |
| 477 | } |
| 478 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 479 | return false; |
| 480 | } |
| 481 | |
| 482 | public synchronized int getBondState(String address) { |
| 483 | Integer state = mState.get(address); |
| 484 | if (state == null) { |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 485 | return BluetoothDevice.BOND_NONE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 486 | } |
| 487 | return state.intValue(); |
| 488 | } |
| 489 | |
| 490 | private synchronized String[] listInState(int state) { |
| 491 | ArrayList<String> result = new ArrayList<String>(mState.size()); |
| 492 | for (Map.Entry<String, Integer> e : mState.entrySet()) { |
| 493 | if (e.getValue().intValue() == state) { |
| 494 | result.add(e.getKey()); |
| 495 | } |
| 496 | } |
| 497 | return result.toArray(new String[result.size()]); |
| 498 | } |
| 499 | |
| 500 | public synchronized void addAutoPairingFailure(String address) { |
| 501 | if (!mAutoPairingFailures.contains(address)) { |
| 502 | mAutoPairingFailures.add(address); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | public synchronized boolean isAutoPairingAttemptsInProgress(String address) { |
| 507 | return getAttempt(address) != 0; |
| 508 | } |
| 509 | |
| 510 | public synchronized void clearPinAttempts(String address) { |
| 511 | mPinAttempt.remove(address); |
| 512 | } |
| 513 | |
| 514 | public synchronized boolean hasAutoPairingFailed(String address) { |
| 515 | return mAutoPairingFailures.contains(address); |
| 516 | } |
| 517 | |
| 518 | public synchronized int getAttempt(String address) { |
| 519 | Integer attempt = mPinAttempt.get(address); |
| 520 | if (attempt == null) { |
| 521 | return 0; |
| 522 | } |
| 523 | return attempt.intValue(); |
| 524 | } |
| 525 | |
| 526 | public synchronized void attempt(String address) { |
| 527 | Integer attempt = mPinAttempt.get(address); |
| 528 | int newAttempt; |
| 529 | if (attempt == null) { |
| 530 | newAttempt = 1; |
| 531 | } else { |
| 532 | newAttempt = attempt.intValue() + 1; |
| 533 | } |
| 534 | mPinAttempt.put(address, new Integer(newAttempt)); |
| 535 | } |
| 536 | |
| 537 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 538 | |
| 539 | private static String toBondStateString(int bondState) { |
| 540 | switch (bondState) { |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 541 | case BluetoothDevice.BOND_NONE: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 542 | return "not bonded"; |
| 543 | case BluetoothDevice.BOND_BONDING: |
| 544 | return "bonding"; |
| 545 | case BluetoothDevice.BOND_BONDED: |
| 546 | return "bonded"; |
| 547 | default: |
| 548 | return "??????"; |
| 549 | } |
| 550 | } |
| 551 | |
Jaikumar Ganesh | 9519ce7 | 2009-09-08 21:37:32 -0700 | [diff] [blame] | 552 | /*package*/ synchronized boolean isAdapterPropertiesEmpty() { |
| 553 | return mAdapterProperties.isEmpty(); |
| 554 | } |
| 555 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 556 | /*package*/synchronized void getAllProperties() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 557 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 558 | mAdapterProperties.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 559 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 560 | String properties[] = (String [])getAdapterPropertiesNative(); |
| 561 | // The String Array consists of key-value pairs. |
| 562 | if (properties == null) { |
| 563 | Log.e(TAG, "*Error*: GetAdapterProperties returned NULL"); |
| 564 | return; |
| 565 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 566 | |
Jaikumar Ganesh | 8bc8ce4 | 2009-06-24 16:42:51 -0700 | [diff] [blame] | 567 | for (int i = 0; i < properties.length; i++) { |
| 568 | String name = properties[i]; |
Jaikumar Ganesh | efa3367 | 2009-08-28 13:48:55 -0700 | [diff] [blame] | 569 | String newValue = null; |
Jaikumar Ganesh | 8bc8ce4 | 2009-06-24 16:42:51 -0700 | [diff] [blame] | 570 | int len; |
| 571 | if (name == null) { |
| 572 | Log.e(TAG, "Error:Adapter Property at index" + i + "is null"); |
| 573 | continue; |
| 574 | } |
| 575 | if (name.equals("Devices")) { |
Jaikumar Ganesh | efa3367 | 2009-08-28 13:48:55 -0700 | [diff] [blame] | 576 | StringBuilder str = new StringBuilder(); |
Jaikumar Ganesh | 8bc8ce4 | 2009-06-24 16:42:51 -0700 | [diff] [blame] | 577 | len = Integer.valueOf(properties[++i]); |
Jaikumar Ganesh | 8bc8ce4 | 2009-06-24 16:42:51 -0700 | [diff] [blame] | 578 | for (int j = 0; j < len; j++) { |
Jaikumar Ganesh | efa3367 | 2009-08-28 13:48:55 -0700 | [diff] [blame] | 579 | str.append(properties[++i]); |
| 580 | str.append(","); |
| 581 | } |
| 582 | if (len > 0) { |
| 583 | newValue = str.toString(); |
Jaikumar Ganesh | 8bc8ce4 | 2009-06-24 16:42:51 -0700 | [diff] [blame] | 584 | } |
| 585 | } else { |
| 586 | newValue = properties[++i]; |
| 587 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 588 | mAdapterProperties.put(name, newValue); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | // Add adapter object path property. |
| 592 | String adapterPath = getAdapterPathNative(); |
| 593 | if (adapterPath != null) |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 594 | mAdapterProperties.put("ObjectPath", adapterPath + "/dev_"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 595 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 596 | |
| 597 | /* package */ synchronized void setProperty(String name, String value) { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 598 | mAdapterProperties.put(name, value); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 599 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 600 | |
| 601 | public synchronized boolean setName(String name) { |
| 602 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 603 | "Need BLUETOOTH_ADMIN permission"); |
| 604 | if (name == null) { |
| 605 | return false; |
| 606 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 607 | return setPropertyString("Name", name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 608 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 609 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 610 | //TODO(): setPropertyString, setPropertyInteger, setPropertyBoolean |
| 611 | // Either have a single property function with Object as the parameter |
| 612 | // or have a function for each property and then obfuscate in the JNI layer. |
| 613 | // The following looks dirty. |
| 614 | private boolean setPropertyString(String key, String value) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 615 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 616 | return setAdapterPropertyStringNative(key, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 617 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 618 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 619 | private boolean setPropertyInteger(String key, int value) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 620 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 621 | return setAdapterPropertyIntegerNative(key, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 622 | } |
| 623 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 624 | private boolean setPropertyBoolean(String key, boolean value) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 625 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 626 | return setAdapterPropertyBooleanNative(key, value ? 1 : 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 627 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 628 | |
| 629 | /** |
| 630 | * Set the discoverability window for the device. A timeout of zero |
| 631 | * makes the device permanently discoverable (if the device is |
| 632 | * discoverable). Setting the timeout to a nonzero value does not make |
| 633 | * a device discoverable; you need to call setMode() to make the device |
| 634 | * explicitly discoverable. |
| 635 | * |
| 636 | * @param timeout_s The discoverable timeout in seconds. |
| 637 | */ |
| 638 | public synchronized boolean setDiscoverableTimeout(int timeout) { |
| 639 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 640 | "Need BLUETOOTH_ADMIN permission"); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 641 | return setPropertyInteger("DiscoverableTimeout", timeout); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 642 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 643 | |
| 644 | public synchronized boolean setScanMode(int mode) { |
| 645 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 646 | "Need BLUETOOTH_ADMIN permission"); |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 647 | boolean pairable = false; |
| 648 | boolean discoverable = false; |
| 649 | switch (mode) { |
| 650 | case BluetoothAdapter.SCAN_MODE_NONE: |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 651 | pairable = false; |
| 652 | discoverable = false; |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 653 | break; |
| 654 | case BluetoothAdapter.SCAN_MODE_CONNECTABLE: |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 655 | pairable = true; |
| 656 | discoverable = false; |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 657 | break; |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 658 | case BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE: |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 659 | pairable = true; |
| 660 | discoverable = true; |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 661 | break; |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 662 | default: |
| 663 | Log.w(TAG, "Requested invalid scan mode " + mode); |
| 664 | return false; |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 665 | } |
| 666 | setPropertyBoolean("Pairable", pairable); |
| 667 | setPropertyBoolean("Discoverable", discoverable); |
| 668 | |
| 669 | return true; |
| 670 | } |
| 671 | |
| 672 | /*package*/ synchronized String getProperty (String name) { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 673 | if (!mAdapterProperties.isEmpty()) |
| 674 | return mAdapterProperties.get(name); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 675 | getAllProperties(); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 676 | return mAdapterProperties.get(name); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | public synchronized String getAddress() { |
| 680 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
| 681 | return getProperty("Address"); |
| 682 | } |
| 683 | |
| 684 | public synchronized String getName() { |
| 685 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
| 686 | return getProperty("Name"); |
| 687 | } |
| 688 | |
| 689 | /** |
| 690 | * Returns the user-friendly name of a remote device. This value is |
| 691 | * returned from our local cache, which is updated when onPropertyChange |
| 692 | * event is received. |
| 693 | * Do not expect to retrieve the updated remote name immediately after |
| 694 | * changing the name on the remote device. |
| 695 | * |
| 696 | * @param address Bluetooth address of remote device. |
| 697 | * |
| 698 | * @return The user-friendly name of the specified remote device. |
| 699 | */ |
| 700 | public synchronized String getRemoteName(String address) { |
| 701 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 702 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 703 | return null; |
| 704 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 705 | Map <String, String> properties = mDeviceProperties.get(address); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 706 | if (properties != null) return properties.get("Name"); |
| 707 | return null; |
| 708 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 709 | |
| 710 | /** |
| 711 | * Get the discoverability window for the device. A timeout of zero |
| 712 | * means that the device is permanently discoverable (if the device is |
| 713 | * in the discoverable mode). |
| 714 | * |
| 715 | * @return The discoverability window of the device, in seconds. A negative |
| 716 | * value indicates an error. |
| 717 | */ |
| 718 | public synchronized int getDiscoverableTimeout() { |
| 719 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 720 | String timeout = getProperty("DiscoverableTimeout"); |
| 721 | if (timeout != null) |
| 722 | return Integer.valueOf(timeout); |
| 723 | else |
| 724 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 725 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 726 | |
| 727 | public synchronized int getScanMode() { |
| 728 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 729 | if (!isEnabled()) |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 730 | return BluetoothAdapter.SCAN_MODE_NONE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 731 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 732 | boolean pairable = getProperty("Pairable").equals("true"); |
| 733 | boolean discoverable = getProperty("Discoverable").equals("true"); |
| 734 | return bluezStringToScanMode (pairable, discoverable); |
| 735 | } |
| 736 | |
| 737 | public synchronized boolean startDiscovery() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 738 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 739 | "Need BLUETOOTH_ADMIN permission"); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 740 | if (!isEnabled()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 741 | return false; |
| 742 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 743 | return startDiscoveryNative(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 744 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 745 | |
| 746 | public synchronized boolean cancelDiscovery() { |
| 747 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 748 | "Need BLUETOOTH_ADMIN permission"); |
| 749 | return stopDiscoveryNative(); |
| 750 | } |
| 751 | |
| 752 | public synchronized boolean isDiscovering() { |
| 753 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
| 754 | return mIsDiscovering; |
| 755 | } |
| 756 | |
| 757 | /* package */ void setIsDiscovering(boolean isDiscovering) { |
| 758 | mIsDiscovering = isDiscovering; |
| 759 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 760 | |
| 761 | public synchronized boolean createBond(String address) { |
| 762 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 763 | "Need BLUETOOTH_ADMIN permission"); |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 764 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 765 | return false; |
| 766 | } |
| 767 | address = address.toUpperCase(); |
| 768 | |
| 769 | String[] bonding = mBondState.listInState(BluetoothDevice.BOND_BONDING); |
| 770 | if (bonding.length > 0 && !bonding[0].equals(address)) { |
| 771 | log("Ignoring createBond(): another device is bonding"); |
| 772 | // a different device is currently bonding, fail |
| 773 | return false; |
| 774 | } |
| 775 | |
| 776 | // Check for bond state only if we are not performing auto |
| 777 | // pairing exponential back-off attempts. |
| 778 | if (!mBondState.isAutoPairingAttemptsInProgress(address) && |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 779 | mBondState.getBondState(address) != BluetoothDevice.BOND_NONE) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 780 | log("Ignoring createBond(): this device is already bonding or bonded"); |
| 781 | return false; |
| 782 | } |
| 783 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 784 | if (!createPairedDeviceNative(address, 60000 /* 1 minute */)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 785 | return false; |
| 786 | } |
| 787 | |
| 788 | mBondState.setBondState(address, BluetoothDevice.BOND_BONDING); |
| 789 | return true; |
| 790 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 791 | |
| 792 | public synchronized boolean cancelBondProcess(String address) { |
| 793 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 794 | "Need BLUETOOTH_ADMIN permission"); |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 795 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 796 | return false; |
| 797 | } |
| 798 | address = address.toUpperCase(); |
| 799 | if (mBondState.getBondState(address) != BluetoothDevice.BOND_BONDING) { |
| 800 | return false; |
| 801 | } |
| 802 | |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 803 | mBondState.setBondState(address, BluetoothDevice.BOND_NONE, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 804 | BluetoothDevice.UNBOND_REASON_AUTH_CANCELED); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 805 | cancelDeviceCreationNative(address); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 806 | return true; |
| 807 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 808 | |
| 809 | public synchronized boolean removeBond(String address) { |
| 810 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 811 | "Need BLUETOOTH_ADMIN permission"); |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 812 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 813 | return false; |
| 814 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 815 | return removeDeviceNative(getObjectPathFromAddress(address)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 816 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 817 | |
| 818 | public synchronized String[] listBonds() { |
| 819 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
| 820 | return mBondState.listInState(BluetoothDevice.BOND_BONDED); |
| 821 | } |
| 822 | |
| 823 | public synchronized int getBondState(String address) { |
| 824 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 825 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 826 | return BluetoothDevice.ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 827 | } |
| 828 | return mBondState.getBondState(address.toUpperCase()); |
| 829 | } |
| 830 | |
Jaikumar Ganesh | 9488cbd | 2009-08-03 17:17:37 -0700 | [diff] [blame] | 831 | /*package*/ boolean isRemoteDeviceInCache(String address) { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 832 | return (mDeviceProperties.get(address) != null); |
Jaikumar Ganesh | 9488cbd | 2009-08-03 17:17:37 -0700 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | /*package*/ String[] getRemoteDeviceProperties(String address) { |
| 836 | String objectPath = getObjectPathFromAddress(address); |
| 837 | return (String [])getDevicePropertiesNative(objectPath); |
| 838 | } |
| 839 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 840 | /*package*/ synchronized String getRemoteDeviceProperty(String address, String property) { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 841 | Map<String, String> properties = mDeviceProperties.get(address); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 842 | if (properties != null) { |
| 843 | return properties.get(property); |
| 844 | } else { |
| 845 | // Query for remote device properties, again. |
| 846 | // We will need to reload the cache when we switch Bluetooth on / off |
| 847 | // or if we crash. |
Jaikumar Ganesh | 9488cbd | 2009-08-03 17:17:37 -0700 | [diff] [blame] | 848 | String[] propValues = getRemoteDeviceProperties(address); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 849 | if (propValues != null) { |
| 850 | addRemoteDeviceProperties(address, propValues); |
| 851 | return getRemoteDeviceProperty(address, property); |
| 852 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 853 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 854 | Log.e(TAG, "getRemoteDeviceProperty: " + property + "not present:" + address); |
| 855 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 856 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 857 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 858 | /* package */ synchronized void addRemoteDeviceProperties(String address, String[] properties) { |
Jaikumar Ganesh | 395d102 | 2009-06-19 16:12:31 -0700 | [diff] [blame] | 859 | /* |
| 860 | * We get a DeviceFound signal every time RSSI changes or name changes. |
| 861 | * Don't create a new Map object every time */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 862 | Map<String, String> propertyValues = mDeviceProperties.get(address); |
Jaikumar Ganesh | efa3367 | 2009-08-28 13:48:55 -0700 | [diff] [blame] | 863 | if (propertyValues == null) { |
Jaikumar Ganesh | 395d102 | 2009-06-19 16:12:31 -0700 | [diff] [blame] | 864 | propertyValues = new HashMap<String, String>(); |
| 865 | } |
| 866 | |
Jaikumar Ganesh | 8bc8ce4 | 2009-06-24 16:42:51 -0700 | [diff] [blame] | 867 | for (int i = 0; i < properties.length; i++) { |
| 868 | String name = properties[i]; |
Jaikumar Ganesh | efa3367 | 2009-08-28 13:48:55 -0700 | [diff] [blame] | 869 | String newValue = null; |
Jaikumar Ganesh | 8bc8ce4 | 2009-06-24 16:42:51 -0700 | [diff] [blame] | 870 | int len; |
| 871 | if (name == null) { |
| 872 | Log.e(TAG, "Error: Remote Device Property at index" + i + "is null"); |
| 873 | continue; |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 874 | } |
Jaikumar Ganesh | 8bc8ce4 | 2009-06-24 16:42:51 -0700 | [diff] [blame] | 875 | if (name.equals("UUIDs") || name.equals("Nodes")) { |
Jaikumar Ganesh | efa3367 | 2009-08-28 13:48:55 -0700 | [diff] [blame] | 876 | StringBuilder str = new StringBuilder(); |
Jaikumar Ganesh | 8bc8ce4 | 2009-06-24 16:42:51 -0700 | [diff] [blame] | 877 | len = Integer.valueOf(properties[++i]); |
Jaikumar Ganesh | 8bc8ce4 | 2009-06-24 16:42:51 -0700 | [diff] [blame] | 878 | for (int j = 0; j < len; j++) { |
Jaikumar Ganesh | efa3367 | 2009-08-28 13:48:55 -0700 | [diff] [blame] | 879 | str.append(properties[++i]); |
| 880 | str.append(","); |
| 881 | } |
| 882 | if (len > 0) { |
| 883 | newValue = str.toString(); |
Jaikumar Ganesh | 8bc8ce4 | 2009-06-24 16:42:51 -0700 | [diff] [blame] | 884 | } |
| 885 | } else { |
| 886 | newValue = properties[++i]; |
| 887 | } |
Jaikumar Ganesh | efa3367 | 2009-08-28 13:48:55 -0700 | [diff] [blame] | 888 | |
Jaikumar Ganesh | 8bc8ce4 | 2009-06-24 16:42:51 -0700 | [diff] [blame] | 889 | propertyValues.put(name, newValue); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 890 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 891 | mDeviceProperties.put(address, propertyValues); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 892 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 893 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 894 | /* package */ void removeRemoteDeviceProperties(String address) { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 895 | mDeviceProperties.remove(address); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | /* package */ synchronized void setRemoteDeviceProperty(String address, String name, |
| 899 | String value) { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 900 | Map <String, String> propVal = mDeviceProperties.get(address); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 901 | if (propVal != null) { |
| 902 | propVal.put(name, value); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 903 | mDeviceProperties.put(address, propVal); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 904 | } else { |
| 905 | 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] | 906 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 907 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 908 | |
| 909 | /** |
Lixin Yue | efa1dd7 | 2009-08-31 15:55:13 +0800 | [diff] [blame] | 910 | * Sets the remote device trust state. |
| 911 | * |
| 912 | * @return boolean to indicate operation success or fail |
| 913 | */ |
| 914 | public synchronized boolean setTrust(String address, boolean value) { |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 915 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
Lixin Yue | efa1dd7 | 2009-08-31 15:55:13 +0800 | [diff] [blame] | 916 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
| 917 | return false; |
| 918 | } |
| 919 | |
| 920 | return setDevicePropertyBooleanNative(getObjectPathFromAddress(address), "Trusted", |
| 921 | value ? 1 : 0); |
| 922 | } |
| 923 | |
| 924 | /** |
| 925 | * Gets the remote device trust state as boolean. |
| 926 | * Note: this value may be |
| 927 | * retrieved from cache if we retrieved the data before * |
| 928 | * |
| 929 | * @return boolean to indicate trust or untrust state |
| 930 | */ |
| 931 | public synchronized boolean getTrustState(String address) { |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 932 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
Lixin Yue | efa1dd7 | 2009-08-31 15:55:13 +0800 | [diff] [blame] | 933 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
| 934 | return false; |
| 935 | } |
| 936 | |
| 937 | String val = getRemoteDeviceProperty(address, "Trusted"); |
| 938 | if (val == null) { |
| 939 | return false; |
| 940 | } else { |
| 941 | return val.equals("true") ? true : false; |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | /** |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 946 | * 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] | 947 | * integer. |
| 948 | * |
| 949 | * Note: this value is retrieved from cache, because we get it during |
| 950 | * remote-device discovery. |
| 951 | * |
| 952 | * @return 32-bit integer encoding the remote major, minor, and service |
| 953 | * classes. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 954 | */ |
| 955 | public synchronized int getRemoteClass(String address) { |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 956 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
Nick Pelly | ea600cc | 2009-03-31 14:56:26 -0700 | [diff] [blame] | 957 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
| 958 | return BluetoothClass.ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 959 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 960 | String val = getRemoteDeviceProperty(address, "Class"); |
| 961 | if (val == null) |
| 962 | return BluetoothClass.ERROR; |
| 963 | else { |
| 964 | return Integer.valueOf(val); |
| 965 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 966 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 967 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 968 | |
| 969 | /** |
| 970 | * Gets the remote features encoded as bit mask. |
| 971 | * |
| 972 | * Note: This method may be obsoleted soon. |
| 973 | * |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 974 | * @return String array of 128bit UUIDs |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 975 | */ |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 976 | public synchronized String[] getRemoteUuids(String address) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 977 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 978 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 979 | return null; |
| 980 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 981 | String value = getRemoteDeviceProperty(address, "UUIDs"); |
| 982 | String[] uuids = null; |
| 983 | // The UUIDs are stored as a "," separated string. |
| 984 | if (value != null) |
| 985 | uuids = value.split(","); |
| 986 | return uuids; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 987 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 988 | |
| 989 | /** |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 990 | * Gets the rfcomm channel associated with the UUID. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 991 | * |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 992 | * @param address Address of the remote device |
| 993 | * @param uuid UUID of the service attribute |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 994 | * |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 995 | * @return rfcomm channel associated with the service attribute |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 996 | */ |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 997 | public int getRemoteServiceChannel(String address, String uuid) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 998 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 999 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 1000 | return BluetoothDevice.ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1001 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 1002 | return getDeviceServiceChannelNative(getObjectPathFromAddress(address), uuid, 0x0004); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1003 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1004 | |
| 1005 | public synchronized boolean setPin(String address, byte[] pin) { |
| 1006 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 1007 | "Need BLUETOOTH_ADMIN permission"); |
| 1008 | if (pin == null || pin.length <= 0 || pin.length > 16 || |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1009 | !BluetoothAdapter.checkBluetoothAddress(address)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1010 | return false; |
| 1011 | } |
| 1012 | address = address.toUpperCase(); |
| 1013 | Integer data = mEventLoop.getPasskeyAgentRequestData().remove(address); |
| 1014 | if (data == null) { |
| 1015 | Log.w(TAG, "setPin(" + address + ") called but no native data available, " + |
| 1016 | "ignoring. Maybe the PasskeyAgent Request was cancelled by the remote device" + |
| 1017 | " or by bluez.\n"); |
| 1018 | return false; |
| 1019 | } |
| 1020 | // bluez API wants pin as a string |
| 1021 | String pinString; |
| 1022 | try { |
| 1023 | pinString = new String(pin, "UTF8"); |
| 1024 | } catch (UnsupportedEncodingException uee) { |
| 1025 | Log.e(TAG, "UTF8 not supported?!?"); |
| 1026 | return false; |
| 1027 | } |
| 1028 | return setPinNative(address, pinString, data.intValue()); |
| 1029 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1030 | |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 1031 | public synchronized boolean setPasskey(String address, int passkey) { |
| 1032 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 1033 | "Need BLUETOOTH_ADMIN permission"); |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1034 | if (passkey < 0 || passkey > 999999 || !BluetoothAdapter.checkBluetoothAddress(address)) { |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 1035 | return false; |
| 1036 | } |
| 1037 | address = address.toUpperCase(); |
| 1038 | Integer data = mEventLoop.getPasskeyAgentRequestData().remove(address); |
| 1039 | if (data == null) { |
| 1040 | Log.w(TAG, "setPasskey(" + address + ") called but no native data available, " + |
| 1041 | "ignoring. Maybe the PasskeyAgent Request was cancelled by the remote device" + |
| 1042 | " or by bluez.\n"); |
| 1043 | return false; |
| 1044 | } |
| 1045 | return setPasskeyNative(address, passkey, data.intValue()); |
| 1046 | } |
| 1047 | |
| 1048 | public synchronized boolean setPairingConfirmation(String address, boolean confirm) { |
| 1049 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 1050 | "Need BLUETOOTH_ADMIN permission"); |
| 1051 | address = address.toUpperCase(); |
| 1052 | Integer data = mEventLoop.getPasskeyAgentRequestData().remove(address); |
| 1053 | if (data == null) { |
| 1054 | Log.w(TAG, "setPasskey(" + address + ") called but no native data available, " + |
| 1055 | "ignoring. Maybe the PasskeyAgent Request was cancelled by the remote device" + |
| 1056 | " or by bluez.\n"); |
| 1057 | return false; |
| 1058 | } |
| 1059 | return setPairingConfirmationNative(address, confirm, data.intValue()); |
| 1060 | } |
| 1061 | |
| 1062 | public synchronized boolean cancelPairingUserInput(String address) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1063 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 1064 | "Need BLUETOOTH_ADMIN permission"); |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1065 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1066 | return false; |
| 1067 | } |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1068 | mBondState.setBondState(address, BluetoothDevice.BOND_NONE, |
Jaikumar Ganesh | 397d8f4 | 2009-08-21 11:50:17 -0700 | [diff] [blame] | 1069 | BluetoothDevice.UNBOND_REASON_AUTH_CANCELED); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1070 | address = address.toUpperCase(); |
| 1071 | Integer data = mEventLoop.getPasskeyAgentRequestData().remove(address); |
| 1072 | if (data == null) { |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 1073 | Log.w(TAG, "cancelUserInputNative(" + address + ") called but no native data " + |
| 1074 | "available, ignoring. Maybe the PasskeyAgent Request was already cancelled " + |
| 1075 | "by the remote or by bluez.\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1076 | return false; |
| 1077 | } |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 1078 | return cancelPairingUserInputNative(address, data.intValue()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1079 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1080 | |
| 1081 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 1082 | @Override |
| 1083 | public void onReceive(Context context, Intent intent) { |
| 1084 | String action = intent.getAction(); |
| 1085 | if (action.equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)) { |
| 1086 | ContentResolver resolver = context.getContentResolver(); |
| 1087 | // Query the airplane mode from Settings.System just to make sure that |
| 1088 | // some random app is not sending this intent and disabling bluetooth |
| 1089 | boolean enabled = !isAirplaneModeOn(); |
| 1090 | // If bluetooth is currently expected to be on, then enable or disable bluetooth |
| 1091 | if (Settings.Secure.getInt(resolver, Settings.Secure.BLUETOOTH_ON, 0) > 0) { |
| 1092 | if (enabled) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1093 | enable(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1094 | } else { |
| 1095 | disable(false); |
| 1096 | } |
| 1097 | } |
| 1098 | } |
| 1099 | } |
| 1100 | }; |
| 1101 | |
| 1102 | private void registerForAirplaneMode() { |
| 1103 | String airplaneModeRadios = Settings.System.getString(mContext.getContentResolver(), |
| 1104 | Settings.System.AIRPLANE_MODE_RADIOS); |
| 1105 | mIsAirplaneSensitive = airplaneModeRadios == null |
| 1106 | ? true : airplaneModeRadios.contains(Settings.System.RADIO_BLUETOOTH); |
| 1107 | if (mIsAirplaneSensitive) { |
| 1108 | mIntentFilter = new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
| 1109 | mContext.registerReceiver(mReceiver, mIntentFilter); |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | /* Returns true if airplane mode is currently on */ |
| 1114 | private final boolean isAirplaneModeOn() { |
| 1115 | return Settings.System.getInt(mContext.getContentResolver(), |
| 1116 | Settings.System.AIRPLANE_MODE_ON, 0) == 1; |
| 1117 | } |
| 1118 | |
| 1119 | @Override |
| 1120 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1121 | pw.println("\nmIsAirplaneSensitive = " + mIsAirplaneSensitive + "\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1122 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1123 | switch(mBluetoothState) { |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 1124 | case BluetoothAdapter.STATE_OFF: |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1125 | pw.println("\nBluetooth OFF\n"); |
| 1126 | return; |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 1127 | case BluetoothAdapter.STATE_TURNING_ON: |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1128 | pw.println("\nBluetooth TURNING ON\n"); |
| 1129 | return; |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 1130 | case BluetoothAdapter.STATE_TURNING_OFF: |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1131 | pw.println("\nBluetooth TURNING OFF\n"); |
| 1132 | return; |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 1133 | case BluetoothAdapter.STATE_ON: |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1134 | pw.println("\nBluetooth ON\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1135 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1136 | |
| 1137 | pw.println("\nLocal address = " + getAddress()); |
| 1138 | pw.println("\nLocal name = " + getName()); |
| 1139 | pw.println("\nisDiscovering() = " + isDiscovering()); |
| 1140 | |
| 1141 | BluetoothHeadset headset = new BluetoothHeadset(mContext, null); |
| 1142 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1143 | pw.println("\n--Known devices--"); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1144 | for (String address : mDeviceProperties.keySet()) { |
Nick Pelly | 1eada0d | 2009-08-26 10:57:33 -0700 | [diff] [blame] | 1145 | int bondState = mBondState.getBondState(address); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1146 | pw.printf("%s %10s (%d) %s\n", address, |
Nick Pelly | 1eada0d | 2009-08-26 10:57:33 -0700 | [diff] [blame] | 1147 | toBondStateString(bondState), |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1148 | mBondState.getAttempt(address), |
| 1149 | getRemoteName(address)); |
Nick Pelly | 1eada0d | 2009-08-26 10:57:33 -0700 | [diff] [blame] | 1150 | if (bondState == BluetoothDevice.BOND_BONDED) { |
| 1151 | String[] uuids = getRemoteUuids(address); |
| 1152 | if (uuids == null) { |
| 1153 | pw.printf("\tuuids = null\n"); |
| 1154 | } else { |
| 1155 | for (String uuid : uuids) { |
Nick Pelly | b015e19 | 2009-08-26 14:11:24 -0700 | [diff] [blame] | 1156 | pw.printf("\t" + uuid + "\n"); |
Nick Pelly | 1eada0d | 2009-08-26 10:57:33 -0700 | [diff] [blame] | 1157 | } |
| 1158 | } |
| 1159 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1160 | } |
| 1161 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 1162 | String value = getProperty("Devices"); |
Nick Pelly | 1eada0d | 2009-08-26 10:57:33 -0700 | [diff] [blame] | 1163 | String[] devicesObjectPath = null; |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 1164 | if (value != null) { |
| 1165 | devicesObjectPath = value.split(","); |
| 1166 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1167 | pw.println("\n--ACL connected devices--"); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 1168 | for (String device : devicesObjectPath) { |
| 1169 | pw.println(getAddressFromObjectPath(device)); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | // Rather not do this from here, but no-where else and I need this |
| 1173 | // dump |
| 1174 | pw.println("\n--Headset Service--"); |
| 1175 | switch (headset.getState()) { |
| 1176 | case BluetoothHeadset.STATE_DISCONNECTED: |
| 1177 | pw.println("getState() = STATE_DISCONNECTED"); |
| 1178 | break; |
| 1179 | case BluetoothHeadset.STATE_CONNECTING: |
| 1180 | pw.println("getState() = STATE_CONNECTING"); |
| 1181 | break; |
| 1182 | case BluetoothHeadset.STATE_CONNECTED: |
| 1183 | pw.println("getState() = STATE_CONNECTED"); |
| 1184 | break; |
| 1185 | case BluetoothHeadset.STATE_ERROR: |
| 1186 | pw.println("getState() = STATE_ERROR"); |
| 1187 | break; |
| 1188 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1189 | pw.println("getCurrentHeadset() = " + headset.getCurrentHeadset()); |
Nick Pelly | 6c901db | 2009-06-19 10:08:09 -0700 | [diff] [blame] | 1190 | pw.println("getBatteryUsageHint() = " + headset.getBatteryUsageHint()); |
| 1191 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1192 | headset.close(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1193 | } |
| 1194 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 1195 | /* package */ static int bluezStringToScanMode(boolean pairable, boolean discoverable) { |
| 1196 | if (pairable && discoverable) |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1197 | return BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE; |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 1198 | else if (pairable && !discoverable) |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1199 | return BluetoothAdapter.SCAN_MODE_CONNECTABLE; |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 1200 | else |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1201 | return BluetoothAdapter.SCAN_MODE_NONE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | /* package */ static String scanModeToBluezString(int mode) { |
| 1205 | switch (mode) { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1206 | case BluetoothAdapter.SCAN_MODE_NONE: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1207 | return "off"; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1208 | case BluetoothAdapter.SCAN_MODE_CONNECTABLE: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1209 | return "connectable"; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1210 | case BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1211 | return "discoverable"; |
| 1212 | } |
| 1213 | return null; |
| 1214 | } |
| 1215 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 1216 | /*package*/ String getAddressFromObjectPath(String objectPath) { |
| 1217 | String adapterObjectPath = getProperty("ObjectPath"); |
| 1218 | if (adapterObjectPath == null || objectPath == null) { |
| 1219 | Log.e(TAG, "getAddressFromObjectPath: AdpaterObjectPath:" + adapterObjectPath + |
| 1220 | " or deviceObjectPath:" + objectPath + " is null"); |
| 1221 | return null; |
| 1222 | } |
| 1223 | if (!objectPath.startsWith(adapterObjectPath)) { |
| 1224 | Log.e(TAG, "getAddressFromObjectPath: AdpaterObjectPath:" + adapterObjectPath + |
| 1225 | " is not a prefix of deviceObjectPath:" + objectPath + |
| 1226 | "bluetoothd crashed ?"); |
| 1227 | return null; |
| 1228 | } |
| 1229 | String address = objectPath.substring(adapterObjectPath.length()); |
| 1230 | if (address != null) return address.replace('_', ':'); |
| 1231 | |
| 1232 | Log.e(TAG, "getAddressFromObjectPath: Address being returned is null"); |
| 1233 | return null; |
| 1234 | } |
| 1235 | |
| 1236 | /*package*/ String getObjectPathFromAddress(String address) { |
| 1237 | String path = getProperty("ObjectPath"); |
| 1238 | if (path == null) { |
| 1239 | Log.e(TAG, "Error: Object Path is null"); |
| 1240 | return null; |
| 1241 | } |
| 1242 | path = path + address.replace(":", "_"); |
| 1243 | return path; |
| 1244 | } |
| 1245 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1246 | private static void log(String msg) { |
| 1247 | Log.d(TAG, msg); |
| 1248 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 1249 | |
| 1250 | private native static void classInitNative(); |
| 1251 | private native void initializeNativeDataNative(); |
| 1252 | private native boolean setupNativeDataNative(); |
| 1253 | private native boolean tearDownNativeDataNative(); |
| 1254 | private native void cleanupNativeDataNative(); |
| 1255 | private native String getAdapterPathNative(); |
| 1256 | |
| 1257 | private native int isEnabledNative(); |
| 1258 | private native int enableNative(); |
| 1259 | private native int disableNative(); |
| 1260 | |
| 1261 | private native Object[] getAdapterPropertiesNative(); |
| 1262 | private native Object[] getDevicePropertiesNative(String objectPath); |
| 1263 | private native boolean setAdapterPropertyStringNative(String key, String value); |
| 1264 | private native boolean setAdapterPropertyIntegerNative(String key, int value); |
| 1265 | private native boolean setAdapterPropertyBooleanNative(String key, int value); |
| 1266 | |
| 1267 | private native boolean startDiscoveryNative(); |
| 1268 | private native boolean stopDiscoveryNative(); |
| 1269 | |
| 1270 | private native boolean createPairedDeviceNative(String address, int timeout_ms); |
| 1271 | private native boolean cancelDeviceCreationNative(String address); |
| 1272 | private native boolean removeDeviceNative(String objectPath); |
| 1273 | private native int getDeviceServiceChannelNative(String objectPath, String uuid, |
| 1274 | int attributeId); |
| 1275 | |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 1276 | private native boolean cancelPairingUserInputNative(String address, int nativeData); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 1277 | private native boolean setPinNative(String address, String pin, int nativeData); |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 1278 | private native boolean setPasskeyNative(String address, int passkey, int nativeData); |
| 1279 | private native boolean setPairingConfirmationNative(String address, boolean confirm, |
| 1280 | int nativeData); |
Lixin Yue | efa1dd7 | 2009-08-31 15:55:13 +0800 | [diff] [blame] | 1281 | private native boolean setDevicePropertyBooleanNative(String objectPath, String key, int value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1282 | } |