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