FileDocCategorySizeDatePackage
BluetoothHealth.javaAPI DocAndroid 5.1 API23256Thu Mar 12 22:22:10 GMT 2015android.bluetooth

BluetoothHealth

public final class BluetoothHealth extends Object implements BluetoothProfile
Public 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_ROLE
    Health Profile Source Role - the health device.
    public static final int
    SINK_ROLE
    Health Profile Sink Role the device talking to the health device.
    public static final int
    CHANNEL_TYPE_RELIABLE
    Health Profile - Channel Type used - Reliable
    public static final int
    CHANNEL_TYPE_STREAMING
    Health 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_DISCONNECTED
    Health Channel Connection State - Disconnected
    public static final int
    STATE_CHANNEL_CONNECTING
    Health Channel Connection State - Connecting
    public static final int
    STATE_CHANNEL_CONNECTED
    Health Channel Connection State - Connected
    public static final int
    STATE_CHANNEL_DISCONNECTING
    Health Channel Connection State - Disconnecting
    public static final int
    APP_CONFIG_REGISTRATION_SUCCESS
    Health App Configuration registration success
    public static final int
    APP_CONFIG_REGISTRATION_FAILURE
    Health App Configuration registration failure
    public static final int
    APP_CONFIG_UNREGISTRATION_SUCCESS
    Health App Configuration un-registration success
    public static final int
    APP_CONFIG_UNREGISTRATION_FAILURE
    Health 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 booleancheckAppParam(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;
        
    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 booleanconnectChannelToSink(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.

    param
    device The remote Bluetooth device.
    param
    config The application configuration which has been registered using {@link #registerSinkAppConfiguration(String, int, BluetoothHealthCallback) }
    return
    If true, the callback associated with the application config will be called.
    hide

            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 booleanconnectChannelToSource(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.

    param
    device The remote Bluetooth device.
    param
    config The application configuration which has been registered using {@link #registerSinkAppConfiguration(String, int, BluetoothHealthCallback) }
    return
    If true, the callback associated with the application config will be called.

            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 booleandisconnectChannel(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.

    param
    device The remote Bluetooth device.
    param
    config The application configuration which has been registered using {@link #registerSinkAppConfiguration(String, int, BluetoothHealthCallback) }
    param
    channelId The channel id associated with the channel
    return
    If true, the callback associated with the application config will be called.

            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;
        
    booleandoBind()

            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.ListgetConnectedDevices()
    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.

    return
    List of devices. The list will be empty on error.

            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 intgetConnectionState(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.

    param
    device Remote bluetooth device.
    return
    State of the profile connection. One of {@link #STATE_CONNECTED}, {@link #STATE_CONNECTING}, {@link #STATE_DISCONNECTED}, {@link #STATE_DISCONNECTING}

            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.ListgetDevicesMatchingConnectionStates(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.

    param
    states Array of states. States can be one of {@link #STATE_CONNECTED}, {@link #STATE_CONNECTING}, {@link #STATE_DISCONNECTED}, {@link #STATE_DISCONNECTING},
    return
    List of devices. The list will be empty on error.

            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.ParcelFileDescriptorgetMainChannelFd(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.

    param
    device The remote Bluetooth health device
    param
    config The application configuration
    return
    null on failure, ParcelFileDescriptor on success.

            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 booleanisEnabled()

    
           
            BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    
            if (adapter != null && adapter.getState() == BluetoothAdapter.STATE_ON) return true;
            log("Bluetooth is Not enabled");
            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 booleanregisterAppConfiguration(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.

    param
    name The friendly name associated with the application or configuration.
    param
    dataType The dataType of the Source role of Health Profile.
    param
    channelType The channel type. Will be one of {@link #CHANNEL_TYPE_RELIABLE} or {@link #CHANNEL_TYPE_STREAMING}
    param
    callback - A callback to indicate success or failure.
    return
    If true, callback will be called.
    hide

            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 booleanregisterSinkAppConfiguration(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.

    param
    name The friendly name associated with the application or configuration.
    param
    dataType The dataType of the Source role of Health Profile to which the sink wants to connect to.
    param
    callback A callback to indicate success or failure of the registration and all operations done on this application configuration.
    return
    If true, callback will be called.

    
    
                                                                                                                                                             
             
                  
            if (!isEnabled() || name == null) return false;
    
            if (VDBG) log("registerSinkApplication(" + name + ":" + dataType + ")");
            return registerAppConfiguration(name, dataType, SINK_ROLE,
                    CHANNEL_TYPE_ANY, callback);
        
    public booleanunregisterAppConfiguration(BluetoothHealthAppConfiguration config)
    Unregister an application configuration that has been registered using {@link #registerSinkAppConfiguration}

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

    param
    config The health app configuration
    return
    Success or failure.

            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;