| 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..fdddf3d965e81a0f9374a353a62c1254cf09eafa 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;
|
| @@ -45,7 +46,76 @@ import java.util.UUID;
|
| class Wrappers {
|
| private static final String TAG = "Bluetooth";
|
|
|
| - 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 +133,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.");
|
| @@ -251,54 +297,6 @@ class Wrappers {
|
| }
|
|
|
| /**
|
| - * Wraps android.bluetooth.BluetoothDevice.
|
| - */
|
| - static class BluetoothDeviceWrapper {
|
| - private final BluetoothDevice mDevice;
|
| - private final HashMap<BluetoothGattCharacteristic, BluetoothGattCharacteristicWrapper>
|
| - mCharacteristicsToWrappers;
|
| - private final HashMap<BluetoothGattDescriptor, BluetoothGattDescriptorWrapper>
|
| - mDescriptorsToWrappers;
|
| -
|
| - public BluetoothDeviceWrapper(BluetoothDevice device) {
|
| - mDevice = device;
|
| - mCharacteristicsToWrappers =
|
| - new HashMap<BluetoothGattCharacteristic, BluetoothGattCharacteristicWrapper>();
|
| - mDescriptorsToWrappers =
|
| - new HashMap<BluetoothGattDescriptor, BluetoothGattDescriptorWrapper>();
|
| - }
|
| -
|
| - public BluetoothGattWrapper connectGatt(
|
| - Context context, boolean autoConnect, BluetoothGattCallbackWrapper callback) {
|
| - return new BluetoothGattWrapper(
|
| - mDevice.connectGatt(context, autoConnect,
|
| - new ForwardBluetoothGattCallbackToWrapper(callback, this)),
|
| - this);
|
| - }
|
| -
|
| - public String getAddress() {
|
| - return mDevice.getAddress();
|
| - }
|
| -
|
| - public int getBluetoothClass_getDeviceClass() {
|
| - if (mDevice == null || mDevice.getBluetoothClass() == null) {
|
| - // BluetoothDevice.getBluetoothClass() returns null if adapter has been powered off.
|
| - // Return DEVICE_CLASS_UNSPECIFIED in these cases.
|
| - return DEVICE_CLASS_UNSPECIFIED;
|
| - }
|
| - return mDevice.getBluetoothClass().getDeviceClass();
|
| - }
|
| -
|
| - public int getBondState() {
|
| - return mDevice.getBondState();
|
| - }
|
| -
|
| - public String getName() {
|
| - return mDevice.getName();
|
| - }
|
| - }
|
| -
|
| - /**
|
| * Wraps android.bluetooth.BluetoothGatt.
|
| */
|
| static class BluetoothGattWrapper {
|
|
|