Chromium Code Reviews| 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..4e90503d0966795c195c6c2a62e8dac9af38d6fb 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); |
| + } |
| + } |
| + |
| /** |
| * Starts a Low Energy scan. |
| * @return True on success. |
| @@ -275,6 +294,28 @@ 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, "BluetoothStateReceiver#onReceive action: %d", state); |
|
scheib
2016/02/26 00:05:59
This string doesn't mean much to me, would it be m
perja
2016/02/26 10:51:04
Done.
|
| + |
| + switch (state) { |
| + case BluetoothAdapter.STATE_ON: |
| + nativeOnAdapterStateChanged(mNativeBluetoothAdapterAndroid, true); |
| + break; |
| + case BluetoothAdapter.STATE_OFF: |
| + nativeOnAdapterStateChanged(mNativeBluetoothAdapterAndroid, false); |
| + break; |
| + default: |
| + // do nothing |
| + } |
| + } |
| + } |
| + |
| // --------------------------------------------------------------------------------------------- |
| // BluetoothAdapterAndroid C++ methods declared for access from java: |
| @@ -287,4 +328,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); |
| } |