手机功耗中蓝牙扫描事件插桩埋点
功耗主要监控蓝牙扫描的时间和次数,进而换算为频次监控。其中不同的蓝牙扫描模式带来的功耗影响也是不一样的。
即功耗影响度低延迟扫描>平衡模式扫描>低功耗模式。例如某款机型分别为:低延迟扫描 14.64mA,平衡模式扫描4.64mA,低功耗模式0.64mA
例如:亮屏车控界面使用低延迟扫描;车控后台且手机非doze状态使用平衡模式扫描;手机处于doze场景采用低功耗模式扫描。
蓝牙扫描事件开始埋点
android/qssi/packages/modules/Bluetooth/framework/java/android/bluetooth/le/BluetoothLeScanner.java
具体位于:BluetoothLeScanner.onScannerRegistered 进行插桩埋点
521 /**
522 * Application interface registered - app is ready to go
523 */
524 @Override
525 public void onScannerRegistered(int status, int scannerId) {
526 Log.d(TAG, "onScannerRegistered() - status=" + status
527 + " scannerId=" + scannerId + " mScannerId=" + mScannerId);
528 synchronized (this) {
529 if (status == BluetoothGatt.GATT_SUCCESS) {
530 try {
531 final SynchronousResultReceiver recv = SynchronousResultReceiver.get();
532 if (mScannerId == -1) {
533 // Registration succeeds after timeout, unregister scanner.
534 mBluetoothGatt.unregisterScanner(scannerId, mAttributionSource, recv);
535 } else {
536 mScannerId = scannerId;
537 mBluetoothGatt.startScan(mScannerId, mSettings, mFilters,
538 mAttributionSource, recv);
539 // 蓝牙扫描开始事件埋点,其中不同蓝牙模式的功耗是不一样的
540 PowerTrack.get().sendEvent(BLUETOOTH_START_SCAN, mAttributionSource.getUid(),
541 mAttributionSource.getPackageName() + "|" + mSettings.getScanMode() + "|" + mScannerId);
542 // 蓝牙扫描开始事件埋点
543 }
544 recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(null);
545 } catch (TimeoutException | RemoteException e) {
546 Log.e(TAG, "fail to start le scan: " + e);
547 mScannerId = -1;
548 }
549 } else if (status == ScanCallback.SCAN_FAILED_SCANNING_TOO_FREQUENTLY) {
550 // applicaiton was scanning too frequently
551 mScannerId = -2;
552 } else {
553 // registration failed
554 mScannerId = -1;
555 }
556 notifyAll();
557 }
558 }
蓝牙扫描结束事件埋点
android/qssi/packages/modules/Bluetooth/framework/java/android/bluetooth/le/BluetoothLeScanner.java
具体位于:BluetoothLeScanner.stopLeScan 进行插桩埋点
478 @RequiresPermission(android.Manifest.permission.BLUETOOTH_SCAN)
479 public void stopLeScan() {
480 synchronized (this) {
481 if (mScannerId <= 0) {
482 Log.e(TAG, "Error state, mLeHandle: " + mScannerId);
483 return;
484 }
485 try {
486 final SynchronousResultReceiver recv = SynchronousResultReceiver.get();
487 mBluetoothGatt.stopScan(mScannerId, mAttributionSource, recv);
488 recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(null);
489 // 蓝牙扫描结束事件埋点
490 PowerTrack.get().sendEvent(BLUETOOTH_STOP_SCAN, mAttributionSource.getUid(),
491 mAttributionSource.getPackageName() + "|" + mSettings.getScanMode() + "|" + mScannerId);
492 // 蓝牙扫描结束事件埋点
493
494 final SynchronousResultReceiver recv2 = SynchronousResultReceiver.get();
495 mBluetoothGatt.unregisterScanner(mScannerId, mAttributionSource, recv2);
496 recv2.awaitResultNoInterrupt(getSyncTimeout()).getValue(null);
497 } catch (TimeoutException | RemoteException e) {
498 Log.e(TAG, "Failed to stop scan and unregister", e);
499 }
500 mScannerId = -1;
501 }
502 }