Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [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 | package android.bluetooth; |
| 18 | |
| 19 | import android.annotation.SdkConstant; |
| 20 | import android.annotation.SdkConstant.SdkConstantType; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 21 | import android.content.ComponentName; |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 22 | import android.content.Context; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 23 | import android.content.Intent; |
| 24 | import android.content.ServiceConnection; |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 25 | import android.os.IBinder; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 26 | import android.os.RemoteException; |
| 27 | import android.os.ServiceManager; |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 28 | import android.util.Log; |
| 29 | |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 30 | import java.util.ArrayList; |
| 31 | import java.util.List; |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 34 | * This class provides the APIs to control the Bluetooth Pan |
| 35 | * Profile. |
| 36 | * |
| 37 | *<p>BluetoothPan is a proxy object for controlling the Bluetooth |
| 38 | * Service via IPC. Use {@link BluetoothAdapter#getProfileProxy} to get |
| 39 | * the BluetoothPan proxy object. |
| 40 | * |
| 41 | *<p>Each method is protected with its appropriate permission. |
| 42 | *@hide |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 43 | */ |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 44 | public final class BluetoothPan implements BluetoothProfile { |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 45 | private static final String TAG = "BluetoothPan"; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 46 | private static final boolean DBG = true; |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 47 | private static final boolean VDBG = false; |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 48 | |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 49 | /** |
| 50 | * Intent used to broadcast the change in connection state of the Pan |
| 51 | * profile. |
| 52 | * |
| 53 | * <p>This intent will have 4 extras: |
| 54 | * <ul> |
| 55 | * <li> {@link #EXTRA_STATE} - The current state of the profile. </li> |
| 56 | * <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile.</li> |
| 57 | * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li> |
| 58 | * <li> {@link #EXTRA_LOCAL_ROLE} - Which local role the remote device is |
| 59 | * bound to. </li> |
| 60 | * </ul> |
| 61 | * |
| 62 | * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of |
| 63 | * {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING}, |
| 64 | * {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTING}. |
| 65 | * |
| 66 | * <p> {@link #EXTRA_LOCAL_ROLE} can be one of {@link #LOCAL_NAP_ROLE} or |
| 67 | * {@link #LOCAL_PANU_ROLE} |
| 68 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to |
| 69 | * receive. |
| 70 | */ |
| 71 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 72 | public static final String ACTION_CONNECTION_STATE_CHANGED = |
| 73 | "android.bluetooth.pan.profile.action.CONNECTION_STATE_CHANGED"; |
Jaikumar Ganesh | 5200c8a | 2010-12-14 14:26:46 -0800 | [diff] [blame] | 74 | |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 75 | /** |
| 76 | * Extra for {@link #ACTION_CONNECTION_STATE_CHANGED} intent |
| 77 | * The local role of the PAN profile that the remote device is bound to. |
| 78 | * It can be one of {@link #LOCAL_NAP_ROLE} or {@link #LOCAL_PANU_ROLE}. |
| 79 | */ |
Jaikumar Ganesh | 5200c8a | 2010-12-14 14:26:46 -0800 | [diff] [blame] | 80 | public static final String EXTRA_LOCAL_ROLE = "android.bluetooth.pan.extra.LOCAL_ROLE"; |
| 81 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 82 | public static final int PAN_ROLE_NONE = 0; |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 83 | /** |
| 84 | * The local device is acting as a Network Access Point. |
| 85 | */ |
Jaikumar Ganesh | 5200c8a | 2010-12-14 14:26:46 -0800 | [diff] [blame] | 86 | public static final int LOCAL_NAP_ROLE = 1; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 87 | public static final int REMOTE_NAP_ROLE = 1; |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 88 | |
| 89 | /** |
| 90 | * The local device is acting as a PAN User. |
| 91 | */ |
Jaikumar Ganesh | 5200c8a | 2010-12-14 14:26:46 -0800 | [diff] [blame] | 92 | public static final int LOCAL_PANU_ROLE = 2; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 93 | public static final int REMOTE_PANU_ROLE = 2; |
Jaikumar Ganesh | 5200c8a | 2010-12-14 14:26:46 -0800 | [diff] [blame] | 94 | |
| 95 | /** |
Jaikumar Ganesh | fbe807d | 2011-01-19 13:59:32 -0800 | [diff] [blame] | 96 | * Return codes for the connect and disconnect Bluez / Dbus calls. |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 97 | * @hide |
Jaikumar Ganesh | fbe807d | 2011-01-19 13:59:32 -0800 | [diff] [blame] | 98 | */ |
| 99 | public static final int PAN_DISCONNECT_FAILED_NOT_CONNECTED = 1000; |
| 100 | |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 101 | /** |
| 102 | * @hide |
| 103 | */ |
Jaikumar Ganesh | fbe807d | 2011-01-19 13:59:32 -0800 | [diff] [blame] | 104 | public static final int PAN_CONNECT_FAILED_ALREADY_CONNECTED = 1001; |
| 105 | |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 106 | /** |
| 107 | * @hide |
| 108 | */ |
Jaikumar Ganesh | fbe807d | 2011-01-19 13:59:32 -0800 | [diff] [blame] | 109 | public static final int PAN_CONNECT_FAILED_ATTEMPT_FAILED = 1002; |
| 110 | |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 111 | /** |
| 112 | * @hide |
| 113 | */ |
Jaikumar Ganesh | fbe807d | 2011-01-19 13:59:32 -0800 | [diff] [blame] | 114 | public static final int PAN_OPERATION_GENERIC_FAILURE = 1003; |
| 115 | |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 116 | /** |
| 117 | * @hide |
| 118 | */ |
Jaikumar Ganesh | fbe807d | 2011-01-19 13:59:32 -0800 | [diff] [blame] | 119 | public static final int PAN_OPERATION_SUCCESS = 1004; |
| 120 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 121 | private Context mContext; |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 122 | private ServiceListener mServiceListener; |
| 123 | private BluetoothAdapter mAdapter; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 124 | private IBluetoothPan mPanService; |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 125 | |
| 126 | /** |
| 127 | * Create a BluetoothPan proxy object for interacting with the local |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 128 | * Bluetooth Service which handles the Pan profile |
| 129 | * |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 130 | */ |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 131 | /*package*/ BluetoothPan(Context context, ServiceListener l) { |
| 132 | mContext = context; |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 133 | mServiceListener = l; |
| 134 | mAdapter = BluetoothAdapter.getDefaultAdapter(); |
fredc | 903ac6f | 2012-04-24 03:59:57 -0700 | [diff] [blame] | 135 | try { |
| 136 | mAdapter.getBluetoothManager().registerStateChangeCallback(mStateChangeCallback); |
| 137 | } catch (RemoteException re) { |
| 138 | Log.w(TAG,"Unable to register BluetoothStateChangeCallback",re); |
| 139 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 140 | Log.d(TAG, "BluetoothPan() call bindService"); |
| 141 | if (!context.bindService(new Intent(IBluetoothPan.class.getName()), |
| 142 | mConnection, 0)) { |
| 143 | Log.e(TAG, "Could not bind to Bluetooth HID Service"); |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 144 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 145 | Log.d(TAG, "BluetoothPan(), bindService called"); |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Jaikumar Ganesh | 9bb2751 | 2011-11-28 09:59:08 -0800 | [diff] [blame] | 148 | /*package*/ void close() { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 149 | if (VDBG) log("close()"); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 150 | if (mConnection != null) { |
| 151 | mContext.unbindService(mConnection); |
| 152 | mConnection = null; |
| 153 | } |
Jaikumar Ganesh | 9bb2751 | 2011-11-28 09:59:08 -0800 | [diff] [blame] | 154 | mServiceListener = null; |
fredc | 903ac6f | 2012-04-24 03:59:57 -0700 | [diff] [blame] | 155 | try { |
| 156 | mAdapter.getBluetoothManager().unregisterStateChangeCallback(mStateChangeCallback); |
| 157 | } catch (RemoteException re) { |
| 158 | Log.w(TAG,"Unable to register BluetoothStateChangeCallback",re); |
| 159 | } |
Jaikumar Ganesh | 9bb2751 | 2011-11-28 09:59:08 -0800 | [diff] [blame] | 160 | } |
| 161 | |
fredc | 903ac6f | 2012-04-24 03:59:57 -0700 | [diff] [blame] | 162 | protected void finalize() { |
| 163 | close(); |
| 164 | } |
| 165 | |
| 166 | private IBluetoothStateChangeCallback mStateChangeCallback = new IBluetoothStateChangeCallback.Stub() { |
| 167 | |
| 168 | @Override |
| 169 | public void onBluetoothStateChange(boolean on) throws RemoteException { |
| 170 | //Handle enable request to bind again. |
| 171 | if (on) { |
| 172 | Log.d(TAG, "onBluetoothStateChange(on) call bindService"); |
| 173 | if (!mContext.bindService(new Intent(IBluetoothPan.class.getName()), |
| 174 | mConnection, 0)) { |
| 175 | Log.e(TAG, "Could not bind to Bluetooth HID Service"); |
| 176 | } |
| 177 | Log.d(TAG, "BluetoothPan(), bindService called"); |
| 178 | } else { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 179 | if (VDBG) Log.d(TAG,"Unbinding service..."); |
fredc | 903ac6f | 2012-04-24 03:59:57 -0700 | [diff] [blame] | 180 | synchronized (mConnection) { |
| 181 | try { |
| 182 | mPanService = null; |
| 183 | mContext.unbindService(mConnection); |
| 184 | } catch (Exception re) { |
| 185 | Log.e(TAG,"",re); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | }; |
| 191 | |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 192 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 193 | * Initiate connection to a profile of the remote bluetooth device. |
| 194 | * |
| 195 | * <p> This API returns false in scenarios like the profile on the |
| 196 | * device is already connected or Bluetooth is not turned on. |
| 197 | * When this API returns true, it is guaranteed that |
| 198 | * connection state intent for the profile will be broadcasted with |
| 199 | * the state. Users can get the connection state of the profile |
| 200 | * from this intent. |
| 201 | * |
| 202 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 203 | * permission. |
| 204 | * |
| 205 | * @param device Remote Bluetooth Device |
| 206 | * @return false on immediate error, |
| 207 | * true otherwise |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 208 | * @hide |
| 209 | */ |
| 210 | public boolean connect(BluetoothDevice device) { |
| 211 | if (DBG) log("connect(" + device + ")"); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 212 | if (mPanService != null && isEnabled() && |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 213 | isValidDevice(device)) { |
| 214 | try { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 215 | return mPanService.connect(device); |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 216 | } catch (RemoteException e) { |
| 217 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 218 | return false; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 219 | } |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 220 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 221 | if (mPanService == null) Log.w(TAG, "Proxy not attached to service"); |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 222 | return false; |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 226 | * Initiate disconnection from a profile |
| 227 | * |
| 228 | * <p> This API will return false in scenarios like the profile on the |
| 229 | * Bluetooth device is not in connected state etc. When this API returns, |
| 230 | * true, it is guaranteed that the connection state change |
| 231 | * intent will be broadcasted with the state. Users can get the |
| 232 | * disconnection state of the profile from this intent. |
| 233 | * |
| 234 | * <p> If the disconnection is initiated by a remote device, the state |
| 235 | * will transition from {@link #STATE_CONNECTED} to |
| 236 | * {@link #STATE_DISCONNECTED}. If the disconnect is initiated by the |
| 237 | * host (local) device the state will transition from |
| 238 | * {@link #STATE_CONNECTED} to state {@link #STATE_DISCONNECTING} to |
| 239 | * state {@link #STATE_DISCONNECTED}. The transition to |
| 240 | * {@link #STATE_DISCONNECTING} can be used to distinguish between the |
| 241 | * two scenarios. |
| 242 | * |
| 243 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 244 | * permission. |
| 245 | * |
| 246 | * @param device Remote Bluetooth Device |
| 247 | * @return false on immediate error, |
| 248 | * true otherwise |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 249 | * @hide |
| 250 | */ |
| 251 | public boolean disconnect(BluetoothDevice device) { |
| 252 | if (DBG) log("disconnect(" + device + ")"); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 253 | if (mPanService != null && isEnabled() && |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 254 | isValidDevice(device)) { |
| 255 | try { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 256 | return mPanService.disconnect(device); |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 257 | } catch (RemoteException e) { |
| 258 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 259 | return false; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 260 | } |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 261 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 262 | if (mPanService == null) Log.w(TAG, "Proxy not attached to service"); |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 263 | return false; |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Jaikumar Ganesh | 5200c8a | 2010-12-14 14:26:46 -0800 | [diff] [blame] | 266 | /** |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 267 | * {@inheritDoc} |
Jaikumar Ganesh | 5200c8a | 2010-12-14 14:26:46 -0800 | [diff] [blame] | 268 | */ |
| 269 | public List<BluetoothDevice> getConnectedDevices() { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 270 | if (VDBG) log("getConnectedDevices()"); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 271 | if (mPanService != null && isEnabled()) { |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 272 | try { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 273 | return mPanService.getConnectedDevices(); |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 274 | } catch (RemoteException e) { |
| 275 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 276 | return new ArrayList<BluetoothDevice>(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 277 | } |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 278 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 279 | if (mPanService == null) Log.w(TAG, "Proxy not attached to service"); |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 280 | return new ArrayList<BluetoothDevice>(); |
Jaikumar Ganesh | 5200c8a | 2010-12-14 14:26:46 -0800 | [diff] [blame] | 281 | } |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 282 | |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 283 | /** |
| 284 | * {@inheritDoc} |
| 285 | */ |
| 286 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 287 | if (VDBG) log("getDevicesMatchingStates()"); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 288 | if (mPanService != null && isEnabled()) { |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 289 | try { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 290 | return mPanService.getDevicesMatchingConnectionStates(states); |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 291 | } catch (RemoteException e) { |
| 292 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 293 | return new ArrayList<BluetoothDevice>(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 294 | } |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 295 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 296 | if (mPanService == null) Log.w(TAG, "Proxy not attached to service"); |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 297 | return new ArrayList<BluetoothDevice>(); |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * {@inheritDoc} |
| 302 | */ |
| 303 | public int getConnectionState(BluetoothDevice device) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 304 | if (VDBG) log("getState(" + device + ")"); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 305 | if (mPanService != null && isEnabled() |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 306 | && isValidDevice(device)) { |
| 307 | try { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 308 | return mPanService.getConnectionState(device); |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 309 | } catch (RemoteException e) { |
| 310 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 311 | return BluetoothProfile.STATE_DISCONNECTED; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 312 | } |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 313 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 314 | if (mPanService == null) Log.w(TAG, "Proxy not attached to service"); |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 315 | return BluetoothProfile.STATE_DISCONNECTED; |
| 316 | } |
| 317 | |
Jaikumar Ganesh | b70765c | 2010-09-02 12:17:05 -0700 | [diff] [blame] | 318 | public void setBluetoothTethering(boolean value) { |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 319 | if (DBG) log("setBluetoothTethering(" + value + ")"); |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 320 | try { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 321 | mPanService.setBluetoothTethering(value); |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 322 | } catch (RemoteException e) { |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 323 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 324 | } |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | public boolean isTetheringOn() { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 328 | if (VDBG) log("isTetheringOn()"); |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 329 | try { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 330 | return mPanService.isTetheringOn(); |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 331 | } catch (RemoteException e) { |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 332 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 333 | } |
Jaikumar Ganesh | e4caddb | 2012-01-25 16:16:48 -0800 | [diff] [blame] | 334 | return false; |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 335 | } |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 336 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 337 | private ServiceConnection mConnection = new ServiceConnection() { |
| 338 | public void onServiceConnected(ComponentName className, IBinder service) { |
| 339 | if (DBG) Log.d(TAG, "BluetoothPAN Proxy object connected"); |
| 340 | mPanService = IBluetoothPan.Stub.asInterface(service); |
| 341 | |
| 342 | if (mServiceListener != null) { |
| 343 | mServiceListener.onServiceConnected(BluetoothProfile.PAN, |
| 344 | BluetoothPan.this); |
| 345 | } |
| 346 | } |
| 347 | public void onServiceDisconnected(ComponentName className) { |
| 348 | if (DBG) Log.d(TAG, "BluetoothPAN Proxy object disconnected"); |
| 349 | mPanService = null; |
| 350 | if (mServiceListener != null) { |
| 351 | mServiceListener.onServiceDisconnected(BluetoothProfile.PAN); |
| 352 | } |
| 353 | } |
| 354 | }; |
| 355 | |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 356 | private boolean isEnabled() { |
| 357 | if (mAdapter.getState() == BluetoothAdapter.STATE_ON) return true; |
| 358 | return false; |
| 359 | } |
| 360 | |
| 361 | private boolean isValidDevice(BluetoothDevice device) { |
| 362 | if (device == null) return false; |
| 363 | |
| 364 | if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true; |
| 365 | return false; |
| 366 | } |
| 367 | |
| 368 | private static void log(String msg) { |
| 369 | Log.d(TAG, msg); |
| 370 | } |
Jaikumar Ganesh | 9bb2751 | 2011-11-28 09:59:08 -0800 | [diff] [blame] | 371 | } |