import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
+import java.util.HashMap;
import java.util.List;
/**
private String mName;
private short mRssi;
private BluetoothClass mBtClass;
+ private HashMap<LocalBluetoothProfile, Integer> mProfileConnectionState;
private final List<LocalBluetoothProfile> mProfiles =
new ArrayList<LocalBluetoothProfile>();
" newProfileState " + newProfileState);
}
+ mProfileConnectionState.put(profile, newProfileState);
if (newProfileState == BluetoothProfile.STATE_CONNECTED) {
if (!mProfiles.contains(profile)) {
mProfiles.add(profile);
mLocalAdapter = adapter;
mProfileManager = profileManager;
mDevice = device;
+ mProfileConnectionState = new HashMap<LocalBluetoothProfile, Integer>();
fillData();
}
}
}
+ int getProfileConnectionState(LocalBluetoothProfile profile) {
+ if (mProfileConnectionState == null ||
+ mProfileConnectionState.get(profile) == null) {
+ // If cache is empty make the binder call to get the state
+ int state = profile.getConnectionStatus(mDevice);
+ mProfileConnectionState.put(profile, state);
+ }
+ return mProfileConnectionState.get(profile);
+ }
+
// TODO: do any of these need to run async on a background thread?
private void fillData() {
fetchName();
*/
boolean isConnected() {
for (LocalBluetoothProfile profile : mProfiles) {
- int status = profile.getConnectionStatus(mDevice);
+ int status = getProfileConnectionState(profile);
if (status == BluetoothProfile.STATE_CONNECTED) {
return true;
}
}
boolean isConnectedProfile(LocalBluetoothProfile profile) {
- int status = profile.getConnectionStatus(mDevice);
+ int status = getProfileConnectionState(profile);
return status == BluetoothProfile.STATE_CONNECTED;
}
boolean isBusy() {
for (LocalBluetoothProfile profile : mProfiles) {
- int status = profile.getConnectionStatus(mDevice);
+ int status = getProfileConnectionState(profile);
if (status == BluetoothProfile.STATE_CONNECTING
|| status == BluetoothProfile.STATE_DISCONNECTING) {
return true;