/* * Copyright (C) 2014 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.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Binder; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.util.Log; import java.util.ArrayList; import java.util.List; /** * Public API to control Hands Free Profile (HFP role only). *
* This class defines methods that shall be used by application to manage profile * connection, calls states and calls actions. *
* * @hide * */ public final class BluetoothHeadsetClient implements BluetoothProfile { private static final String TAG = "BluetoothHeadsetClient"; private static final boolean DBG = true; private static final boolean VDBG = false; /** * Intent sent whenever connection to remote changes. * *
It includes two extras:
* BluetoothProfile.EXTRA_PREVIOUS_STATE
* and BluetoothProfile.EXTRA_STATE
, which
* are mandatory.
*
There are also non mandatory feature extras:
* {@link #EXTRA_AG_FEATURE_3WAY_CALLING},
* {@link #EXTRA_AG_FEATURE_VOICE_RECOGNITION},
* {@link #EXTRA_AG_FEATURE_ATTACH_NUMBER_TO_VT},
* {@link #EXTRA_AG_FEATURE_REJECT_CALL},
* {@link #EXTRA_AG_FEATURE_ECC},
* {@link #EXTRA_AG_FEATURE_RESPONSE_AND_HOLD},
* {@link #EXTRA_AG_FEATURE_ACCEPT_HELD_OR_WAITING_CALL},
* {@link #EXTRA_AG_FEATURE_RELEASE_HELD_OR_WAITING_CALL},
* {@link #EXTRA_AG_FEATURE_RELEASE_AND_ACCEPT},
* {@link #EXTRA_AG_FEATURE_MERGE},
* {@link #EXTRA_AG_FEATURE_MERGE_AND_DETACH},
* sent as boolean values only when EXTRA_STATE
* is set to STATE_CONNECTED
.
Note that features supported by AG are being sent as
* booleans with value true
,
* and not supported ones are not being sent at all.
It includes two mandatory extras: * {@link BluetoothProfile#EXTRA_STATE}, * {@link BluetoothProfile#EXTRA_PREVIOUS_STATE}, * with possible values: * {@link #STATE_AUDIO_CONNECTING}, * {@link #STATE_AUDIO_CONNECTED}, * {@link #STATE_AUDIO_DISCONNECTED}
*When EXTRA_STATE
is set
* to STATE_AUDIO_CONNECTED,
* it also includes {@link #EXTRA_AUDIO_WBS}
* indicating wide band speech support.
It can contain one or more of the following extras: * {@link #EXTRA_NETWORK_STATUS}, * {@link #EXTRA_NETWORK_SIGNAL_STRENGTH}, * {@link #EXTRA_NETWORK_ROAMING}, * {@link #EXTRA_BATTERY_LEVEL}, * {@link #EXTRA_OPERATOR_NAME}, * {@link #EXTRA_VOICE_RECOGNITION}, * {@link #EXTRA_IN_BAND_RING}
*/ public static final String ACTION_AG_EVENT = "android.bluetooth.headsetclient.profile.action.AG_EVENT"; /** * Intent sent whenever state of a call changes. * *It includes: * {@link #EXTRA_CALL}, * with value of {@link BluetoothHeadsetClientCall} instance, * representing actual call state.
*/ public static final String ACTION_CALL_CHANGED = "android.bluetooth.headsetclient.profile.action.AG_CALL_CHANGED"; /** * Intent that notifies about the result of the last issued action. * Please note that not every action results in explicit action result code being sent. * Instead other notifications about new Audio Gateway state might be sent, * likeACTION_AG_EVENT
with EXTRA_VOICE_RECOGNITION
value
* when for example user started voice recognition from HF unit.
*/
public static final String ACTION_RESULT =
"android.bluetooth.headsetclient.profile.action.RESULT";
/**
* Intent that notifies about the number attached to the last voice tag
* recorded on AG.
*
* It contains:
* {@link #EXTRA_NUMBER},
* with a String
value representing phone number.
Possible values: true
,
* false
.
Value: 0 - network unavailable, * 1 - network available
*/ public static final String EXTRA_NETWORK_STATUS = "android.bluetooth.headsetclient.extra.NETWORK_STATUS"; /** * Extra for AG_EVENT intent indicates network signal strength. *Value: Integer
representing signal strength.
Value: 0 - no roaming * 1 - active roaming
*/ public static final String EXTRA_NETWORK_ROAMING = "android.bluetooth.headsetclient.extra.NETWORK_ROAMING"; /** * Extra for AG_EVENT intent indicates the battery level. *Value: Integer
representing signal strength.
Value: String
representing operator name.
Value: * 0 - voice recognition stopped, * 1 - voice recognition started.
*/ public static final String EXTRA_VOICE_RECOGNITION = "android.bluetooth.headsetclient.extra.VOICE_RECOGNITION"; /** * Extra for AG_EVENT intent indicates in band ring state. *Value: * 0 - in band ring tone not supported, or * 1 - in band ring tone supported.
*/ public static final String EXTRA_IN_BAND_RING = "android.bluetooth.headsetclient.extra.IN_BAND_RING"; /** * Extra for AG_EVENT intent indicates subscriber info. *Value: String
containing subscriber information.
Value: String
representing phone number
* corresponding to last voice tag recorded on AG
Possible results: * {@link #ACTION_RESULT_OK}, * {@link #ACTION_RESULT_ERROR}, * {@link #ACTION_RESULT_ERROR_NO_CARRIER}, * {@link #ACTION_RESULT_ERROR_BUSY}, * {@link #ACTION_RESULT_ERROR_NO_ANSWER}, * {@link #ACTION_RESULT_ERROR_DELAYED}, * {@link #ACTION_RESULT_ERROR_BLACKLISTED}, * {@link #ACTION_RESULT_ERROR_CME}
*/ public static final String EXTRA_RESULT_CODE = "android.bluetooth.headsetclient.extra.RESULT_CODE"; /** * Extra for ACTION_RESULT intent that shows the extended result code of * last issued action. *Value: Integer
- error code.
true
if command has been issued successfully;
* false
otherwise;
* upon completion HFP sends {@link #ACTION_CONNECTION_STATE_CHANGED}
* intent.
*/
public boolean connect(BluetoothDevice device) {
if (DBG) log("connect(" + device + ")");
final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try {
return service.connect(device);
} catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable()));
return false;
}
}
if (service == null) Log.w(TAG, "Proxy not attached to service");
return false;
}
/**
* Disconnects remote device
*
* @param device a remote device we want disconnect
* @return true
if command has been issued successfully;
* false
otherwise;
* upon completion HFP sends {@link #ACTION_CONNECTION_STATE_CHANGED}
* intent.
*/
public boolean disconnect(BluetoothDevice device) {
if (DBG) log("disconnect(" + device + ")");
final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try {
return service.disconnect(device);
} catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable()));
return false;
}
}
if (service == null) Log.w(TAG, "Proxy not attached to service");
return false;
}
/**
* Return the list of connected remote devices
*
* @return list of connected devices; empty list if nothing is connected.
*/
@Override
public Liststates
; empty list if nothing matches the
* states
*/
@Override
public Listdevice
*
* @param device a remote device
* @return the state of connection of the device
*/
@Override
public int getConnectionState(BluetoothDevice device) {
if (VDBG) log("getConnectionState(" + device + ")");
final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try {
return service.getConnectionState(device);
} catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable()));
return BluetoothProfile.STATE_DISCONNECTED;
}
}
if (service == null) Log.w(TAG, "Proxy not attached to service");
return BluetoothProfile.STATE_DISCONNECTED;
}
/**
* Set priority of the profile
*
* The device should already be paired.
*/
public boolean setPriority(BluetoothDevice device, int priority) {
if (DBG) log("setPriority(" + device + ", " + priority + ")");
final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
if (priority != BluetoothProfile.PRIORITY_OFF
&& priority != BluetoothProfile.PRIORITY_ON) {
return false;
}
try {
return service.setPriority(device, priority);
} catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable()));
return false;
}
}
if (service == null) Log.w(TAG, "Proxy not attached to service");
return false;
}
/**
* Get the priority of the profile.
*/
public int getPriority(BluetoothDevice device) {
if (VDBG) log("getPriority(" + device + ")");
final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try {
return service.getPriority(device);
} catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable()));
return PRIORITY_OFF;
}
}
if (service == null) Log.w(TAG, "Proxy not attached to service");
return PRIORITY_OFF;
}
/**
* Starts voice recognition.
*
* @param device remote device
* @return true
if command has been issued successfully;
* false
otherwise;
* upon completion HFP sends {@link #ACTION_AG_EVENT}
* intent.
*
* Feature required for successful execution is being reported by: * {@link #EXTRA_AG_FEATURE_VOICE_RECOGNITION}. * This method invocation will fail silently when feature is not supported.
*/ public boolean startVoiceRecognition(BluetoothDevice device) { if (DBG) log("startVoiceRecognition()"); final IBluetoothHeadsetClient service = mService; if (service != null && isEnabled() && isValidDevice(device)) { try { return service.startVoiceRecognition(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); } } if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } /** * Stops voice recognition. * * @param device remote device * @returntrue
if command has been issued successfully;
* false
otherwise;
* upon completion HFP sends {@link #ACTION_AG_EVENT}
* intent.
*
* Feature required for successful execution is being reported by: * {@link #EXTRA_AG_FEATURE_VOICE_RECOGNITION}. * This method invocation will fail silently when feature is not supported.
*/ public boolean stopVoiceRecognition(BluetoothDevice device) { if (DBG) log("stopVoiceRecognition()"); final IBluetoothHeadsetClient service = mService; if (service != null && isEnabled() && isValidDevice(device)) { try { return service.stopVoiceRecognition(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); } } if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } /** * Returns list of all calls in any state. * * @param device remote device * @return list of calls; empty list if none call exists */ public Listtrue
if command has been issued successfully;
* false
otherwise;
* upon completion HFP sends {@link #ACTION_CALL_CHANGED}
* intent.
*/
public boolean acceptCall(BluetoothDevice device, int flag) {
if (DBG) log("acceptCall()");
final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try {
return service.acceptCall(device, flag);
} catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable()));
}
}
if (service == null) Log.w(TAG, "Proxy not attached to service");
return false;
}
/**
* Holds a call.
*
* @param device remote device
* @return true
if command has been issued successfully;
* false
otherwise;
* upon completion HFP sends {@link #ACTION_CALL_CHANGED}
* intent.
*/
public boolean holdCall(BluetoothDevice device) {
if (DBG) log("holdCall()");
final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try {
return service.holdCall(device);
} catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable()));
}
}
if (service == null) Log.w(TAG, "Proxy not attached to service");
return false;
}
/**
* Rejects a call.
*
* @param device remote device
* @return true
if command has been issued successfully;
* false
otherwise;
* upon completion HFP sends {@link #ACTION_CALL_CHANGED}
* intent.
*
* Feature required for successful execution is being reported by: * {@link #EXTRA_AG_FEATURE_REJECT_CALL}. * This method invocation will fail silently when feature is not supported.
*/ public boolean rejectCall(BluetoothDevice device) { if (DBG) log("rejectCall()"); final IBluetoothHeadsetClient service = mService; if (service != null && isEnabled() && isValidDevice(device)) { try { return service.rejectCall(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); } } if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } /** * Terminates a specified call. * * Works only when Extended Call Control is supported by Audio Gateway. * * @param device remote device * @param call Handle of call obtained in {@link dial()} or obtained via * {@link #ACTION_CALL_CHANGED}. {@code call} may be null in which * case we will hangup all active calls. * @returntrue
if command has been issued successfully;
* false
otherwise;
* upon completion HFP sends {@link #ACTION_CALL_CHANGED}
* intent.
*
* Feature required for successful execution is being reported by: * {@link #EXTRA_AG_FEATURE_ECC}. * This method invocation will fail silently when feature is not supported.
*/ public boolean terminateCall(BluetoothDevice device, BluetoothHeadsetClientCall call) { if (DBG) log("terminateCall()"); final IBluetoothHeadsetClient service = mService; if (service != null && isEnabled() && isValidDevice(device)) { try { return service.terminateCall(device, call); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); } } if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } /** * Enters private mode with a specified call. * * Works only when Extended Call Control is supported by Audio Gateway. * * @param device remote device * @param index index of the call to connect in private mode * @returntrue
if command has been issued successfully;
* false
otherwise;
* upon completion HFP sends {@link #ACTION_CALL_CHANGED}
* intent.
*
* Feature required for successful execution is being reported by: * {@link #EXTRA_AG_FEATURE_ECC}. * This method invocation will fail silently when feature is not supported.
*/ public boolean enterPrivateMode(BluetoothDevice device, int index) { if (DBG) log("enterPrivateMode()"); final IBluetoothHeadsetClient service = mService; if (service != null && isEnabled() && isValidDevice(device)) { try { return service.enterPrivateMode(device, index); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); } } if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } /** * Performs explicit call transfer. * * That means connect other calls and disconnect. * * @param device remote device * @returntrue
if command has been issued successfully;
* false
otherwise;
* upon completion HFP sends {@link #ACTION_CALL_CHANGED}
* intent.
*
* Feature required for successful execution is being reported by: * {@link #EXTRA_AG_FEATURE_MERGE_AND_DETACH}. * This method invocation will fail silently when feature is not supported.
*/ public boolean explicitCallTransfer(BluetoothDevice device) { if (DBG) log("explicitCallTransfer()"); final IBluetoothHeadsetClient service = mService; if (service != null && isEnabled() && isValidDevice(device)) { try { return service.explicitCallTransfer(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); } } if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } /** * Places a call with specified number. * * @param device remote device * @param number valid phone number * @return{@link BluetoothHeadsetClientCall} call
if command has been
* issued successfully;
* {@link null}
otherwise;
* upon completion HFP sends {@link #ACTION_CALL_CHANGED}
* intent in case of success; {@link #ACTION_RESULT} is sent
* otherwise;
*/
public BluetoothHeadsetClientCall dial(BluetoothDevice device, String number) {
if (DBG) log("dial()");
final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try {
return service.dial(device, number);
} catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable()));
}
}
if (service == null) Log.w(TAG, "Proxy not attached to service");
return null;
}
/**
* Sends DTMF code.
*
* Possible code values : 0,1,2,3,4,5,6,7,8,9,A,B,C,D,*,#
*
* @param device remote device
* @param code ASCII code
* @return true
if command has been issued successfully;
* false
otherwise;
* upon completion HFP sends {@link #ACTION_RESULT} intent;
*/
public boolean sendDTMF(BluetoothDevice device, byte code) {
if (DBG) log("sendDTMF()");
final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try {
return service.sendDTMF(device, code);
} catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable()));
}
}
if (service == null) Log.w(TAG, "Proxy not attached to service");
return false;
}
/**
* Get a number corresponding to last voice tag recorded on AG.
*
* @param device remote device
* @return true
if command has been issued successfully;
* false
otherwise;
* upon completion HFP sends {@link #ACTION_LAST_VTAG}
* or {@link #ACTION_RESULT} intent;
*
* Feature required for successful execution is being reported by: * {@link #EXTRA_AG_FEATURE_ATTACH_NUMBER_TO_VT}. * This method invocation will fail silently when feature is not supported.
*/ public boolean getLastVoiceTagNumber(BluetoothDevice device) { if (DBG) log("getLastVoiceTagNumber()"); final IBluetoothHeadsetClient service = mService; if (service != null && isEnabled() && isValidDevice(device)) { try { return service.getLastVoiceTagNumber(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); } } if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } /** * Returns current audio state of Audio Gateway. * * Note: This is an internal function and shouldn't be exposed */ public int getAudioState(BluetoothDevice device) { if (VDBG) log("getAudioState"); final IBluetoothHeadsetClient service = mService; if (service != null && isEnabled()) { try { return service.getAudioState(device); } catch (RemoteException e) { Log.e(TAG, e.toString()); } } else { Log.w(TAG, "Proxy not attached to service"); if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); } return BluetoothHeadsetClient.STATE_AUDIO_DISCONNECTED; } /** * Sets whether audio routing is allowed. * * @param device remote device * @param allowed if routing is allowed to the device * Note: This is an internal function and shouldn't be exposed */ public void setAudioRouteAllowed(BluetoothDevice device, boolean allowed) { if (VDBG) log("setAudioRouteAllowed"); final IBluetoothHeadsetClient service = mService; if (service != null && isEnabled()) { try { service.setAudioRouteAllowed(device, allowed); } catch (RemoteException e) { Log.e(TAG, e.toString()); } } else { Log.w(TAG, "Proxy not attached to service"); if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); } } /** * Returns whether audio routing is allowed. * @param device remote device * @return whether the command succeeded * Note: This is an internal function and shouldn't be exposed */ public boolean getAudioRouteAllowed(BluetoothDevice device) { if (VDBG) log("getAudioRouteAllowed"); final IBluetoothHeadsetClient service = mService; if (service != null && isEnabled()) { try { return service.getAudioRouteAllowed(device); } catch (RemoteException e) { Log.e(TAG, e.toString()); } } else { Log.w(TAG, "Proxy not attached to service"); if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); } return false; } /** * Initiates a connection of audio channel. * * It setup SCO channel with remote connected Handsfree AG device. * * @param device remote device * @returntrue
if command has been issued successfully;
* false
otherwise;
* upon completion HFP sends {@link #ACTION_AUDIO_STATE_CHANGED}
* intent;
*/
public boolean connectAudio(BluetoothDevice device) {
final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled()) {
try {
return service.connectAudio(device);
} catch (RemoteException e) {
Log.e(TAG, e.toString());
}
} else {
Log.w(TAG, "Proxy not attached to service");
if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
}
return false;
}
/**
* Disconnects audio channel.
*
* It tears down the SCO channel from remote AG device.
*
* @param device remote device
* @return true
if command has been issued successfully;
* false
otherwise;
* upon completion HFP sends {@link #ACTION_AUDIO_STATE_CHANGED}
* intent;
*/
public boolean disconnectAudio(BluetoothDevice device) {
final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled()) {
try {
return service.disconnectAudio(device);
} catch (RemoteException e) {
Log.e(TAG, e.toString());
}
} else {
Log.w(TAG, "Proxy not attached to service");
if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
}
return false;
}
/**
* Get Audio Gateway features
*
* @param device remote device
* @return bundle of AG features; null if no service or
* AG not connected
*/
public Bundle getCurrentAgFeatures(BluetoothDevice device) {
final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled()) {
try {
return service.getCurrentAgFeatures(device);
} catch (RemoteException e) {
Log.e(TAG, e.toString());
}
} else {
Log.w(TAG, "Proxy not attached to service");
if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
}
return null;
}
private final ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
if (DBG) Log.d(TAG, "Proxy object connected");
mService = IBluetoothHeadsetClient.Stub.asInterface(Binder.allowBlocking(service));
if (mServiceListener != null) {
mServiceListener.onServiceConnected(BluetoothProfile.HEADSET_CLIENT,
BluetoothHeadsetClient.this);
}
}
@Override
public void onServiceDisconnected(ComponentName className) {
if (DBG) Log.d(TAG, "Proxy object disconnected");
mService = null;
if (mServiceListener != null) {
mServiceListener.onServiceDisconnected(BluetoothProfile.HEADSET_CLIENT);
}
}
};
private boolean isEnabled() {
return mAdapter.getState() == BluetoothAdapter.STATE_ON;
}
private static boolean isValidDevice(BluetoothDevice device) {
return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress());
}
private static void log(String msg) {
Log.d(TAG, msg);
}
}