问题
APK UI上的转圈启、停是根据什么广播来的,安卓原生实际的扫描时间如何控制
分析
扫描开始广播
BluetoothAdapter.ACTION_DISCOVERY_STARTED = "android.bluetooth.adapter.action.DISCOVERY_STARTED";
扫描完成广播
BluetoothAdapter.ACTION_DISCOVERY_FINISHED = "android.bluetooth.adapter.action.DISCOVERY_FINISHED";
如下路径发出广播
packages\apps\Bluetooth\src\com\android\bluetooth\btservice\AdapterProperties.java
void discoveryStateChangeCallback(int state) {infoLog("Callback:discoveryStateChangeCallback with state:" + state);synchronized (mObject) {Intent intent;if (state == AbstractionLayer.BT_DISCOVERY_STOPPED) {mDiscovering = false;mService.clearDiscoveringPackages();mDiscoveryEndMs = System.currentTimeMillis();intent = new Intent(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);mService.sendBroadcast(intent, AdapterService.BLUETOOTH_PERM);} else if (state == AbstractionLayer.BT_DISCOVERY_STARTED) {mDiscovering = true;mDiscoveryEndMs = System.currentTimeMillis() + DEFAULT_DISCOVERY_TIMEOUT_MS;intent = new Intent(BluetoothAdapter.ACTION_DISCOVERY_STARTED);mService.sendBroadcast(intent, AdapterService.BLUETOOTH_PERM);}}}
协议栈中实际扫描时间控制
system\bt\btif\src\btif_dm.cc
/********************************************************************************* Function btif_dm_start_discovery** Description Start device discovery/inquiry** Returns bt_status_t*******************************************************************************/
bt_status_t btif_dm_start_discovery(void) {tBTA_DM_INQ inq_params;tBTA_SERVICE_MASK services = 0;BTIF_TRACE_EVENT("%s", __func__);/* Cleanup anything remaining on index 0 */do_in_main_thread(FROM_HERE,base::Bind(&BTM_BleAdvFilterParamSetup, BTM_BLE_SCAN_COND_DELETE, 0,nullptr, base::Bind(&bte_scan_filt_param_cfg_evt, 0)));auto adv_filt_param = std::make_unique<btgatt_filt_param_setup_t>();/* Add an allow-all filter on index 0*/adv_filt_param->dely_mode = IMMEDIATE_DELY_MODE;adv_filt_param->feat_seln = ALLOW_ALL_FILTER;adv_filt_param->filt_logic_type = BTA_DM_BLE_PF_FILT_LOGIC_OR;adv_filt_param->list_logic_type = BTA_DM_BLE_PF_LIST_LOGIC_OR;adv_filt_param->rssi_low_thres = LOWEST_RSSI_VALUE;adv_filt_param->rssi_high_thres = LOWEST_RSSI_VALUE;do_in_main_thread(FROM_HERE, base::Bind(&BTM_BleAdvFilterParamSetup, BTM_BLE_SCAN_COND_ADD,0, base::Passed(&adv_filt_param),base::Bind(&bte_scan_filt_param_cfg_evt, 0)));/* TODO: Do we need to handle multiple inquiries at the same time? *//* Set inquiry params and call API */inq_params.mode = BTA_DM_GENERAL_INQUIRY | BTA_BLE_GENERAL_INQUIRY;inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION;inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS;inq_params.report_dup = true;inq_params.filter_type = BTA_DM_INQ_CLR;/* TODO: Filter device by BDA needs to be implemented here *//* Will be enabled to true once inquiry busy level has been received */btif_dm_inquiry_in_progress = false;/* find nearby devices */BTA_DmSearch(&inq_params, services, bte_search_devices_evt);return BT_STATUS_SUCCESS;
}
其中 BTIF_DM_DEFAULT_INQ_MAX_DURATION可以控制实际扫描时长,unit--1.28s
Note:packages\apps\Bluetooth\src\com\android\bluetooth\btservice\AdapterProperties.java中的private static final long DEFAULT_DISCOVERY_TIMEOUT_MS = 12800;并不能改变实际扫描时长