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 07f6349ddaaa8c6954e7e93511c10cccfb41b9be..3c8b26521f600895c60f8deadbf46b72bfc27f73 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 |
@@ -4,7 +4,9 @@ |
package org.chromium.device.bluetooth; |
+import android.Manifest; |
import android.content.Context; |
+import android.content.ContextWrapper; |
import android.content.pm.PackageManager; |
import org.chromium.base.CalledByNative; |
@@ -20,6 +22,7 @@ final class BluetoothAdapter { |
private static final String TAG = Log.makeTag("Bluetooth"); |
private final boolean mHasBluetoothPermission; |
+ private android.bluetooth.BluetoothAdapter mAdapter; |
@CalledByNative |
private static BluetoothAdapter create(Context context) { |
@@ -27,16 +30,78 @@ final class BluetoothAdapter { |
} |
@CalledByNative |
- private boolean hasBluetoothPermission() { |
- return mHasBluetoothPermission; |
+ private static BluetoothAdapter createWithoutPermissionForTesting(Context context) { |
+ Context contextWithoutPermission = new ContextWrapper(context) { |
+ @Override |
+ public int checkCallingOrSelfPermission(String permission) { |
+ return PackageManager.PERMISSION_DENIED; |
+ } |
+ }; |
+ return new BluetoothAdapter(contextWithoutPermission); |
} |
+ // Constructs a BluetoothAdapter. |
private BluetoothAdapter(Context context) { |
mHasBluetoothPermission = |
- context.checkCallingOrSelfPermission(android.Manifest.permission.BLUETOOTH) |
- == PackageManager.PERMISSION_GRANTED; |
+ context.checkCallingOrSelfPermission(Manifest.permission.BLUETOOTH) |
+ == PackageManager.PERMISSION_GRANTED |
+ && context.checkCallingOrSelfPermission(Manifest.permission.BLUETOOTH_ADMIN) |
+ == PackageManager.PERMISSION_GRANTED; |
if (!mHasBluetoothPermission) { |
- Log.w(TAG, "Can not use bluetooth API, requires BLUETOOTH permission."); |
+ Log.w(TAG, |
+ "Bluetooth API disabled; BLUETOOTH and BLUETOOTH_ADMIN permissions required."); |
+ return; |
} |
+ |
+ mAdapter = android.bluetooth.BluetoothAdapter.getDefaultAdapter(); |
+ if (mAdapter == null) Log.i(TAG, "No adapter found."); |
+ } |
+ |
+ @CalledByNative |
+ private boolean hasBluetoothPermission() { |
+ return mHasBluetoothPermission; |
+ } |
+ |
+ // --------------------------------------------------------------------------------------------- |
+ // BluetoothAdapterAndroid.h interface: |
+ |
+ @CalledByNative |
+ private String getAddress() { |
+ if (isPresent()) { |
+ return mAdapter.getAddress(); |
+ } else { |
+ return ""; |
+ } |
+ } |
+ |
+ @CalledByNative |
+ private String getName() { |
+ if (isPresent()) { |
+ return mAdapter.getName(); |
+ } else { |
+ return ""; |
+ } |
+ } |
+ |
+ @CalledByNative |
+ private boolean isPresent() { |
+ return mAdapter != null; |
+ } |
+ |
+ @CalledByNative |
+ private boolean isPowered() { |
+ return isPresent() && mAdapter.isEnabled(); |
+ } |
+ |
+ @CalledByNative |
+ private boolean isDiscoverable() { |
+ return isPresent() |
+ && mAdapter.getScanMode() |
+ == android.bluetooth.BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE; |
+ } |
+ |
+ @CalledByNative |
+ private boolean isDiscovering() { |
+ return isPresent() && mAdapter.isDiscovering(); |
} |
} |