Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 1 | /* |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 2 | * Copyright (C) 2011 The Android Open Source Project |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 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; |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame^] | 21 | import android.content.ComponentName; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 22 | import android.content.Context; |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame^] | 23 | import android.content.Intent; |
| 24 | import android.content.ServiceConnection; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 25 | import android.os.IBinder; |
| 26 | import android.os.RemoteException; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 27 | import android.util.Log; |
| 28 | |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 29 | import java.util.ArrayList; |
| 30 | import java.util.List; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 31 | |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 32 | |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 33 | /** |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 34 | * This class provides the public APIs to control the Bluetooth Input |
| 35 | * Device Profile. |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 36 | * |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 37 | *<p>BluetoothInputDevice is a proxy object for controlling the Bluetooth |
| 38 | * Service via IPC. Use {@link BluetoothAdapter#getProfileProxy} to get |
| 39 | * the BluetoothInputDevice proxy object. |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 40 | * |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 41 | *<p>Each method is protected with its appropriate permission. |
| 42 | *@hide |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 43 | */ |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 44 | public final class BluetoothInputDevice implements BluetoothProfile { |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 45 | private static final String TAG = "BluetoothInputDevice"; |
| 46 | private static final boolean DBG = false; |
| 47 | |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 48 | /** |
| 49 | * Intent used to broadcast the change in connection state of the Input |
| 50 | * Device profile. |
| 51 | * |
| 52 | * <p>This intent will have 3 extras: |
| 53 | * <ul> |
| 54 | * <li> {@link #EXTRA_STATE} - The current state of the profile. </li> |
| 55 | * <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile.</li> |
| 56 | * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li> |
| 57 | * </ul> |
| 58 | * |
| 59 | * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of |
| 60 | * {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING}, |
| 61 | * {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTING}. |
| 62 | * |
| 63 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to |
| 64 | * receive. |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 65 | */ |
| 66 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 67 | public static final String ACTION_CONNECTION_STATE_CHANGED = |
| 68 | "android.bluetooth.input.profile.action.CONNECTION_STATE_CHANGED"; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 69 | |
Jaikumar Ganesh | fbe807d | 2011-01-19 13:59:32 -0800 | [diff] [blame] | 70 | /** |
| 71 | * Return codes for the connect and disconnect Bluez / Dbus calls. |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 72 | * @hide |
Jaikumar Ganesh | fbe807d | 2011-01-19 13:59:32 -0800 | [diff] [blame] | 73 | */ |
| 74 | public static final int INPUT_DISCONNECT_FAILED_NOT_CONNECTED = 5000; |
| 75 | |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 76 | /** |
| 77 | * @hide |
| 78 | */ |
Jaikumar Ganesh | fbe807d | 2011-01-19 13:59:32 -0800 | [diff] [blame] | 79 | public static final int INPUT_CONNECT_FAILED_ALREADY_CONNECTED = 5001; |
| 80 | |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 81 | /** |
| 82 | * @hide |
| 83 | */ |
Jaikumar Ganesh | fbe807d | 2011-01-19 13:59:32 -0800 | [diff] [blame] | 84 | public static final int INPUT_CONNECT_FAILED_ATTEMPT_FAILED = 5002; |
| 85 | |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 86 | /** |
| 87 | * @hide |
| 88 | */ |
Jaikumar Ganesh | fbe807d | 2011-01-19 13:59:32 -0800 | [diff] [blame] | 89 | public static final int INPUT_OPERATION_GENERIC_FAILURE = 5003; |
| 90 | |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 91 | /** |
| 92 | * @hide |
| 93 | */ |
Jaikumar Ganesh | fbe807d | 2011-01-19 13:59:32 -0800 | [diff] [blame] | 94 | public static final int INPUT_OPERATION_SUCCESS = 5004; |
| 95 | |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame^] | 96 | private Context mContext; |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 97 | private ServiceListener mServiceListener; |
| 98 | private BluetoothAdapter mAdapter; |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame^] | 99 | private IBluetoothInputDevice mService; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * Create a BluetoothInputDevice proxy object for interacting with the local |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 103 | * Bluetooth Service which handles the InputDevice profile |
| 104 | * |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 105 | */ |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame^] | 106 | /*package*/ BluetoothInputDevice(Context context, ServiceListener l) { |
| 107 | mContext = context; |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 108 | mServiceListener = l; |
| 109 | mAdapter = BluetoothAdapter.getDefaultAdapter(); |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame^] | 110 | if (!context.bindService(new Intent(IBluetoothInputDevice.class.getName()), |
| 111 | mConnection, 0)) { |
| 112 | Log.e(TAG, "Could not bind to Bluetooth HID Service"); |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
Jaikumar Ganesh | 9bb2751 | 2011-11-28 09:59:08 -0800 | [diff] [blame] | 116 | /*package*/ void close() { |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame^] | 117 | if (DBG) log("close()"); |
| 118 | if (mConnection != null) { |
| 119 | mContext.unbindService(mConnection); |
| 120 | mConnection = null; |
| 121 | } |
Jaikumar Ganesh | 9bb2751 | 2011-11-28 09:59:08 -0800 | [diff] [blame] | 122 | mServiceListener = null; |
| 123 | } |
| 124 | |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 125 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 126 | * Initiate connection to a profile of the remote bluetooth device. |
| 127 | * |
| 128 | * <p> The system supports connection to multiple input devices. |
| 129 | * |
| 130 | * <p> This API returns false in scenarios like the profile on the |
| 131 | * device is already connected or Bluetooth is not turned on. |
| 132 | * When this API returns true, it is guaranteed that |
| 133 | * connection state intent for the profile will be broadcasted with |
| 134 | * the state. Users can get the connection state of the profile |
| 135 | * from this intent. |
| 136 | * |
| 137 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 138 | * permission. |
| 139 | * |
| 140 | * @param device Remote Bluetooth Device |
| 141 | * @return false on immediate error, |
| 142 | * true otherwise |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 143 | * @hide |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 144 | */ |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 145 | public boolean connect(BluetoothDevice device) { |
| 146 | if (DBG) log("connect(" + device + ")"); |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame^] | 147 | if (mService != null && isEnabled() && isValidDevice(device)) { |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 148 | try { |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame^] | 149 | return mService.connect(device); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 150 | } catch (RemoteException e) { |
| 151 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 152 | return false; |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame^] | 153 | } |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 154 | } |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 155 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 156 | return false; |
| 157 | } |
| 158 | |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 159 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 160 | * Initiate disconnection from a profile |
| 161 | * |
| 162 | * <p> This API will return false in scenarios like the profile on the |
| 163 | * Bluetooth device is not in connected state etc. When this API returns, |
| 164 | * true, it is guaranteed that the connection state change |
| 165 | * intent will be broadcasted with the state. Users can get the |
| 166 | * disconnection state of the profile from this intent. |
| 167 | * |
| 168 | * <p> If the disconnection is initiated by a remote device, the state |
| 169 | * will transition from {@link #STATE_CONNECTED} to |
| 170 | * {@link #STATE_DISCONNECTED}. If the disconnect is initiated by the |
| 171 | * host (local) device the state will transition from |
| 172 | * {@link #STATE_CONNECTED} to state {@link #STATE_DISCONNECTING} to |
| 173 | * state {@link #STATE_DISCONNECTED}. The transition to |
| 174 | * {@link #STATE_DISCONNECTING} can be used to distinguish between the |
| 175 | * two scenarios. |
| 176 | * |
| 177 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 178 | * permission. |
| 179 | * |
| 180 | * @param device Remote Bluetooth Device |
| 181 | * @return false on immediate error, |
| 182 | * true otherwise |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 183 | * @hide |
| 184 | */ |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 185 | public boolean disconnect(BluetoothDevice device) { |
| 186 | if (DBG) log("disconnect(" + device + ")"); |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame^] | 187 | if (mService != null && isEnabled() && isValidDevice(device)) { |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 188 | try { |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame^] | 189 | return mService.disconnect(device); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 190 | } catch (RemoteException e) { |
| 191 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 192 | return false; |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame^] | 193 | } |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 194 | } |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 195 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 196 | return false; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /** |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 200 | * {@inheritDoc} |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 201 | */ |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 202 | public List<BluetoothDevice> getConnectedDevices() { |
| 203 | if (DBG) log("getConnectedDevices()"); |
| 204 | if (mService != null && isEnabled()) { |
| 205 | try { |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame^] | 206 | return mService.getConnectedDevices(); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 207 | } catch (RemoteException e) { |
| 208 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 209 | return new ArrayList<BluetoothDevice>(); |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame^] | 210 | } |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 211 | } |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 212 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 213 | return new ArrayList<BluetoothDevice>(); |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /** |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 217 | * {@inheritDoc} |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 218 | */ |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 219 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
| 220 | if (DBG) log("getDevicesMatchingStates()"); |
| 221 | if (mService != null && isEnabled()) { |
| 222 | try { |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame^] | 223 | return mService.getDevicesMatchingConnectionStates(states); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 224 | } catch (RemoteException e) { |
| 225 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 226 | return new ArrayList<BluetoothDevice>(); |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame^] | 227 | } |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 228 | } |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 229 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 230 | return new ArrayList<BluetoothDevice>(); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * {@inheritDoc} |
| 235 | */ |
| 236 | public int getConnectionState(BluetoothDevice device) { |
| 237 | if (DBG) log("getState(" + device + ")"); |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame^] | 238 | if (mService != null && isEnabled() && isValidDevice(device)) { |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 239 | try { |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame^] | 240 | return mService.getConnectionState(device); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 241 | } catch (RemoteException e) { |
| 242 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 243 | return BluetoothProfile.STATE_DISCONNECTED; |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame^] | 244 | } |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 245 | } |
| 246 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 247 | return BluetoothProfile.STATE_DISCONNECTED; |
| 248 | } |
| 249 | |
| 250 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 251 | * Set priority of the profile |
| 252 | * |
| 253 | * <p> The device should already be paired. |
| 254 | * Priority can be one of {@link #PRIORITY_ON} or |
| 255 | * {@link #PRIORITY_OFF}, |
| 256 | * |
| 257 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 258 | * permission. |
| 259 | * |
| 260 | * @param device Paired bluetooth device |
| 261 | * @param priority |
| 262 | * @return true if priority is set, false on error |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 263 | * @hide |
| 264 | */ |
| 265 | public boolean setPriority(BluetoothDevice device, int priority) { |
| 266 | if (DBG) log("setPriority(" + device + ", " + priority + ")"); |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame^] | 267 | if (mService != null && isEnabled() && isValidDevice(device)) { |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 268 | if (priority != BluetoothProfile.PRIORITY_OFF && |
| 269 | priority != BluetoothProfile.PRIORITY_ON) { |
| 270 | return false; |
| 271 | } |
| 272 | try { |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame^] | 273 | return mService.setPriority(device, priority); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 274 | } catch (RemoteException e) { |
| 275 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 276 | return false; |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame^] | 277 | } |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 278 | } |
| 279 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 284 | * Get the priority of the profile. |
| 285 | * |
| 286 | * <p> The priority can be any of: |
| 287 | * {@link #PRIORITY_AUTO_CONNECT}, {@link #PRIORITY_OFF}, |
| 288 | * {@link #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED} |
| 289 | * |
| 290 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 291 | * |
| 292 | * @param device Bluetooth device |
| 293 | * @return priority of the device |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 294 | * @hide |
| 295 | */ |
| 296 | public int getPriority(BluetoothDevice device) { |
| 297 | if (DBG) log("getPriority(" + device + ")"); |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame^] | 298 | if (mService != null && isEnabled() && isValidDevice(device)) { |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 299 | try { |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame^] | 300 | return mService.getPriority(device); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 301 | } catch (RemoteException e) { |
| 302 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 303 | return BluetoothProfile.PRIORITY_OFF; |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame^] | 304 | } |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 305 | } |
| 306 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 307 | return BluetoothProfile.PRIORITY_OFF; |
| 308 | } |
| 309 | |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame^] | 310 | private ServiceConnection mConnection = new ServiceConnection() { |
| 311 | public void onServiceConnected(ComponentName className, IBinder service) { |
| 312 | if (DBG) Log.d(TAG, "Proxy object connected"); |
| 313 | mService = IBluetoothInputDevice.Stub.asInterface(service); |
| 314 | |
| 315 | if (mServiceListener != null) { |
| 316 | mServiceListener.onServiceConnected(BluetoothProfile.INPUT_DEVICE, BluetoothInputDevice.this); |
| 317 | } |
| 318 | } |
| 319 | public void onServiceDisconnected(ComponentName className) { |
| 320 | if (DBG) Log.d(TAG, "Proxy object disconnected"); |
| 321 | mService = null; |
| 322 | if (mServiceListener != null) { |
| 323 | mServiceListener.onServiceDisconnected(BluetoothProfile.INPUT_DEVICE); |
| 324 | } |
| 325 | } |
| 326 | }; |
| 327 | |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 328 | private boolean isEnabled() { |
| 329 | if (mAdapter.getState() == BluetoothAdapter.STATE_ON) return true; |
| 330 | return false; |
| 331 | } |
| 332 | |
| 333 | private boolean isValidDevice(BluetoothDevice device) { |
| 334 | if (device == null) return false; |
| 335 | |
| 336 | if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true; |
| 337 | return false; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | private static void log(String msg) { |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 341 | Log.d(TAG, msg); |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 342 | } |
| 343 | } |