private final LocalBluetoothAdapter mLocalAdapter;
private final CachedBluetoothDeviceManager mDeviceManager;
private LocalBluetoothProfileManager mProfileManager;
- private final IntentFilter mIntentFilter;
+ private final IntentFilter mAdapterIntentFilter, mProfileIntentFilter;
private final Map<String, Handler> mHandlerMap;
+ private Context mContext;
private final Collection<BluetoothCallback> mCallbacks =
new ArrayList<BluetoothCallback>();
void addHandler(String action, Handler handler) {
mHandlerMap.put(action, handler);
- mIntentFilter.addAction(action);
+ mAdapterIntentFilter.addAction(action);
+ }
+
+ void addProfileHandler(String action, Handler handler) {
+ mHandlerMap.put(action, handler);
+ mProfileIntentFilter.addAction(action);
}
// Set profile manager after construction due to circular dependency
}
BluetoothEventManager(LocalBluetoothAdapter adapter,
- CachedBluetoothDeviceManager deviceManager) {
+ CachedBluetoothDeviceManager deviceManager, Context context) {
mLocalAdapter = adapter;
mDeviceManager = deviceManager;
- mIntentFilter = new IntentFilter();
+ mAdapterIntentFilter = new IntentFilter();
+ mProfileIntentFilter = new IntentFilter();
mHandlerMap = new HashMap<String, Handler>();
+ mContext = context;
// Bluetooth on/off broadcasts
addHandler(BluetoothAdapter.ACTION_STATE_CHANGED, new AdapterStateChangedHandler());
// Dock event broadcasts
addHandler(Intent.ACTION_DOCK_EVENT, new DockEventHandler());
+ mContext.registerReceiver(mBroadcastReceiver, mAdapterIntentFilter);
}
- /**
- * A Bluetooth-related activity is now in the foreground. Register to
- * start receiving Bluetooth events.
- * @param context a Context object for the current Activity
- */
- void resume(Context context) {
- if (mLocalAdapter.syncBluetoothState()) {
- // adapter state changed while we were paused: send callbacks
- int newState = mLocalAdapter.getState();
- synchronized (mCallbacks) {
- for (BluetoothCallback callback : mCallbacks) {
- callback.onBluetoothStateChanged(newState);
- }
- }
- }
- context.registerReceiver(mBroadcastReceiver, mIntentFilter);
- }
-
- void pause(Context context) {
- context.unregisterReceiver(mBroadcastReceiver);
+ void registerProfileIntentReceiver() {
+ mContext.registerReceiver(mBroadcastReceiver, mProfileIntentFilter);
}
/** Register to start receiving callbacks for Bluetooth events. */
mCachedDeviceManager = new CachedBluetoothDeviceManager();
mEventManager = new BluetoothEventManager(mLocalAdapter,
- mCachedDeviceManager);
+ mCachedDeviceManager, context);
mProfileManager = new LocalBluetoothProfileManager(context,
mLocalAdapter, mCachedDeviceManager, mEventManager);
}
if (context != null) {
Log.d(TAG, "setting foreground activity to non-null context");
mForegroundActivity = context;
- mEventManager.resume(context);
} else {
if (mForegroundActivity != null) {
Log.d(TAG, "setting foreground activity to null");
- mEventManager.pause(mForegroundActivity);
mForegroundActivity = null;
}
}
} else if (mOppProfile != null) {
Log.w(TAG, "Warning: OPP profile was previously added but the UUID is now missing.");
}
+ mEventManager.registerProfileIntentReceiver();
// There is no local SDP record for HID and Settings app doesn't control PBAP
}
private void addProfile(LocalBluetoothProfile profile,
String profileName, String stateChangedAction) {
- mEventManager.addHandler(stateChangedAction, new StateChangedHandler(profile));
+ mEventManager.addProfileHandler(stateChangedAction, new StateChangedHandler(profile));
mProfileNameMap.put(profileName, profile);
}