FileDocCategorySizeDatePackage
BluetoothInputDevice.javaAPI DocAndroid 5.1 API22919Thu Mar 12 22:22:10 GMT 2015android.bluetooth

BluetoothInputDevice

public final class BluetoothInputDevice extends Object implements BluetoothProfile
This class provides the public APIs to control the Bluetooth Input Device Profile.

BluetoothInputDevice is a proxy object for controlling the Bluetooth Service via IPC. Use {@link BluetoothAdapter#getProfileProxy} to get the BluetoothInputDevice proxy object.

Each method is protected with its appropriate permission.

hide

Fields Summary
private static final String
TAG
private static final boolean
DBG
private static final boolean
VDBG
public static final String
ACTION_CONNECTION_STATE_CHANGED
Intent used to broadcast the change in connection state of the Input Device profile.

This intent will have 3 extras:

  • {@link #EXTRA_STATE} - The current state of the profile.
  • {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile.
  • {@link BluetoothDevice#EXTRA_DEVICE} - The remote device.

{@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.

public static final String
ACTION_PROTOCOL_MODE_CHANGED
public static final String
ACTION_HANDSHAKE
public static final String
ACTION_REPORT
public static final String
ACTION_VIRTUAL_UNPLUG_STATUS
public static final int
INPUT_DISCONNECT_FAILED_NOT_CONNECTED
Return codes for the connect and disconnect Bluez / Dbus calls.
public static final int
INPUT_CONNECT_FAILED_ALREADY_CONNECTED
public static final int
INPUT_CONNECT_FAILED_ATTEMPT_FAILED
public static final int
INPUT_OPERATION_GENERIC_FAILURE
public static final int
INPUT_OPERATION_SUCCESS
public static final int
PROTOCOL_REPORT_MODE
public static final int
PROTOCOL_BOOT_MODE
public static final int
PROTOCOL_UNSUPPORTED_MODE
public static final byte
REPORT_TYPE_INPUT
public static final byte
REPORT_TYPE_OUTPUT
public static final byte
REPORT_TYPE_FEATURE
public static final int
VIRTUAL_UNPLUG_STATUS_SUCCESS
public static final int
VIRTUAL_UNPLUG_STATUS_FAIL
public static final String
EXTRA_PROTOCOL_MODE
public static final String
EXTRA_REPORT_TYPE
public static final String
EXTRA_REPORT_ID
public static final String
EXTRA_REPORT_BUFFER_SIZE
public static final String
EXTRA_REPORT
public static final String
EXTRA_STATUS
public static final String
EXTRA_VIRTUAL_UNPLUG_STATUS
private android.content.Context
mContext
private ServiceListener
mServiceListener
private BluetoothAdapter
mAdapter
private IBluetoothInputDevice
mService
private final IBluetoothStateChangeCallback
mBluetoothStateChangeCallback
private final android.content.ServiceConnection
mConnection
Constructors Summary
BluetoothInputDevice(android.content.Context context, ServiceListener l)
Create a BluetoothInputDevice proxy object for interacting with the local Bluetooth Service which handles the InputDevice profile


                          
    /*package*/     
        mContext = context;
        mServiceListener = l;
        mAdapter = BluetoothAdapter.getDefaultAdapter();

        IBluetoothManager mgr = mAdapter.getBluetoothManager();
        if (mgr != null) {
            try {
                mgr.registerStateChangeCallback(mBluetoothStateChangeCallback);
            } catch (RemoteException e) {
                Log.e(TAG,"",e);
            }
        }

        doBind();
    
Methods Summary
voidclose()

        if (VDBG) log("close()");
        IBluetoothManager mgr = mAdapter.getBluetoothManager();
        if (mgr != null) {
            try {
                mgr.unregisterStateChangeCallback(mBluetoothStateChangeCallback);
            } catch (Exception e) {
                Log.e(TAG,"",e);
            }
        }

        synchronized (mConnection) {
            if (mService != null) {
                try {
                    mService = null;
                    mContext.unbindService(mConnection);
                } catch (Exception re) {
                    Log.e(TAG,"",re);
                }
           }
        }
        mServiceListener = null;
    
public booleanconnect(BluetoothDevice device)
Initiate connection to a profile of the remote bluetooth device.

The system supports connection to multiple input devices.

This API returns false in scenarios like the profile on the device is already connected or Bluetooth is not turned on. When this API returns true, it is guaranteed that connection state intent for the profile will be broadcasted with the state. Users can get the connection state of the profile from this intent.

Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.

param
device Remote Bluetooth Device
return
false on immediate error, true otherwise
hide

        if (DBG) log("connect(" + device + ")");
        if (mService != null && isEnabled() && isValidDevice(device)) {
            try {
                return mService.connect(device);
            } catch (RemoteException e) {
                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
                return false;
            }
        }
        if (mService == null) Log.w(TAG, "Proxy not attached to service");
        return false;
    
public booleandisconnect(BluetoothDevice device)
Initiate disconnection from a profile

This API will return false in scenarios like the profile on the Bluetooth device is not in connected state etc. When this API returns, true, it is guaranteed that the connection state change intent will be broadcasted with the state. Users can get the disconnection state of the profile from this intent.

If the disconnection is initiated by a remote device, the state will transition from {@link #STATE_CONNECTED} to {@link #STATE_DISCONNECTED}. If the disconnect is initiated by the host (local) device the state will transition from {@link #STATE_CONNECTED} to state {@link #STATE_DISCONNECTING} to state {@link #STATE_DISCONNECTED}. The transition to {@link #STATE_DISCONNECTING} can be used to distinguish between the two scenarios.

Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.

param
device Remote Bluetooth Device
return
false on immediate error, true otherwise
hide

        if (DBG) log("disconnect(" + device + ")");
        if (mService != null && isEnabled() && isValidDevice(device)) {
            try {
                return mService.disconnect(device);
            } catch (RemoteException e) {
                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
                return false;
            }
        }
        if (mService == null) Log.w(TAG, "Proxy not attached to service");
        return false;
    
booleandoBind()

        Intent intent = new Intent(IBluetoothInputDevice.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 Service with " + intent);
            return false;
        }
        return true;
    
public java.util.ListgetConnectedDevices()
{@inheritDoc}

        if (VDBG) log("getConnectedDevices()");
        if (mService != null && isEnabled()) {
            try {
                return mService.getConnectedDevices();
            } catch (RemoteException e) {
                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
                return new ArrayList<BluetoothDevice>();
            }
        }
        if (mService == null) Log.w(TAG, "Proxy not attached to service");
        return new ArrayList<BluetoothDevice>();
    
public intgetConnectionState(BluetoothDevice device)
{@inheritDoc}

        if (VDBG) log("getState(" + device + ")");
        if (mService != null && isEnabled() && isValidDevice(device)) {
            try {
                return mService.getConnectionState(device);
            } catch (RemoteException e) {
                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
                return BluetoothProfile.STATE_DISCONNECTED;
            }
        }
        if (mService == null) Log.w(TAG, "Proxy not attached to service");
        return BluetoothProfile.STATE_DISCONNECTED;
    
public java.util.ListgetDevicesMatchingConnectionStates(int[] states)
{@inheritDoc}

        if (VDBG) log("getDevicesMatchingStates()");
        if (mService != null && isEnabled()) {
            try {
                return mService.getDevicesMatchingConnectionStates(states);
            } catch (RemoteException e) {
                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
                return new ArrayList<BluetoothDevice>();
            }
        }
        if (mService == null) Log.w(TAG, "Proxy not attached to service");
        return new ArrayList<BluetoothDevice>();
    
public intgetPriority(BluetoothDevice device)
Get the priority of the profile.

The priority can be any of: {@link #PRIORITY_AUTO_CONNECT}, {@link #PRIORITY_OFF}, {@link #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED}

Requires {@link android.Manifest.permission#BLUETOOTH} permission.

param
device Bluetooth device
return
priority of the device
hide

        if (VDBG) log("getPriority(" + device + ")");
        if (mService != null && isEnabled() && isValidDevice(device)) {
            try {
                return mService.getPriority(device);
            } catch (RemoteException e) {
                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
                return BluetoothProfile.PRIORITY_OFF;
            }
        }
        if (mService == null) Log.w(TAG, "Proxy not attached to service");
        return BluetoothProfile.PRIORITY_OFF;
    
public booleangetProtocolMode(BluetoothDevice device)
Send Get_Protocol_Mode command to the connected HID input device.

Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.

param
device Remote Bluetooth Device
return
false on immediate error, true otherwise
hide

        if (VDBG) log("getProtocolMode(" + device + ")");
        if (mService != null && isEnabled() && isValidDevice(device)) {
            try {
                return mService.getProtocolMode(device);
            } catch (RemoteException e) {
                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
                return false;
            }
        }
        if (mService == null) Log.w(TAG, "Proxy not attached to service");
            return false;
    
public booleangetReport(BluetoothDevice device, byte reportType, byte reportId, int bufferSize)
Send Get_Report command to the connected HID input device.

Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.

param
device Remote Bluetooth Device
param
reportType Report type
param
reportId Report ID
param
bufferSize Report receiving buffer size
return
false on immediate error, true otherwise
hide

        if (VDBG) log("getReport(" + device + "), reportType=" + reportType + " reportId=" + reportId + "bufferSize=" + bufferSize);
        if (mService != null && isEnabled() && isValidDevice(device)) {
            try {
                return mService.getReport(device, reportType, reportId, bufferSize);
            } catch (RemoteException e) {
                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
                return false;
            }
        }
        if (mService == null) Log.w(TAG, "Proxy not attached to service");
        return false;
    
private booleanisEnabled()


       
       if (mAdapter.getState() == BluetoothAdapter.STATE_ON) return true;
       return false;
    
private booleanisValidDevice(BluetoothDevice device)

       if (device == null) return false;

       if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true;
       return false;
    
private static voidlog(java.lang.String msg)

      Log.d(TAG, msg);
    
public booleansendData(BluetoothDevice device, java.lang.String report)
Send Send_Data command to the connected HID input device.

Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.

param
device Remote Bluetooth Device
param
report Report to send
return
false on immediate error, true otherwise
hide

        if (DBG) log("sendData(" + device + "), report=" + report);
        if (mService != null && isEnabled() && isValidDevice(device)) {
            try {
                return mService.sendData(device, report);
            } catch (RemoteException e) {
                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
                return false;
            }
        }
        if (mService == null) Log.w(TAG, "Proxy not attached to service");
        return false;
    
public booleansetPriority(BluetoothDevice device, int priority)
Set priority of the profile

The device should already be paired. Priority can be one of {@link #PRIORITY_ON} or {@link #PRIORITY_OFF},

Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.

param
device Paired bluetooth device
param
priority
return
true if priority is set, false on error
hide

        if (DBG) log("setPriority(" + device + ", " + priority + ")");
        if (mService != null && isEnabled() && isValidDevice(device)) {
            if (priority != BluetoothProfile.PRIORITY_OFF &&
                priority != BluetoothProfile.PRIORITY_ON) {
              return false;
            }
            try {
                return mService.setPriority(device, priority);
            } catch (RemoteException e) {
                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
                return false;
            }
        }
        if (mService == null) Log.w(TAG, "Proxy not attached to service");
        return false;
    
public booleansetProtocolMode(BluetoothDevice device, int protocolMode)
Send Set_Protocol_Mode command to the connected HID input device.

Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.

param
device Remote Bluetooth Device
return
false on immediate error, true otherwise
hide

        if (DBG) log("setProtocolMode(" + device + ")");
        if (mService != null && isEnabled() && isValidDevice(device)) {
            try {
                return mService.setProtocolMode(device, protocolMode);
            } catch (RemoteException e) {
                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
                return false;
            }
        }
        if (mService == null) Log.w(TAG, "Proxy not attached to service");
        return false;
    
public booleansetReport(BluetoothDevice device, byte reportType, java.lang.String report)
Send Set_Report command to the connected HID input device.

Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.

param
device Remote Bluetooth Device
param
reportType Report type
param
report Report receiving buffer size
return
false on immediate error, true otherwise
hide

        if (VDBG) log("setReport(" + device + "), reportType=" + reportType + " report=" + report);
        if (mService != null && isEnabled() && isValidDevice(device)) {
            try {
                return mService.setReport(device, reportType, report);
            } catch (RemoteException e) {
                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
                return false;
            }
        }
        if (mService == null) Log.w(TAG, "Proxy not attached to service");
        return false;
    
public booleanvirtualUnplug(BluetoothDevice device)
Initiate virtual unplug for a HID input device.

Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.

param
device Remote Bluetooth Device
return
false on immediate error, true otherwise
hide

        if (DBG) log("virtualUnplug(" + device + ")");
        if (mService != null && isEnabled() && isValidDevice(device)) {
            try {
                return mService.virtualUnplug(device);
            } catch (RemoteException e) {
                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
                return false;
            }
        }

        if (mService == null) Log.w(TAG, "Proxy not attached to service");
        return false;