BluetoothHealthpublic final class BluetoothHealth extends Object implements BluetoothProfilePublic API for Bluetooth Health Profile.
BluetoothHealth is a proxy object for controlling the Bluetooth
Service via IPC.
How to connect to a health device which is acting in the source role.
Use {@link BluetoothAdapter#getProfileProxy} to get
the BluetoothHealth proxy object.
Create an {@link BluetoothHealth} callback and call
{@link #registerSinkAppConfiguration} to register an application
configuration
Pair with the remote device. This currently needs to be done manually
from Bluetooth Settings
Connect to a health device using {@link #connectChannelToSource}. Some
devices will connect the channel automatically. The {@link BluetoothHealth}
callback will inform the application of channel state change.
Use the file descriptor provided with a connected channel to read and
write data to the health channel.
The received data needs to be interpreted using a health manager which
implements the IEEE 11073-xxxxx specifications.
When done, close the health channel by calling {@link #disconnectChannel}
and unregister the application configuration calling
{@link #unregisterAppConfiguration} |
Fields Summary |
---|
private static final String | TAG | private static final boolean | DBG | private static final boolean | VDBG | public static final int | SOURCE_ROLEHealth Profile Source Role - the health device. | public static final int | SINK_ROLEHealth Profile Sink Role the device talking to the health device. | public static final int | CHANNEL_TYPE_RELIABLEHealth Profile - Channel Type used - Reliable | public static final int | CHANNEL_TYPE_STREAMINGHealth Profile - Channel Type used - Streaming | public static final int | CHANNEL_TYPE_ANY | public static final int | HEALTH_OPERATION_SUCCESS | public static final int | HEALTH_OPERATION_ERROR | public static final int | HEALTH_OPERATION_INVALID_ARGS | public static final int | HEALTH_OPERATION_GENERIC_FAILURE | public static final int | HEALTH_OPERATION_NOT_FOUND | public static final int | HEALTH_OPERATION_NOT_ALLOWED | private final IBluetoothStateChangeCallback | mBluetoothStateChangeCallback | public static final int | STATE_CHANNEL_DISCONNECTEDHealth Channel Connection State - Disconnected | public static final int | STATE_CHANNEL_CONNECTINGHealth Channel Connection State - Connecting | public static final int | STATE_CHANNEL_CONNECTEDHealth Channel Connection State - Connected | public static final int | STATE_CHANNEL_DISCONNECTINGHealth Channel Connection State - Disconnecting | public static final int | APP_CONFIG_REGISTRATION_SUCCESSHealth App Configuration registration success | public static final int | APP_CONFIG_REGISTRATION_FAILUREHealth App Configuration registration failure | public static final int | APP_CONFIG_UNREGISTRATION_SUCCESSHealth App Configuration un-registration success | public static final int | APP_CONFIG_UNREGISTRATION_FAILUREHealth App Configuration un-registration failure | private android.content.Context | mContext | private ServiceListener | mServiceListener | private IBluetoothHealth | mService | BluetoothAdapter | mAdapter | private final android.content.ServiceConnection | mConnection |
Constructors Summary |
---|
BluetoothHealth(android.content.Context context, ServiceListener l)Create a BluetoothHealth proxy object.
/*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 |
---|
private boolean | checkAppParam(java.lang.String name, int role, int channelType, BluetoothHealthCallback callback)
if (name == null || (role != SOURCE_ROLE && role != SINK_ROLE) ||
(channelType != CHANNEL_TYPE_RELIABLE &&
channelType != CHANNEL_TYPE_STREAMING &&
channelType != CHANNEL_TYPE_ANY) || callback == null) {
return false;
}
if (role == SOURCE_ROLE && channelType == CHANNEL_TYPE_ANY) return false;
return true;
| void | close()
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 boolean | connectChannelToSink(BluetoothDevice device, BluetoothHealthAppConfiguration config, int channelType)Connect to a health device which has the {@link #SINK_ROLE}.
This is an asynchronous call. If this function returns true, the callback
associated with the application configuration will be called.
Requires {@link android.Manifest.permission#BLUETOOTH} permission.
if (mService != null && isEnabled() && isValidDevice(device) &&
config != null) {
try {
return mService.connectChannelToSink(device, config, channelType);
} 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;
| public boolean | connectChannelToSource(BluetoothDevice device, BluetoothHealthAppConfiguration config)Connect to a health device which has the {@link #SOURCE_ROLE}.
This is an asynchronous call. If this function returns true, the callback
associated with the application configuration will be called.
Requires {@link android.Manifest.permission#BLUETOOTH} permission.
if (mService != null && isEnabled() && isValidDevice(device) &&
config != null) {
try {
return mService.connectChannelToSource(device, config);
} 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;
| public boolean | disconnectChannel(BluetoothDevice device, BluetoothHealthAppConfiguration config, int channelId)Disconnect a connected health channel.
This is an asynchronous call. If this function returns true, the callback
associated with the application configuration will be called.
Requires {@link android.Manifest.permission#BLUETOOTH} permission.
if (mService != null && isEnabled() && isValidDevice(device) &&
config != null) {
try {
return mService.disconnectChannel(device, config, channelId);
} 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;
| boolean | doBind()
Intent intent = new Intent(IBluetoothHealth.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 Health Service with " + intent);
return false;
}
return true;
| public java.util.List | getConnectedDevices()Get connected devices for the health profile.
Return the set of devices which are in state {@link #STATE_CONNECTED}
Requires {@link android.Manifest.permission#BLUETOOTH} permission.
This is not specific to any application configuration but represents the connection
state of the local Bluetooth adapter for this profile. This can be used
by applications like status bar which would just like to know the state of the
local adapter.
if (mService != null && isEnabled()) {
try {
return mService.getConnectedHealthDevices();
} 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 int | getConnectionState(BluetoothDevice device)Get the current connection state of the profile.
Requires {@link android.Manifest.permission#BLUETOOTH} permission.
This is not specific to any application configuration but represents the connection
state of the local Bluetooth adapter with the remote device. This can be used
by applications like status bar which would just like to know the state of the
local adapter.
if (mService != null && isEnabled() && isValidDevice(device)) {
try {
return mService.getHealthDeviceConnectionState(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 STATE_DISCONNECTED;
| public java.util.List | getDevicesMatchingConnectionStates(int[] states)Get a list of devices that match any of the given connection
states.
If none of the devices match any of the given states,
an empty list will be returned.
Requires {@link android.Manifest.permission#BLUETOOTH} permission.
This is not specific to any application configuration but represents the connection
state of the local Bluetooth adapter for this profile. This can be used
by applications like status bar which would just like to know the state of the
local adapter.
if (mService != null && isEnabled()) {
try {
return mService.getHealthDevicesMatchingConnectionStates(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 android.os.ParcelFileDescriptor | getMainChannelFd(BluetoothDevice device, BluetoothHealthAppConfiguration config)Get the file descriptor of the main channel associated with the remote device
and application configuration.
Requires {@link android.Manifest.permission#BLUETOOTH} permission.
Its the responsibility of the caller to close the ParcelFileDescriptor
when done.
if (mService != null && isEnabled() && isValidDevice(device) &&
config != null) {
try {
return mService.getMainChannelFd(device, config);
} 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 boolean | isEnabled()
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter != null && adapter.getState() == BluetoothAdapter.STATE_ON) return true;
log("Bluetooth is Not enabled");
return false;
| private boolean | isValidDevice(BluetoothDevice device)
if (device == null) return false;
if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true;
return false;
| private static void | log(java.lang.String msg)
Log.d(TAG, msg);
| public boolean | registerAppConfiguration(java.lang.String name, int dataType, int role, int channelType, BluetoothHealthCallback callback)Register an application configuration that acts as a Health SINK or in a Health
SOURCE role.This is an asynchronous call and so
the callback is used to notify success or failure if the function returns true.
Requires {@link android.Manifest.permission#BLUETOOTH} permission.
boolean result = false;
if (!isEnabled() || !checkAppParam(name, role, channelType, callback)) return result;
if (VDBG) log("registerApplication(" + name + ":" + dataType + ")");
BluetoothHealthCallbackWrapper wrapper = new BluetoothHealthCallbackWrapper(callback);
BluetoothHealthAppConfiguration config =
new BluetoothHealthAppConfiguration(name, dataType, role, channelType);
if (mService != null) {
try {
result = mService.registerAppConfiguration(config, wrapper);
} 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 result;
| public boolean | registerSinkAppConfiguration(java.lang.String name, int dataType, BluetoothHealthCallback callback)Register an application configuration that acts as a Health SINK.
This is the configuration that will be used to communicate with health devices
which will act as the {@link #SOURCE_ROLE}. This is an asynchronous call and so
the callback is used to notify success or failure if the function returns true.
Requires {@link android.Manifest.permission#BLUETOOTH} permission.
if (!isEnabled() || name == null) return false;
if (VDBG) log("registerSinkApplication(" + name + ":" + dataType + ")");
return registerAppConfiguration(name, dataType, SINK_ROLE,
CHANNEL_TYPE_ANY, callback);
| public boolean | unregisterAppConfiguration(BluetoothHealthAppConfiguration config)Unregister an application configuration that has been registered using
{@link #registerSinkAppConfiguration}
Requires {@link android.Manifest.permission#BLUETOOTH} permission.
boolean result = false;
if (mService != null && isEnabled() && config != null) {
try {
result = mService.unregisterAppConfiguration(config);
} 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 result;
|
|