| Index: device/bluetooth/android/java/src/org/chromium/device/bluetooth/Wrappers.java
|
| diff --git a/device/bluetooth/android/java/src/org/chromium/device/bluetooth/Wrappers.java b/device/bluetooth/android/java/src/org/chromium/device/bluetooth/Wrappers.java
|
| index 282faa877e3c0b4d7c62f3ee86d9f6520d49840b..1462ba2edde625180eeac977d01ac2cb4cd300e8 100644
|
| --- a/device/bluetooth/android/java/src/org/chromium/device/bluetooth/Wrappers.java
|
| +++ b/device/bluetooth/android/java/src/org/chromium/device/bluetooth/Wrappers.java
|
| @@ -13,6 +13,7 @@ import android.bluetooth.BluetoothGattCallback;
|
| import android.bluetooth.BluetoothGattCharacteristic;
|
| import android.bluetooth.BluetoothGattDescriptor;
|
| import android.bluetooth.BluetoothGattService;
|
| +import android.bluetooth.BluetoothManager;
|
| import android.bluetooth.le.BluetoothLeScanner;
|
| import android.bluetooth.le.ScanCallback;
|
| import android.bluetooth.le.ScanFilter;
|
| @@ -47,6 +48,77 @@ class Wrappers {
|
|
|
| public static final int DEVICE_CLASS_UNSPECIFIED = 0x1F00;
|
|
|
| + private static boolean canUseLE(Context context) {
|
| + final boolean hasMinAPI = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
|
| + if (!hasMinAPI) {
|
| + Log.i(TAG, "Failed to create Bluetooth object: SDK version (%d) too low.",
|
| + Build.VERSION.SDK_INT);
|
| + return false;
|
| + }
|
| +
|
| + final boolean hasPermissions =
|
| + context.checkCallingOrSelfPermission(Manifest.permission.BLUETOOTH)
|
| + == PackageManager.PERMISSION_GRANTED
|
| + && context.checkCallingOrSelfPermission(Manifest.permission.BLUETOOTH_ADMIN)
|
| + == PackageManager.PERMISSION_GRANTED;
|
| + if (!hasPermissions) {
|
| + Log.w(TAG, "Failed to create Bluetooth object: Lacking Bluetooth permissions.");
|
| + return false;
|
| + }
|
| +
|
| + // Only Low Energy currently supported, see BluetoothAdapterAndroid class note.
|
| + final boolean hasLowEnergyFeature =
|
| + Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2
|
| + && context.getPackageManager().hasSystemFeature(
|
| + PackageManager.FEATURE_BLUETOOTH_LE);
|
| + if (!hasLowEnergyFeature) {
|
| + Log.i(TAG, "Failed to create Bluetooth object: No Low Energy support.");
|
| + return false;
|
| + }
|
| +
|
| + return true;
|
| + }
|
| +
|
| + /**
|
| + * Wraps android.bluetooth.BluetoothManager.
|
| + */
|
| + static class BluetoothManagerWrapper {
|
| + private final BluetoothManager mManager;
|
| +
|
| + /**
|
| + * Creates a BluetoothManagerWrapper using the default android.bluetooth.BluetoothManager.
|
| + * May fail if the default adapter is not available or if the application does not have
|
| + * sufficient permissions.
|
| + */
|
| + @CalledByNative("BluetoothManagerWrapper")
|
| + public static BluetoothManagerWrapper createWithDefaultManager(Context context) {
|
| + if (!canUseLE(context)) {
|
| + return null;
|
| + }
|
| + BluetoothManager manager =
|
| + (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
|
| + if (manager == null) {
|
| + Log.i(TAG, "BluetoothManagerWrapper.create failed: Default adapter not found.");
|
| + return null;
|
| + } else {
|
| + return new BluetoothManagerWrapper(manager);
|
| + }
|
| + }
|
| +
|
| + public BluetoothManagerWrapper(BluetoothManager manager) {
|
| + mManager = manager;
|
| + }
|
| +
|
| + public List<BluetoothDeviceWrapper> getConnectedDevices(int profile) {
|
| + List<BluetoothDevice> devices = mManager.getConnectedDevices(profile);
|
| + ArrayList<BluetoothDeviceWrapper> device_wrappers = new ArrayList<>(devices.size());
|
| + for (BluetoothDevice device : devices) {
|
| + device_wrappers.add(new BluetoothDeviceWrapper(device));
|
| + }
|
| + return device_wrappers;
|
| + }
|
| + }
|
| +
|
| /**
|
| * Wraps android.bluetooth.BluetoothAdapter.
|
| */
|
| @@ -63,33 +135,9 @@ class Wrappers {
|
| */
|
| @CalledByNative("BluetoothAdapterWrapper")
|
| public static BluetoothAdapterWrapper createWithDefaultAdapter(Context context) {
|
| - final boolean hasMinAPI = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
|
| - if (!hasMinAPI) {
|
| - Log.i(TAG, "BluetoothAdapterWrapper.create failed: SDK version (%d) too low.",
|
| - Build.VERSION.SDK_INT);
|
| + if (!canUseLE(context)) {
|
| return null;
|
| }
|
| -
|
| - final boolean hasPermissions =
|
| - context.checkCallingOrSelfPermission(Manifest.permission.BLUETOOTH)
|
| - == PackageManager.PERMISSION_GRANTED
|
| - && context.checkCallingOrSelfPermission(Manifest.permission.BLUETOOTH_ADMIN)
|
| - == PackageManager.PERMISSION_GRANTED;
|
| - if (!hasPermissions) {
|
| - Log.w(TAG, "BluetoothAdapterWrapper.create failed: Lacking Bluetooth permissions.");
|
| - return null;
|
| - }
|
| -
|
| - // Only Low Energy currently supported, see BluetoothAdapterAndroid class note.
|
| - final boolean hasLowEnergyFeature =
|
| - Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2
|
| - && context.getPackageManager().hasSystemFeature(
|
| - PackageManager.FEATURE_BLUETOOTH_LE);
|
| - if (!hasLowEnergyFeature) {
|
| - Log.i(TAG, "BluetoothAdapterWrapper.create failed: No Low Energy support.");
|
| - return null;
|
| - }
|
| -
|
| BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
|
| if (adapter == null) {
|
| Log.i(TAG, "BluetoothAdapterWrapper.create failed: Default adapter not found.");
|
| @@ -296,6 +344,10 @@ class Wrappers {
|
| public String getName() {
|
| return mDevice.getName();
|
| }
|
| +
|
| + public ParcelUuid[] getUuids() {
|
| + return mDevice.getUuids();
|
| + }
|
| }
|
|
|
| /**
|
|
|