android 蓝牙扫描方法,Android 8 蓝牙 扫描流程

记录android 8 蓝牙扫描设备的流程

src/com/android/settings/bluetooth/BluetoothSettings.java

@Override

protected List getPreferenceControllers(Context context) {

final List controllers = new ArrayList<>();

final Lifecycle lifecycle = getLifecycle();

mDeviceNamePrefController = new BluetoothDeviceNamePreferenceController(context, lifecycle);

mPairingPrefController = new BluetoothPairingPreferenceController(context, this,

(SettingsActivity) getActivity());

controllers.add(mDeviceNamePrefController);

controllers.add(mPairingPrefController);

controllers.add(new BluetoothFilesPreferenceController(context));

controllers.add(new BluetoothDeviceRenamePreferenceController(context, this, lifecycle));

return controllers;

}

src/com/android/settings/bluetooth/BluetoothPairingPreferenceController.java

public boolean handlePreferenceTreeClick(Preference preference) {

if (KEY_PAIRING.equals(preference.getKey())) {

mActivity.startPreferencePanelAsUser(mFragment, BluetoothPairingDetail.class.getName(),

null, R.string.bluetooth_pairing_page_title, null,

new UserHandle(UserHandle.myUserId()));

return true;

}

return false;

}

src/com/android/settings/bluetooth/BluetoothPairingDetail.java

@VisibleForTesting

void updateContent(int bluetoothState) {

switch (bluetoothState) {

case BluetoothAdapter.STATE_ON:

mDevicePreferenceMap.clear();

mLocalAdapter.setBluetoothEnabled(true);

addDeviceCategory(mAvailableDevicesCategory,

R.string.bluetooth_preference_found_devices,

BluetoothDeviceFilter.UNBONDED_DEVICE_FILTER, mInitialScanStarted);

updateFooterPreference(mFooterPreference);

mAlwaysDiscoverable.start();

enableScanning(); // 扫描

break;

case BluetoothAdapter.STATE_OFF:

finish();

break;

}

}

@Override

void enableScanning() {

// Clear all device states before first scan

if (!mInitialScanStarted) {

if (mAvailableDevicesCategory != null) {

removeAllDevices();

}

mLocalManager.getCachedDeviceManager().clearNonBondedDevices();

mInitialScanStarted = true;

}

super.enableScanning();

}

packages/apps/Settings/src/com/android/settings/bluetooth/DeviceListPreferenceFragment.java

@VisibleForTesting

void enableScanning() {

// LocalBluetoothAdapter already handles repeated scan requests

mLocalAdapter.startScanning(true);

mScanEnabled = true;

}

frameworks\base\packages\SettingsLib\src\com\android\settingslib\bluetooth\LocalBluetoothAdapter.java

public void startScanning(boolean force) {

// Only start if we're not already scanning

if (!mAdapter.isDiscovering()) { // 判断是否正在扫描

if (!force) {

// Don't scan more than frequently than SCAN_EXPIRATION_MS,

// unless forced

if (mLastScan + SCAN_EXPIRATION_MS > System.currentTimeMillis()) {

return;

}

// 放音乐的时候不进行扫描,除非强制

// If we are playing music, don't scan unless forced.

A2dpProfile a2dp = mProfileManager.getA2dpProfile();

if (a2dp != null && a2dp.isA2dpPlaying()) {

return;

}

A2dpSinkProfile a2dpSink = mProfileManager.getA2dpSinkProfile();

if ((a2dpSink != null) && (a2dpSink.isA2dpPlaying())){

return;

}

}

if (mAdapter.startDiscovery()) {

mLastScan = System.currentTimeMillis();

}

}

}

frameworks\base\core\java\android\bluetooth\BluetoothAdapter.java

public boolean startDiscovery() {

android.util.SeempLog.record(58);

if (getState() != STATE_ON) return false;

try {

mServiceLock.readLock().lock();

if (mService != null) return mService.startDiscovery();

} catch (RemoteException e) {

Log.e(TAG, "", e);

} finally {

mServiceLock.readLock().unlock();

}

return false;

}

packages\apps\Bluetooth\src\com\android\bluetooth\btservice\AdapterService.java

private static class AdapterServiceBinder extends IBluetooth.Stub {

public boolean startDiscovery() {

if (!Utils.checkCaller()) {

Log.w(TAG, "startDiscovery() - Not allowed for non-active user");

return false;

}

AdapterService service = getService();

if (service == null) return false;

return service.startDiscovery();

}

boolean startDiscovery() {

debugLog("startDiscovery");

enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,

"Need BLUETOOTH ADMIN permission");

//do not allow new connections with active multicast

A2dpService a2dpService = A2dpService.getA2dpService();

if (a2dpService != null &&

a2dpService.isMulticastFeatureEnabled() &&

a2dpService.isMulticastOngoing(null)) {

Log.i(TAG,"A2dp Multicast is Ongoing, ignore discovery");

return false;

}

if (mAdapterProperties.isDiscovering()) {

Log.i(TAG,"discovery already active, ignore startDiscovery");

return false;

}

return startDiscoveryNative();

}

Liu Tao

2019-3-27

标签:return,蓝牙,扫描,bluetooth,java,Android,null,startDiscovery,android

来源: https://www.cnblogs.com/helloworldtoyou/p/10607364.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值