/* * Copyright (C) 2016 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.bluetooth; import android.annotation.SdkConstant; import android.annotation.SdkConstant.SdkConstantType; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.IBinder; import android.os.RemoteException; import android.util.Log; import java.util.Arrays; import java.util.ArrayList; import java.util.List; /** * @hide */ public final class BluetoothInputHost implements BluetoothProfile { private static final String TAG = BluetoothInputHost.class.getSimpleName(); /** * Intent used to broadcast the change in connection state of the Input * Host profile. * *
This intent will have 3 extras: *
{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of * {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING}, * {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTING}. * *
Requires {@link android.Manifest.permission#BLUETOOTH} permission to
* receive.
*/
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String ACTION_CONNECTION_STATE_CHANGED =
"android.bluetooth.inputhost.profile.action.CONNECTION_STATE_CHANGED";
/**
* Constants representing device subclass.
*
* @see #registerApp(String, String, String, byte, byte[],
* BluetoothHidDeviceCallback)
*/
public static final byte SUBCLASS1_NONE = (byte) 0x00;
public static final byte SUBCLASS1_KEYBOARD = (byte) 0x40;
public static final byte SUBCLASS1_MOUSE = (byte) 0x80;
public static final byte SUBCLASS1_COMBO = (byte) 0xC0;
public static final byte SUBCLASS2_UNCATEGORIZED = (byte) 0x00;
public static final byte SUBCLASS2_JOYSTICK = (byte) 0x01;
public static final byte SUBCLASS2_GAMEPAD = (byte) 0x02;
public static final byte SUBCLASS2_REMOTE_CONTROL = (byte) 0x03;
public static final byte SUBCLASS2_SENSING_DEVICE = (byte) 0x04;
public static final byte SUBCLASS2_DIGITIZER_TABLED = (byte) 0x05;
public static final byte SUBCLASS2_CARD_READER = (byte) 0x06;
/**
* Constants representing report types.
*
* @see BluetoothHidDeviceCallback#onGetReport(byte, byte, int)
* @see BluetoothHidDeviceCallback#onSetReport(byte, byte, byte[])
* @see BluetoothHidDeviceCallback#onIntrData(byte, byte[])
*/
public static final byte REPORT_TYPE_INPUT = (byte) 1;
public static final byte REPORT_TYPE_OUTPUT = (byte) 2;
public static final byte REPORT_TYPE_FEATURE = (byte) 3;
/**
* Constants representing error response for Set Report.
*
* @see BluetoothHidDeviceCallback#onSetReport(byte, byte, byte[])
*/
public static final byte ERROR_RSP_SUCCESS = (byte) 0;
public static final byte ERROR_RSP_NOT_READY = (byte) 1;
public static final byte ERROR_RSP_INVALID_RPT_ID = (byte) 2;
public static final byte ERROR_RSP_UNSUPPORTED_REQ = (byte) 3;
public static final byte ERROR_RSP_INVALID_PARAM = (byte) 4;
public static final byte ERROR_RSP_UNKNOWN = (byte) 14;
/**
* Constants representing protocol mode used set by host. Default is always
* {@link #PROTOCOL_REPORT_MODE} unless notified otherwise.
*
* @see BluetoothHidDeviceCallback#onSetProtocol(byte)
*/
public static final byte PROTOCOL_BOOT_MODE = (byte) 0;
public static final byte PROTOCOL_REPORT_MODE = (byte) 1;
private Context mContext;
private ServiceListener mServiceListener;
private IBluetoothInputHost mService;
private BluetoothAdapter mAdapter;
private static class BluetoothHidDeviceCallbackWrapper extends IBluetoothHidDeviceCallback.Stub {
private BluetoothHidDeviceCallback mCallback;
public BluetoothHidDeviceCallbackWrapper(BluetoothHidDeviceCallback callback) {
mCallback = callback;
}
@Override
public void onAppStatusChanged(BluetoothDevice pluggedDevice,
BluetoothHidDeviceAppConfiguration config, boolean registered) {
mCallback.onAppStatusChanged(pluggedDevice, config, registered);
}
@Override
public void onConnectionStateChanged(BluetoothDevice device, int state) {
mCallback.onConnectionStateChanged(device, state);
}
@Override
public void onGetReport(BluetoothDevice device, byte type, byte id, int bufferSize) {
mCallback.onGetReport(device, type, id, bufferSize);
}
@Override
public void onSetReport(BluetoothDevice device, byte type, byte id, byte[] data) {
mCallback.onSetReport(device, type, id, data);
}
@Override
public void onSetProtocol(BluetoothDevice device, byte protocol) {
mCallback.onSetProtocol(device, protocol);
}
@Override
public void onIntrData(BluetoothDevice device, byte reportId, byte[] data) {
mCallback.onIntrData(device, reportId, data);
}
@Override
public void onVirtualCableUnplug(BluetoothDevice device) {
mCallback.onVirtualCableUnplug(device);
}
}
final private IBluetoothStateChangeCallback mBluetoothStateChangeCallback =
new IBluetoothStateChangeCallback.Stub() {
public void onBluetoothStateChange(boolean up) {
Log.d(TAG, "onBluetoothStateChange: up=" + up);
synchronized (mConnection) {
if (!up) {
Log.d(TAG,"Unbinding service...");
if (mService != null) {
mService = null;
try {
mContext.unbindService(mConnection);
} catch (IllegalArgumentException e) {
Log.e(TAG,"onBluetoothStateChange: could not unbind service:", e);
}
}
} else {
try {
if (mService == null) {
Log.d(TAG,"Binding HID Device service...");
doBind();
}
} catch (IllegalStateException e) {
Log.e(TAG,"onBluetoothStateChange: could not bind to HID Dev service: ", e);
} catch (SecurityException e) {
Log.e(TAG,"onBluetoothStateChange: could not bind to HID Dev service: ", e);
}
}
}
}
};
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
Log.d(TAG, "onServiceConnected()");
mService = IBluetoothInputHost.Stub.asInterface(service);
if (mServiceListener != null) {
mServiceListener.onServiceConnected(BluetoothProfile.INPUT_HOST,
BluetoothInputHost.this);
}
}
public void onServiceDisconnected(ComponentName className) {
Log.d(TAG, "onServiceDisconnected()");
mService = null;
if (mServiceListener != null) {
mServiceListener.onServiceDisconnected(BluetoothProfile.INPUT_HOST);
}
}
};
BluetoothInputHost(Context context, ServiceListener listener) {
Log.v(TAG, "BluetoothInputHost");
mContext = context;
mServiceListener = listener;
mAdapter = BluetoothAdapter.getDefaultAdapter();
IBluetoothManager mgr = mAdapter.getBluetoothManager();
if (mgr != null) {
try {
mgr.registerStateChangeCallback(mBluetoothStateChangeCallback);
} catch (RemoteException e) {
e.printStackTrace();
}
}
doBind();
}
boolean doBind() {
Intent intent = new Intent(IBluetoothInputHost.class.getName());
ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0);
intent.setComponent(comp);
if (comp == null || !mContext.bindServiceAsUser(intent, mConnection, 0,
android.os.Process.myUserHandle())) {
Log.e(TAG, "Could not bind to Bluetooth HID Device Service with " + intent);
return false;
}
Log.d(TAG, "Bound to HID Device Service");
return true;
}
void close() {
Log.v(TAG, "close()");
IBluetoothManager mgr = mAdapter.getBluetoothManager();
if (mgr != null) {
try {
mgr.unregisterStateChangeCallback(mBluetoothStateChangeCallback);
} catch (RemoteException e) {
e.printStackTrace();
}
}
synchronized (mConnection) {
if (mService != null) {
mService = null;
try {
mContext.unbindService(mConnection);
} catch (IllegalArgumentException e) {
Log.e(TAG,"close: could not unbind HID Dev service: ", e);
}
}
}
mServiceListener = null;
}
/**
* {@inheritDoc}
*/
public List