blob: a70de59a48f621746d739b44796c041ee69eb2eb [file] [log] [blame]
Jaikumar Ganesh545e6702010-06-04 10:23:03 -07001/*
2 * Copyright (C) 2010 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
17package android.bluetooth;
18
19import android.annotation.SdkConstant;
20import android.annotation.SdkConstant.SdkConstantType;
21import android.content.Context;
22import android.os.IBinder;
23import android.os.RemoteException;
24import android.os.ServiceManager;
25import android.util.Log;
26
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070027import java.util.ArrayList;
28import java.util.List;
Jaikumar Ganesh545e6702010-06-04 10:23:03 -070029
30/**
31 * Public API for controlling the Bluetooth HID (Input Device) Profile
32 *
33 * BluetoothInputDevice is a proxy object used to make calls to Bluetooth Service
34 * which handles the HID profile.
35 *
36 * Creating a BluetoothInputDevice object will initiate a binding with the
37 * Bluetooth service. Users of this object should call close() when they
38 * are finished, so that this proxy object can unbind from the service.
39 *
40 * Currently the Bluetooth service runs in the system server and this
41 * proxy object will be immediately bound to the service on construction.
42 *
43 * @hide
44 */
45public final class BluetoothInputDevice {
46 private static final String TAG = "BluetoothInputDevice";
47 private static final boolean DBG = false;
48
49 /** int extra for ACTION_INPUT_DEVICE_STATE_CHANGED */
50 public static final String EXTRA_INPUT_DEVICE_STATE =
51 "android.bluetooth.inputdevice.extra.INPUT_DEVICE_STATE";
52 /** int extra for ACTION_INPUT_DEVICE_STATE_CHANGED */
53 public static final String EXTRA_PREVIOUS_INPUT_DEVICE_STATE =
54 "android.bluetooth.inputdevice.extra.PREVIOUS_INPUT_DEVICE_STATE";
55
56 /** Indicates the state of an input device has changed.
57 * This intent will always contain EXTRA_INPUT_DEVICE_STATE,
58 * EXTRA_PREVIOUS_INPUT_DEVICE_STATE and BluetoothDevice.EXTRA_DEVICE
59 * extras.
60 */
61 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
62 public static final String ACTION_INPUT_DEVICE_STATE_CHANGED =
63 "android.bluetooth.inputdevice.action.INPUT_DEVICE_STATE_CHANGED";
64
65 public static final int STATE_DISCONNECTED = 0;
66 public static final int STATE_CONNECTING = 1;
67 public static final int STATE_CONNECTED = 2;
68 public static final int STATE_DISCONNECTING = 3;
69
70 /**
71 * Auto connection, incoming and outgoing connection are allowed at this
72 * priority level.
73 */
74 public static final int PRIORITY_AUTO_CONNECT = 1000;
75 /**
76 * Incoming and outgoing connection are allowed at this priority level
77 */
78 public static final int PRIORITY_ON = 100;
79 /**
80 * Connections to the device are not allowed at this priority level.
81 */
82 public static final int PRIORITY_OFF = 0;
83 /**
84 * Default priority level when the device is unpaired.
85 */
86 public static final int PRIORITY_UNDEFINED = -1;
87
Jaikumar Ganeshfbe807d2011-01-19 13:59:32 -080088 /**
89 * Return codes for the connect and disconnect Bluez / Dbus calls.
90 */
91 public static final int INPUT_DISCONNECT_FAILED_NOT_CONNECTED = 5000;
92
93 public static final int INPUT_CONNECT_FAILED_ALREADY_CONNECTED = 5001;
94
95 public static final int INPUT_CONNECT_FAILED_ATTEMPT_FAILED = 5002;
96
97 public static final int INPUT_OPERATION_GENERIC_FAILURE = 5003;
98
99 public static final int INPUT_OPERATION_SUCCESS = 5004;
100
Jaikumar Ganesh545e6702010-06-04 10:23:03 -0700101 private final IBluetooth mService;
102 private final Context mContext;
103
104 /**
105 * Create a BluetoothInputDevice proxy object for interacting with the local
106 * Bluetooth Service which handle the HID profile.
107 * @param c Context
108 */
109 public BluetoothInputDevice(Context c) {
110 mContext = c;
111
112 IBinder b = ServiceManager.getService(BluetoothAdapter.BLUETOOTH_SERVICE);
113 if (b != null) {
114 mService = IBluetooth.Stub.asInterface(b);
115 } else {
116 Log.w(TAG, "Bluetooth Service not available!");
117
118 // Instead of throwing an exception which prevents people from going
119 // into Wireless settings in the emulator. Let it crash later when it is actually used.
120 mService = null;
121 }
122 }
123
124 /** Initiate a connection to an Input device.
125 *
126 * This function returns false on error and true if the connection
127 * attempt is being made.
128 *
129 * Listen for INPUT_DEVICE_STATE_CHANGED_ACTION to find out when the
130 * connection is completed.
131 * @param device Remote BT device.
132 * @return false on immediate error, true otherwise
133 * @hide
134 */
135 public boolean connectInputDevice(BluetoothDevice device) {
136 if (DBG) log("connectInputDevice(" + device + ")");
137 try {
138 return mService.connectInputDevice(device);
139 } catch (RemoteException e) {
140 Log.e(TAG, "", e);
141 return false;
142 }
143 }
144
145 /** Initiate disconnect from an Input Device.
146 * This function return false on error and true if the disconnection
147 * attempt is being made.
148 *
149 * Listen for INPUT_DEVICE_STATE_CHANGED_ACTION to find out when
150 * disconnect is completed.
151 *
152 * @param device Remote BT device.
153 * @return false on immediate error, true otherwise
154 * @hide
155 */
156 public boolean disconnectInputDevice(BluetoothDevice device) {
157 if (DBG) log("disconnectInputDevice(" + device + ")");
158 try {
159 return mService.disconnectInputDevice(device);
160 } catch (RemoteException e) {
161 Log.e(TAG, "", e);
162 return false;
163 }
164 }
165
166 /** Check if a specified InputDevice is connected.
167 *
168 * @param device Remote BT device.
169 * @return True if connected , false otherwise and on error.
170 * @hide
171 */
172 public boolean isInputDeviceConnected(BluetoothDevice device) {
173 if (DBG) log("isInputDeviceConnected(" + device + ")");
174 int state = getInputDeviceState(device);
175 if (state == STATE_CONNECTED) return true;
176 return false;
177 }
178
179 /** Check if any Input Device is connected.
180 *
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700181 * @return List of devices, empty List on error.
Jaikumar Ganesh545e6702010-06-04 10:23:03 -0700182 * @hide
183 */
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700184 public List<BluetoothDevice> getConnectedInputDevices() {
Jaikumar Ganesh545e6702010-06-04 10:23:03 -0700185 if (DBG) log("getConnectedInputDevices()");
186 try {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700187 return mService.getConnectedInputDevices();
Jaikumar Ganesh545e6702010-06-04 10:23:03 -0700188 } catch (RemoteException e) {
189 Log.e(TAG, "", e);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700190 return new ArrayList<BluetoothDevice>();
Jaikumar Ganesh545e6702010-06-04 10:23:03 -0700191 }
192 }
193
194 /** Get the state of an Input Device.
195 *
196 * @param device Remote BT device.
197 * @return The current state of the Input Device
198 * @hide
199 */
200 public int getInputDeviceState(BluetoothDevice device) {
201 if (DBG) log("getInputDeviceState(" + device + ")");
202 try {
203 return mService.getInputDeviceState(device);
204 } catch (RemoteException e) {
205 Log.e(TAG, "", e);
206 return STATE_DISCONNECTED;
207 }
208 }
209
210 /**
211 * Set priority of an input device.
212 *
213 * Priority is a non-negative integer. Priority can take the following
214 * values:
215 * {@link PRIORITY_ON}, {@link PRIORITY_OFF}, {@link PRIORITY_AUTO_CONNECT}
216 *
217 * @param device Paired device.
218 * @param priority Integer priority
219 * @return true if priority is set, false on error
220 */
221 public boolean setInputDevicePriority(BluetoothDevice device, int priority) {
222 if (DBG) log("setInputDevicePriority(" + device + ", " + priority + ")");
223 try {
224 return mService.setInputDevicePriority(device, priority);
225 } catch (RemoteException e) {
226 Log.e(TAG, "", e);
227 return false;
228 }
229 }
230
231 /**
232 * Get the priority associated with an Input Device.
233 *
234 * @param device Input Device
235 * @return non-negative priority, or negative error code on error.
236 */
237 public int getInputDevicePriority(BluetoothDevice device) {
238 if (DBG) log("getInputDevicePriority(" + device + ")");
239 try {
240 return mService.getInputDevicePriority(device);
241 } catch (RemoteException e) {
242 Log.e(TAG, "", e);
243 return PRIORITY_OFF;
244 }
245 }
246
247 private static void log(String msg) {
248 Log.d(TAG, msg);
249 }
250}