blob: bc8a836183bc6e5747d1e7ccda45a98327ea2f77 [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
88 private final IBluetooth mService;
89 private final Context mContext;
90
91 /**
92 * Create a BluetoothInputDevice proxy object for interacting with the local
93 * Bluetooth Service which handle the HID profile.
94 * @param c Context
95 */
96 public BluetoothInputDevice(Context c) {
97 mContext = c;
98
99 IBinder b = ServiceManager.getService(BluetoothAdapter.BLUETOOTH_SERVICE);
100 if (b != null) {
101 mService = IBluetooth.Stub.asInterface(b);
102 } else {
103 Log.w(TAG, "Bluetooth Service not available!");
104
105 // Instead of throwing an exception which prevents people from going
106 // into Wireless settings in the emulator. Let it crash later when it is actually used.
107 mService = null;
108 }
109 }
110
111 /** Initiate a connection to an Input device.
112 *
113 * This function returns false on error and true if the connection
114 * attempt is being made.
115 *
116 * Listen for INPUT_DEVICE_STATE_CHANGED_ACTION to find out when the
117 * connection is completed.
118 * @param device Remote BT device.
119 * @return false on immediate error, true otherwise
120 * @hide
121 */
122 public boolean connectInputDevice(BluetoothDevice device) {
123 if (DBG) log("connectInputDevice(" + device + ")");
124 try {
125 return mService.connectInputDevice(device);
126 } catch (RemoteException e) {
127 Log.e(TAG, "", e);
128 return false;
129 }
130 }
131
132 /** Initiate disconnect from an Input Device.
133 * This function return false on error and true if the disconnection
134 * attempt is being made.
135 *
136 * Listen for INPUT_DEVICE_STATE_CHANGED_ACTION to find out when
137 * disconnect is completed.
138 *
139 * @param device Remote BT device.
140 * @return false on immediate error, true otherwise
141 * @hide
142 */
143 public boolean disconnectInputDevice(BluetoothDevice device) {
144 if (DBG) log("disconnectInputDevice(" + device + ")");
145 try {
146 return mService.disconnectInputDevice(device);
147 } catch (RemoteException e) {
148 Log.e(TAG, "", e);
149 return false;
150 }
151 }
152
153 /** Check if a specified InputDevice is connected.
154 *
155 * @param device Remote BT device.
156 * @return True if connected , false otherwise and on error.
157 * @hide
158 */
159 public boolean isInputDeviceConnected(BluetoothDevice device) {
160 if (DBG) log("isInputDeviceConnected(" + device + ")");
161 int state = getInputDeviceState(device);
162 if (state == STATE_CONNECTED) return true;
163 return false;
164 }
165
166 /** Check if any Input Device is connected.
167 *
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700168 * @return List of devices, empty List on error.
Jaikumar Ganesh545e6702010-06-04 10:23:03 -0700169 * @hide
170 */
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700171 public List<BluetoothDevice> getConnectedInputDevices() {
Jaikumar Ganesh545e6702010-06-04 10:23:03 -0700172 if (DBG) log("getConnectedInputDevices()");
173 try {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700174 return mService.getConnectedInputDevices();
Jaikumar Ganesh545e6702010-06-04 10:23:03 -0700175 } catch (RemoteException e) {
176 Log.e(TAG, "", e);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700177 return new ArrayList<BluetoothDevice>();
Jaikumar Ganesh545e6702010-06-04 10:23:03 -0700178 }
179 }
180
181 /** Get the state of an Input Device.
182 *
183 * @param device Remote BT device.
184 * @return The current state of the Input Device
185 * @hide
186 */
187 public int getInputDeviceState(BluetoothDevice device) {
188 if (DBG) log("getInputDeviceState(" + device + ")");
189 try {
190 return mService.getInputDeviceState(device);
191 } catch (RemoteException e) {
192 Log.e(TAG, "", e);
193 return STATE_DISCONNECTED;
194 }
195 }
196
197 /**
198 * Set priority of an input device.
199 *
200 * Priority is a non-negative integer. Priority can take the following
201 * values:
202 * {@link PRIORITY_ON}, {@link PRIORITY_OFF}, {@link PRIORITY_AUTO_CONNECT}
203 *
204 * @param device Paired device.
205 * @param priority Integer priority
206 * @return true if priority is set, false on error
207 */
208 public boolean setInputDevicePriority(BluetoothDevice device, int priority) {
209 if (DBG) log("setInputDevicePriority(" + device + ", " + priority + ")");
210 try {
211 return mService.setInputDevicePriority(device, priority);
212 } catch (RemoteException e) {
213 Log.e(TAG, "", e);
214 return false;
215 }
216 }
217
218 /**
219 * Get the priority associated with an Input Device.
220 *
221 * @param device Input Device
222 * @return non-negative priority, or negative error code on error.
223 */
224 public int getInputDevicePriority(BluetoothDevice device) {
225 if (DBG) log("getInputDevicePriority(" + device + ")");
226 try {
227 return mService.getInputDevicePriority(device);
228 } catch (RemoteException e) {
229 Log.e(TAG, "", e);
230 return PRIORITY_OFF;
231 }
232 }
233
234 private static void log(String msg) {
235 Log.d(TAG, msg);
236 }
237}