Index: device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothAdapter.java |
diff --git a/device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothAdapter.java b/device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothAdapter.java |
index 0a54a0bdfe73f817a2cff9c0ee24783c91842eed..54a8eb926768ad12f3994c9df4baec9adf93ea09 100644 |
--- a/device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothAdapter.java |
+++ b/device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothAdapter.java |
@@ -8,6 +8,10 @@ import android.Manifest; |
import android.annotation.TargetApi; |
import android.bluetooth.BluetoothAdapter; |
import android.bluetooth.le.ScanSettings; |
+import android.content.BroadcastReceiver; |
+import android.content.Context; |
+import android.content.Intent; |
+import android.content.IntentFilter; |
import android.os.Build; |
import android.os.ParcelUuid; |
@@ -26,7 +30,7 @@ import java.util.List; |
*/ |
@JNINamespace("device") |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) |
-final class ChromeBluetoothAdapter { |
+final class ChromeBluetoothAdapter extends BroadcastReceiver { |
private static final String TAG = "Bluetooth"; |
private long mNativeBluetoothAdapterAndroid; |
@@ -49,6 +53,7 @@ final class ChromeBluetoothAdapter { |
long nativeBluetoothAdapterAndroid, Wrappers.BluetoothAdapterWrapper adapterWrapper) { |
mNativeBluetoothAdapterAndroid = nativeBluetoothAdapterAndroid; |
mAdapter = adapterWrapper; |
+ registerBroadcastReceiver(); |
if (adapterWrapper == null) { |
Log.i(TAG, "ChromeBluetoothAdapter created with no adapterWrapper."); |
} else { |
@@ -63,6 +68,7 @@ final class ChromeBluetoothAdapter { |
private void onBluetoothAdapterAndroidDestruction() { |
stopScan(); |
mNativeBluetoothAdapterAndroid = 0; |
+ unregisterBroadcastReceiver(); |
} |
// --------------------------------------------------------------------------------------------- |
@@ -187,6 +193,19 @@ final class ChromeBluetoothAdapter { |
|| context.checkPermission(Manifest.permission.ACCESS_FINE_LOCATION); |
} |
+ private void registerBroadcastReceiver() { |
+ if (mAdapter != null) { |
+ mAdapter.getContext().registerReceiver( |
+ this, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)); |
+ } |
+ } |
+ |
+ private void unregisterBroadcastReceiver() { |
+ if (mAdapter != null) { |
+ mAdapter.getContext().unregisterReceiver(this); |
Ted C
2016/02/26 17:50:35
my comment about marking the adapter final is to e
scheib
2016/02/26 20:23:47
Acknowledged.
|
+ } |
+ } |
+ |
/** |
* Starts a Low Energy scan. |
* @return True on success. |
@@ -275,6 +294,45 @@ final class ChromeBluetoothAdapter { |
} |
} |
+ @Override |
+ public void onReceive(Context context, Intent intent) { |
+ String action = intent.getAction(); |
+ |
+ if (isPresent() && BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) { |
+ int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR); |
+ |
+ Log.w(TAG, "onReceive: BluetoothAdapter.ACTION_STATE_CHANGED: %s", |
+ getBluetoothStateString(state)); |
+ |
+ switch (state) { |
+ case BluetoothAdapter.STATE_ON: |
+ nativeOnAdapterStateChanged(mNativeBluetoothAdapterAndroid, true); |
+ break; |
+ case BluetoothAdapter.STATE_OFF: |
+ nativeOnAdapterStateChanged(mNativeBluetoothAdapterAndroid, false); |
+ break; |
+ default: |
+ // do nothing |
+ } |
+ } |
+ } |
+ |
+ private String getBluetoothStateString(int state) { |
+ switch (state) { |
+ case BluetoothAdapter.STATE_OFF: |
+ return "STATE_OFF"; |
+ case BluetoothAdapter.STATE_ON: |
+ return "STATE_ON"; |
+ case BluetoothAdapter.STATE_TURNING_OFF: |
+ return "STATE_TURNING_OFF"; |
+ case BluetoothAdapter.STATE_TURNING_ON: |
+ return "STATE_TURNING_ON"; |
+ default: |
+ assert false; |
+ return "illegal state: " + state; |
+ } |
+ } |
+ |
// --------------------------------------------------------------------------------------------- |
// BluetoothAdapterAndroid C++ methods declared for access from java: |
@@ -287,4 +345,8 @@ final class ChromeBluetoothAdapter { |
// http://crbug.com/505554 |
private native void nativeCreateOrUpdateDeviceOnScan(long nativeBluetoothAdapterAndroid, |
String address, Object bluetoothDeviceWrapper, List<ParcelUuid> advertisedUuids); |
+ |
+ // Binds to BluetoothAdapterAndroid::nativeOnAdapterStateChanged |
+ private native void nativeOnAdapterStateChanged( |
+ long nativeBluetoothAdapterAndroid, boolean powered); |
} |