FileDocCategorySizeDatePackage
BluetoothMap.javaAPI DocAndroid 5.1 API14342Thu Mar 12 22:22:10 GMT 2015android.bluetooth

BluetoothMap

public final class BluetoothMap extends Object implements BluetoothProfile
This class provides the APIs to control the Bluetooth MAP Profile.
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
private IBluetoothMap
mService
private final android.content.Context
mContext
private ServiceListener
mServiceListener
private BluetoothAdapter
mAdapter
public static final int
STATE_ERROR
There was an error trying to obtain the state
public static final int
RESULT_FAILURE
public static final int
RESULT_SUCCESS
public static final int
RESULT_CANCELED
Connection canceled before completion.
private final IBluetoothStateChangeCallback
mBluetoothStateChangeCallback
private final android.content.ServiceConnection
mConnection
Constructors Summary
BluetoothMap(android.content.Context context, ServiceListener l)
Create a BluetoothMap proxy object.


              
    /*package*/     
        if (DBG) Log.d(TAG, "Create BluetoothMap proxy object");
        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
public synchronized voidclose()
Close the connection to the backing service. Other public functions of BluetoothMap will return default error results once close() has been called. Multiple invocations of close() are ok.

        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. Initiation of outgoing connections is not supported for MAP server.

        if (DBG) log("connect(" + device + ")" + "not supported for MAPS");
        return false;
    
public booleandisconnect(BluetoothDevice device)
Initiate disconnect.

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

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

        Intent intent = new Intent(IBluetoothMap.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 MAP Service with " + intent);
            return false;
        }
        return true;
    
public static booleandoesClassMatchSink(BluetoothClass btClass)
Check class bits for possible Map support. This is a simple heuristic that tries to guess if a device with the given class bits might support Map. It is not accurate for all devices. It tries to err on the side of false positives.

return
True if this device might support Map.

        // TODO optimize the rule
        switch (btClass.getDeviceClass()) {
        case BluetoothClass.Device.COMPUTER_DESKTOP:
        case BluetoothClass.Device.COMPUTER_LAPTOP:
        case BluetoothClass.Device.COMPUTER_SERVER:
        case BluetoothClass.Device.COMPUTER_UNCATEGORIZED:
            return true;
        default:
            return false;
        }
    
protected voidfinalize()

        try {
            close();
        } finally {
            super.finalize();
        }
    
public BluetoothDevicegetClient()
Get the currently connected remote Bluetooth device (PCE).

return
The remote Bluetooth device, or null if not in connected or connecting state, or if this proxy object is not connected to the Map service.

        if (VDBG) log("getClient()");
        if (mService != null) {
            try {
                return mService.getClient();
            } catch (RemoteException e) {Log.e(TAG, e.toString());}
        } else {
            Log.w(TAG, "Proxy not attached to service");
            if (DBG) log(Log.getStackTraceString(new Throwable()));
        }
        return null;
    
public java.util.ListgetConnectedDevices()
Get the list of connected devices. Currently at most one.

return
list of connected devices

        if (DBG) log("getConnectedDevices()");
        if (mService != null && isEnabled()) {
            try {
                return mService.getConnectedDevices();
            } catch (RemoteException e) {
                Log.e(TAG, 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 connection state of device

return
device connection state

        if (DBG) log("getConnectionState(" + device + ")");
        if (mService != null && isEnabled() &&
            isValidDevice(device)) {
            try {
                return mService.getConnectionState(device);
            } catch (RemoteException e) {
                Log.e(TAG, 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)
Get the list of devices matching specified states. Currently at most one.

return
list of matching devices

        if (DBG) log("getDevicesMatchingStates()");
        if (mService != null && isEnabled()) {
            try {
                return mService.getDevicesMatchingConnectionStates(states);
            } catch (RemoteException e) {
                Log.e(TAG, 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}

param
device Bluetooth device
return
priority of the device

        if (VDBG) log("getPriority(" + device + ")");
        if (mService != null && isEnabled() &&
            isValidDevice(device)) {
            try {
                return mService.getPriority(device);
            } catch (RemoteException e) {
                Log.e(TAG, Log.getStackTraceString(new Throwable()));
                return PRIORITY_OFF;
            }
        }
        if (mService == null) Log.w(TAG, "Proxy not attached to service");
        return PRIORITY_OFF;
    
public intgetState()
Get the current state of the BluetoothMap service.

return
One of the STATE_ return codes, or STATE_ERROR if this proxy object is currently not connected to the Map service.

        if (VDBG) log("getState()");
        if (mService != null) {
            try {
                return mService.getState();
            } catch (RemoteException e) {Log.e(TAG, e.toString());}
        } else {
            Log.w(TAG, "Proxy not attached to service");
            if (DBG) log(Log.getStackTraceString(new Throwable()));
        }
        return BluetoothMap.STATE_ERROR;
    
public booleanisConnected(BluetoothDevice device)
Returns true if the specified Bluetooth device is connected. Returns false if not connected, or if this proxy object is not currently connected to the Map service.

        if (VDBG) log("isConnected(" + device + ")");
        if (mService != null) {
            try {
                return mService.isConnected(device);
            } catch (RemoteException e) {Log.e(TAG, e.toString());}
        } else {
            Log.w(TAG, "Proxy not attached to service");
            if (DBG) log(Log.getStackTraceString(new Throwable()));
        }
        return false;
    
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 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},

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

        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, Log.getStackTraceString(new Throwable()));
                return false;
            }
        }
        if (mService == null) Log.w(TAG, "Proxy not attached to service");
        return false;