一. Android7.1 連手機藍牙 顯示 藍牙已斷開連接
1.1. /frameworks/base/packages/SettingsLib/res/values-zh-rCN/strings.xml
<string name="bluetooth_disconnected" msgid="6557104142667339895">"已斷開連接"</string>
1.2.藍牙配對 K:\ZK-Rxxx_7.1_RK3399_Firmware\ZK_RXXX_RK3399_ANDROID7.1\packages\apps\Settings\src\com\android\settings\bluetooth\BluetoothSettings.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:\ZK-Rxxx_7.1_RK3399_Firmware\ZK_RXXX_RK3399_ANDROID7.1\frameworks\base\core\java\android\bluetooth\BluetoothAdapter.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;
二.Android6.0 藍牙直接彈窗
2.1.packages\apps\Bluetooth\src\com\android\bluetooth\btservice\AdapterProperties.java
void updateFeatureSupport(byte[] val) {
--- a/packages/apps/Bluetooth/src/com/android/bluetooth/btservice/AdapterProperties.java +++ b/packages/apps/Bluetooth/src/com/android/bluetooth/btservice/AdapterProperties.java @@ -612,11 +612,11 @@ class AdapterProperties { adapterPropertyChangedCallback received before onBluetoothReady */ if (mDiscoverableTimeout != 0) - setScanMode(AbstractionLayer.BT_SCAN_MODE_CONNECTABLE); + setScanMode(AbstractionLayer.BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE); else /* if timeout == never (0) at startup */ setScanMode(AbstractionLayer.BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE); /* though not always required, this keeps NV up-to date on first-boot after flash */ - setDiscoverableTimeout(mDiscoverableTimeout); + setDiscoverableTimeout(0/*mDiscoverableTimeout*/); } } }
2.2.packages\apps\Bluetooth\src\com\android\bluetooth\opp\BluetoothOppNotification.java
private void updateIncomingFileConfirmNotification() {
--- a/packages/apps/Bluetooth/src/com/android/bluetooth/opp/BluetoothOppNotification.java +++ b/packages/apps/Bluetooth/src/com/android/bluetooth/opp/BluetoothOppNotification.java @@ -526,6 +526,15 @@ class BluetoothOppNotification { n.deleteIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0); mNotificationMgr.notify(id, n); + + Uri uri = intent.getData(); + Intent in = new Intent(mContext, BluetoothOppIncomingFileConfirmActivity.class); + in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + in.setDataAndNormalize(uri); + mContext.startActivity(in); + + NotificationManager notGatsby = (NotificationManager)mContext + .getSystemService(Context.NOTIFICATION_SERVICE); } cursor.close(); }