连手机蓝牙 显示 蓝牙已断开连接
一.
1.1. /frameworks/base/packages/SettingsLib/res/values-zh-rCN/strings.xml
<string name="bluetooth_disconnected" msgid="6557104142667339895">"已断开连接"</string>
1.2.蓝牙配对 K:K-Rxxx_7.1_RK3399_FirmwareK_RXXX_RK3399_ANDROID7.1packagesappsSettingssrccomandroidsettingsluetoothBluetoothSettings.java
@Override public void setListening(boolean listening) { BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter(); if (defaultAdapter == null) return; if (listening) { mEnabled = defaultAdapter.isEnabled(); mConnected = defaultAdapter.getConnectionState() == BluetoothAdapter.STATE_CONNECTED; Log.d("gatsby","state aaa -> "+ BluetoothAdapter.STATE_CONNECTED); Log.d("gatsby","state bbb -> "+ defaultAdapter.getConnectionState()); mSummaryLoader.setSummary(this, getSummary()); mBluetoothManager.getEventManager().registerCallback(this); } else { mBluetoothManager.getEventManager().unregisterCallback(this); } } private CharSequence getSummary() { Log.d("gatsby","!mEnabled -> "+ !mEnabled + " mConnected ->"+mConnected); return mContext.getString(!mEnabled ? R.string.bluetooth_disabled : mConnected ? R.string.bluetooth_connected : R.string.bluetooth_disconnected); }
a. Log.d("gatsby","!mEnabled -> "+ !mEnabled + " mConnected ->"+mConnected);
mEnabled ? R.string.bluetooth_disabled: mConnected ? R.string.bluetooth_connected: R.string.bluetooth_disconnected
mEnabled ?"disabled" : mConnected ? "bluetooth_connected" : "bluetooth_disconnected";
打开蓝牙开关 未连接蓝牙 !mEnabled -> false mConnected ->false
b.mConnected = defaultAdapter.getConnectionState() == BluetoothAdapter.STATE_CONNECTED;
defaultAdapter.getConnectionState() -> 0 BluetoothAdapter.STATE_CONNECTED -> 2
1.3.Android检查设备连接状态
K:K-Rxxx_7.1_RK3399_FirmwareK_RXXX_RK3399_ANDROID7.1frameworksasecorejavaandroidluetoothBluetoothAdapter.java
/** The profile is in disconnected state */ public static final int STATE_DISCONNECTED = 0; /** * Get the current connection state of the local Bluetooth adapter. * This can be used to check whether the local Bluetooth adapter is connected * to any profile of any other remote Bluetooth Device. * * <p> Use this function along with {@link #ACTION_CONNECTION_STATE_CHANGED} * intent to get the connection state of the adapter. * * @return One of {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTED}, * {@link #STATE_CONNECTING} or {@link #STATE_DISCONNECTED} * * @hide */ public int getConnectionState() { if (getState() != STATE_ON) return BluetoothAdapter.STATE_DISCONNECTED; try { mServiceLock.readLock().lock(); if (mService != null) return mService.getAdapterConnectionState(); } catch (RemoteException e) { Log.e(TAG, "getConnectionState:", e); } finally { mServiceLock.readLock().unlock(); } return BluetoothAdapter.STATE_DISCONNECTED; }
调用BluetoothAdapter中的getConnectionState()方法,直接检查是否存在连接状态的蓝牙设备存在 不能检测出手机发出的蓝牙 可以检测出蓝牙耳机
/** The profile is in disconnected state */ public static final int STATE_DISCONNECTED = 0; /** The profile is in connecting state */ public static final int STATE_CONNECTING = 1; /** The profile is in connected state */ public static final int STATE_CONNECTED = 2; /** The profile is in disconnecting state */ public static final int STATE_DISCONNECTING = 3;