/* ContextMap here is needed to grab Apps and Connections */
ContextMap contextMap;
+ /* GattService is needed to add scan event protos to be dumped later */
+ GattService gattService;
+
class LastScan {
long duration;
long timestamp;
boolean opportunistic;
boolean background;
+ int results;
public LastScan(long timestamp, long duration,
boolean opportunistic, boolean background) {
this.timestamp = timestamp;
this.opportunistic = opportunistic;
this.background = background;
+ this.results = 0;
}
}
List<LastScan> lastScans = new ArrayList<LastScan>(NUM_SCAN_DURATIONS_KEPT + 1);
long startTime = 0;
long stopTime = 0;
+ int results = 0;
- public AppScanStats(String name, ContextMap map) {
+ public AppScanStats(String name, ContextMap map, GattService service) {
appName = name;
contextMap = map;
+ gattService = service;
+ }
+
+ synchronized void addResult() {
+ if (!lastScans.isEmpty())
+ lastScans.get(lastScans.size() - 1).results++;
+
+ results++;
}
synchronized void recordScanStart(ScanSettings settings) {
scanEvent.setScanTechnologyType(BluetoothProto.ScanEvent.SCAN_TECH_TYPE_LE);
scanEvent.setInitiator(appName);
scanEvent.setEventTimeMillis(System.currentTimeMillis());
- contextMap.addScanEvent(scanEvent);
+ gattService.addScanEvent(scanEvent);
}
synchronized void recordScanStop() {
scanEvent.setScanTechnologyType(BluetoothProto.ScanEvent.SCAN_TECH_TYPE_LE);
scanEvent.setInitiator(appName);
scanEvent.setEventTimeMillis(System.currentTimeMillis());
- contextMap.addScanEvent(scanEvent);
+ gattService.addScanEvent(scanEvent);
}
synchronized void dumpToString(StringBuilder sb) {
maxScan + " / " +
avgScan + " / " +
totalScanTime + "\n");
+ sb.append(" Total number of results : " +
+ results + "\n");
if (lastScans.size() != 0) {
int lastScansSize = scansStopped < NUM_SCAN_DURATIONS_KEPT ?
sb.append(" " + dateFormat.format(timestamp) + " - ");
sb.append(scan.duration + "ms ");
if (scan.opportunistic) sb.append("Opp ");
- if (scan.background) sb.append("Back");
+ if (scan.background) sb.append("Back ");
+ sb.append(scan.results + " results");
sb.append("\n");
}
}
*/
/*package*/ class ContextMap<T> {
private static final String TAG = GattServiceConfig.TAG_PREFIX + "ContextMap";
- static final int NUM_SCAN_EVENTS_KEPT = 20;
/**
* Connection class helps map connection IDs to device addresses.
/** The package name of the application */
String name;
+ /** Statistics for this app */
+ AppScanStats appScanStats;
+
/** Application callbacks */
T callback;
/**
* Creates a new app context.
*/
- App(UUID uuid, T callback, String name) {
+ App(UUID uuid, T callback, String name, AppScanStats appScanStats) {
this.uuid = uuid;
this.callback = callback;
this.name = name;
+ this.appScanStats = appScanStats;
}
/**
/** Internal map to keep track of logging information by app name */
HashMap<String, AppScanStats> mAppScanStats = new HashMap<String, AppScanStats>();
- /** Internal list of scan events to use with the proto */
- ArrayList<BluetoothProto.ScanEvent> mScanEvents =
- new ArrayList<BluetoothProto.ScanEvent>(NUM_SCAN_EVENTS_KEPT);
-
/** Internal list of connected devices **/
Set<Connection> mConnections = new HashSet<Connection>();
/**
* Add an entry to the application context list.
*/
- void add(UUID uuid, T callback, Context context) {
- String appName = context.getPackageManager().getNameForUid(
+ void add(UUID uuid, T callback, GattService service) {
+ String appName = service.getPackageManager().getNameForUid(
Binder.getCallingUid());
if (appName == null) {
// Assign an app name if one isn't found
appName = "Unknown App (UID: " + Binder.getCallingUid() + ")";
}
synchronized (mApps) {
- mApps.add(new App(uuid, callback, appName));
AppScanStats appScanStats = mAppScanStats.get(appName);
+ mApps.add(new App(uuid, callback, appName, appScanStats));
if (appScanStats == null) {
- appScanStats = new AppScanStats(appName, this);
+ appScanStats = new AppScanStats(appName, this, service);
mAppScanStats.put(appName, appScanStats);
}
appScanStats.isRegistered = true;
App entry = i.next();
if (entry.uuid.equals(uuid)) {
entry.unlinkToDeath();
- mAppScanStats.get(entry.name).isRegistered = false;
+ entry.appScanStats.isRegistered = false;
i.remove();
break;
}
AppScanStats getAppScanStatsById(int id) {
App temp = getById(id);
if (temp != null) {
- return mAppScanStats.get(temp.name);
+ return temp.appScanStats;
}
return null;
}
return connectedmap;
}
- void addScanEvent(BluetoothProto.ScanEvent event) {
- synchronized(mScanEvents) {
- if (mScanEvents.size() == NUM_SCAN_EVENTS_KEPT)
- mScanEvents.remove(0);
- mScanEvents.add(event);
- }
- }
-
/**
* Logs debug information.
*/
appScanStats.dumpToString(sb);
}
}
-
- void dumpProto(BluetoothProto.BluetoothLog proto) {
- synchronized(mScanEvents) {
- for (BluetoothProto.ScanEvent event : mScanEvents) {
- proto.addScanEvent(event);
- }
- }
- }
}
private Map<Integer, List<BluetoothGattService>> gattClientDatabases =
new HashMap<Integer, List<BluetoothGattService>>();
+ static final int NUM_SCAN_EVENTS_KEPT = 20;
+ /**
+ * Internal list of scan events to use with the proto
+ */
+ ArrayList<BluetoothProto.ScanEvent> mScanEvents =
+ new ArrayList<BluetoothProto.ScanEvent>(NUM_SCAN_EVENTS_KEPT);
+
private ServiceDeclaration addDeclaration() {
synchronized (mServiceDeclarations) {
mServiceDeclarations.add(new ServiceDeclaration());
if (VDBG) Log.d(TAG, "onScanResult() - address=" + address
+ ", rssi=" + rssi);
List<UUID> remoteUuids = parseUuids(adv_data);
+ addScanResult();
+
for (ScanClient client : mScanManager.getRegularScanQueue()) {
if (client.uuids.length > 0) {
int matches = 0;
ScanSettings settings = client.settings;
if ((settings.getCallbackType() &
ScanSettings.CALLBACK_TYPE_ALL_MATCHES) != 0) {
+ app.appScanStats.addResult();
app.callback.onScanResult(result);
}
} catch (RemoteException e) {
mHandleMap.dump(sb);
}
+ void addScanResult() {
+ if (mScanEvents.isEmpty())
+ return;
+
+ BluetoothProto.ScanEvent curr = mScanEvents.get(mScanEvents.size() - 1);
+ curr.setNumberResults(curr.getNumberResults() + 1);
+ }
+
+ void addScanEvent(BluetoothProto.ScanEvent event) {
+ synchronized(mScanEvents) {
+ if (mScanEvents.size() == NUM_SCAN_EVENTS_KEPT)
+ mScanEvents.remove(0);
+ mScanEvents.add(event);
+ }
+ }
+
@Override
public void dumpProto(BluetoothProto.BluetoothLog proto) {
- mClientMap.dumpProto(proto);
- mServerMap.dumpProto(proto);
+ synchronized(mScanEvents) {
+ for (BluetoothProto.ScanEvent event : mScanEvents) {
+ proto.addScanEvent(event);
+ }
+ }
}
/**************************************************************************