| Index: device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothManager.java
|
| diff --git a/device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothManager.java b/device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothManager.java
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..34cdd6920d97c562b48f9bd5da8c564c902c2795
|
| --- /dev/null
|
| +++ b/device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothManager.java
|
| @@ -0,0 +1,107 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +package org.chromium.device.bluetooth;
|
| +
|
| +import android.annotation.TargetApi;
|
| +import android.bluetooth.BluetoothProfile;
|
| +import android.os.Build;
|
| +import android.os.ParcelUuid;
|
| +
|
| +import org.chromium.base.Log;
|
| +import org.chromium.base.annotations.CalledByNative;
|
| +import org.chromium.base.annotations.JNINamespace;
|
| +
|
| +import java.util.List;
|
| +
|
| +/**
|
| + * Exposes android.bluetooth.BluetoothManager as necessary for C++
|
| + * device::BluetoothAdapterAndroid, which implements the cross platform
|
| + * device::BluetoothAdapter.
|
| + *
|
| + * Lifetime is controlled by device::BluetoothAdapterAndroid.
|
| + */
|
| +@JNINamespace("device")
|
| +@TargetApi(Build.VERSION_CODES.LOLLIPOP)
|
| +final class ChromeBluetoothManager {
|
| + private static final String TAG = "Bluetooth";
|
| +
|
| + private final Wrappers.BluetoothManagerWrapper mManager;
|
| +
|
| + // Helper class for sending Wrappers.BluetoothDeviceWrapper and string UUIDs pairs
|
| + // over to C++.
|
| + static class DeviceAndUUIDs {
|
| + final String mAddress;
|
| + final Wrappers.BluetoothDeviceWrapper mDevice;
|
| + final String[] mUuids;
|
| +
|
| + DeviceAndUUIDs(Wrappers.BluetoothDeviceWrapper device, String[] uuids) {
|
| + mAddress = device.getAddress();
|
| + mDevice = device;
|
| + mUuids = uuids;
|
| + }
|
| +
|
| + @CalledByNative("DeviceAndUUIDs")
|
| + String getAddress() {
|
| + return mAddress;
|
| + }
|
| +
|
| + // 'Object' type must be used because inner class Wrappers.BluetoothDeviceWrapper reference
|
| + // is not handled by jni_generator.py JavaToJni. http://crbug.com/505554
|
| + @CalledByNative("DeviceAndUUIDs")
|
| + Object getDevice() {
|
| + return mDevice;
|
| + }
|
| +
|
| + @CalledByNative("DeviceAndUUIDs")
|
| + String[] getUuids() {
|
| + return mUuids;
|
| + }
|
| + }
|
| +
|
| + /**
|
| + * Constructs a ChromeBluetoothManager.
|
| + * @param managerWrapper Wraps the default android.bluetooth.BluetoothManager,
|
| + * but may be either null or fake for testing.
|
| + */
|
| + public ChromeBluetoothManager(Wrappers.BluetoothManagerWrapper managerWrapper) {
|
| + mManager = managerWrapper;
|
| + if (mManager == null) {
|
| + Log.i(TAG, "ChromeBluetoothManager created with no managerWrapper.");
|
| + } else {
|
| + Log.i(TAG, "ChromeBluetoothManager created with provided managerWrapper.");
|
| + }
|
| + }
|
| +
|
| + // Wrappers.BluetoothManagerWrapper reference is not handled by jni_generator.py JavaToJni.
|
| + // http://crbug.com/505554
|
| + @CalledByNative
|
| + private static ChromeBluetoothManager create(Object managerWrapper) {
|
| + return new ChromeBluetoothManager((Wrappers.BluetoothManagerWrapper) managerWrapper);
|
| + }
|
| +
|
| + /**
|
| + * Retrieves all GATT Connected devices and their UUIDs.
|
| + * @return An array of DeviceAndUUIDs with all connected devices and their respective
|
| + * UUIDs.
|
| + */
|
| + @CalledByNative
|
| + private DeviceAndUUIDs[] retrieveGattConnectedDevices() {
|
| + List<Wrappers.BluetoothDeviceWrapper> connected_devices =
|
| + mManager.getConnectedDevices(BluetoothProfile.GATT);
|
| + DeviceAndUUIDs[] devices_and_uuids = new DeviceAndUUIDs[connected_devices.size()];
|
| +
|
| + for (int device_idx = 0; device_idx < connected_devices.size(); device_idx++) {
|
| + Wrappers.BluetoothDeviceWrapper device = connected_devices.get(device_idx);
|
| + ParcelUuid[] uuids = device.getUuids();
|
| + String[] uuid_strings = new String[uuids.length];
|
| + for (int uuid_idx = 0; uuid_idx < uuids.length; uuid_idx++) {
|
| + uuid_strings[uuid_idx] = uuids[uuid_idx].toString();
|
| + }
|
| + devices_and_uuids[device_idx] = new DeviceAndUUIDs(device, uuid_strings);
|
| + }
|
| +
|
| + return devices_and_uuids;
|
| + }
|
| +}
|
|
|