Chromium Code Reviews| Index: device/bluetooth/android/java/src/org/chromium/device/bluetooth/BluetoothAdapter.java |
| diff --git a/device/bluetooth/android/java/src/org/chromium/device/bluetooth/BluetoothAdapter.java b/device/bluetooth/android/java/src/org/chromium/device/bluetooth/BluetoothAdapter.java |
| index 3c8b26521f600895c60f8deadbf46b72bfc27f73..7415ab2f9abceb05aaf52920077ba57cabfb9a00 100644 |
| --- a/device/bluetooth/android/java/src/org/chromium/device/bluetooth/BluetoothAdapter.java |
| +++ b/device/bluetooth/android/java/src/org/chromium/device/bluetooth/BluetoothAdapter.java |
| @@ -5,14 +5,20 @@ |
| package org.chromium.device.bluetooth; |
| import android.Manifest; |
| +import android.bluetooth.le.ScanCallback; |
| +import android.bluetooth.le.ScanResult; |
| +import android.bluetooth.le.ScanSettings; |
| import android.content.Context; |
| import android.content.ContextWrapper; |
| import android.content.pm.PackageManager; |
| +import android.os.Build; |
| import org.chromium.base.CalledByNative; |
| import org.chromium.base.JNINamespace; |
| import org.chromium.base.Log; |
| +import java.util.List; |
| + |
| /** |
| * Exposes android.bluetooth.BluetoothAdapter as necessary for C++ |
| * device::BluetoothAdapterAndroid. |
| @@ -21,8 +27,13 @@ import org.chromium.base.Log; |
| final class BluetoothAdapter { |
| private static final String TAG = Log.makeTag("Bluetooth"); |
| - private final boolean mHasBluetoothPermission; |
| + private final boolean mHasBluetoothCapability; |
| private android.bluetooth.BluetoothAdapter mAdapter; |
| + private int mNumDiscoverySessions = 0; |
| + private ScanCallback mLeScanCallback; |
| + |
| + // --------------------------------------------------------------------------------------------- |
| + // Construction: |
| @CalledByNative |
| private static BluetoothAdapter create(Context context) { |
| @@ -42,28 +53,46 @@ final class BluetoothAdapter { |
| // Constructs a BluetoothAdapter. |
| private BluetoothAdapter(Context context) { |
| - mHasBluetoothPermission = |
| + final boolean hasMinAPI = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP; |
| + final boolean hasPermissions = |
| context.checkCallingOrSelfPermission(Manifest.permission.BLUETOOTH) |
| == PackageManager.PERMISSION_GRANTED |
| && context.checkCallingOrSelfPermission(Manifest.permission.BLUETOOTH_ADMIN) |
| == PackageManager.PERMISSION_GRANTED; |
| - if (!mHasBluetoothPermission) { |
| - Log.w(TAG, |
| - "Bluetooth API disabled; BLUETOOTH and BLUETOOTH_ADMIN permissions required."); |
| + final boolean hasLowEnergyFeature = |
| + context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE); |
| + mHasBluetoothCapability = hasMinAPI && hasPermissions && hasLowEnergyFeature; |
| + if (!mHasBluetoothCapability) { |
| + if (!hasMinAPI) { |
| + Log.i(TAG, "Bluetooth API disabled; SDK version (%d) too low.", |
|
ortuno
2015/05/21 16:48:53
You are logging a lot of stuff. We should probably
scheib
2015/05/22 01:26:46
I've moved the verbose bits to .d and .v, which ar
|
| + Build.VERSION.SDK_INT); |
| + } else if (!hasPermissions) { |
| + Log.w(TAG, "Bluetooth API disabled; BLUETOOTH and BLUETOOTH_ADMIN permissions " |
| + + "required."); |
| + } else if (!hasLowEnergyFeature) { |
| + Log.i(TAG, "Bluetooth API disabled; Low Energy not supported on system."); |
| + } |
| return; |
| } |
| mAdapter = android.bluetooth.BluetoothAdapter.getDefaultAdapter(); |
| - if (mAdapter == null) Log.i(TAG, "No adapter found."); |
| + if (mAdapter == null) { |
| + Log.i(TAG, "No adapter found."); |
| + } else { |
| + Log.i(TAG, "BluetoothAdapter successfully constructed."); |
| + } |
| } |
| + // --------------------------------------------------------------------------------------------- |
| + // Accessors @CalledByNative for BluetoothAdapterAndroid: |
| + |
| @CalledByNative |
| - private boolean hasBluetoothPermission() { |
| - return mHasBluetoothPermission; |
| + private boolean hasBluetoothCapability() { |
| + return mHasBluetoothCapability; |
| } |
| // --------------------------------------------------------------------------------------------- |
| - // BluetoothAdapterAndroid.h interface: |
| + // BluetoothAdapterAndroid interface @CalledByNative for BluetoothAdapterAndroid: |
| @CalledByNative |
| private String getAddress() { |
| @@ -104,4 +133,74 @@ final class BluetoothAdapter { |
| private boolean isDiscovering() { |
| return isPresent() && mAdapter.isDiscovering(); |
| } |
| + |
| + @CalledByNative |
| + private boolean addDiscoverySession() { |
|
ortuno
2015/05/21 16:48:53
nit: Add a comment for this function.
scheib
2015/05/22 01:26:46
Done.
|
| + if (!isPowered()) { |
| + Log.i(TAG, "addDiscoverySession: Fails: !isPowered"); |
| + return false; |
| + } |
| + |
| + if (mNumDiscoverySessions > 0) { |
| + Log.i(TAG, "addDiscoverySession: Already scanning."); |
| + return true; |
| + } |
| + Log.i(TAG, "addDiscoverySession"); |
| + mNumDiscoverySessions++; |
| + |
| + ScanSettings.Builder scanSettingsBuilder = new ScanSettings.Builder(); |
|
ortuno
2015/05/21 16:48:53
I would refactor this a bit:
ScanSettings scanSet
scheib
2015/05/22 01:26:46
Done.
scheib
2015/05/22 01:26:46
Done.
|
| + // Note: SCAN_FAILED_FEATURE_UNSUPPORTED is caused (at least on some |
| + // devices) if scanSettingsBuilder.setReportDelay() is set or if |
| + // SCAN_MODE_LOW_LATENCY isn't used. |
| + scanSettingsBuilder.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY); |
| + |
| + if (mLeScanCallback == null) mLeScanCallback = new DiscoveryScanCallback(); |
|
ortuno
2015/05/21 16:48:53
I think you need braces here:
https://google-styl
scheib
2015/05/22 01:26:45
Done.
|
| + mAdapter.getBluetoothLeScanner().startScan( |
| + null /* filters */, scanSettingsBuilder.build(), mLeScanCallback); |
| + return true; |
| + } |
| + |
| + @CalledByNative |
| + private boolean removeDiscoverySession() { |
| + Log.i(TAG, "removeDiscoverySession"); |
| + switch (--mNumDiscoverySessions) { |
|
ortuno
2015/05/21 16:48:53
nit: I think it would be clearer this way:
if (mN
scheib
2015/05/22 01:26:46
Done.
|
| + case -1: |
| + assert false; |
| + Log.w(TAG, "removeDiscoverySession: No scan in progress."); |
| + mNumDiscoverySessions = 0; |
| + return false; |
| + case 0: |
| + mAdapter.getBluetoothLeScanner().stopScan(mLeScanCallback); |
| + } |
| + return true; |
| + } |
| + |
| + // --------------------------------------------------------------------------------------------- |
| + // Implementation details: |
| + |
| + private class DiscoveryScanCallback extends ScanCallback { |
| + @Override |
| + public void onBatchScanResults(List<ScanResult> results) { |
| + Log.i(TAG, "onBatchScanResults"); |
| + } |
| + |
| + @Override |
| + public void onScanResult(int callbackType, ScanResult result) { |
| + Log.i(TAG, "onScanResult %s %s", result.getDevice().getAddress(), |
| + result.getDevice().getName()); |
| + } |
| + |
| + @Override |
| + public void onScanFailed(int errorCode) { |
| + Log.w(TAG, "onScanFailed: %d", errorCode); |
| + // DISCUSS IN CODE REVIEW. |
| + // |
| + // TODO(scheib): Current device/bluetooth API doesn't support a way to communicate |
| + // this asynchronous failure. If there was a way to communicate asynchronous |
| + // success, then the response to AddDiscoverySession would be delayed until then or |
| + // this error. But without only the error we must presume success. |
| + // |
| + // NEED ISSUE NUMBER. |
| + } |
| + } |
| } |