| Index: device/bluetooth/bluetooth_adapter_android.cc
|
| diff --git a/device/bluetooth/bluetooth_adapter_android.cc b/device/bluetooth/bluetooth_adapter_android.cc
|
| index 41228bb635bae3ac24c26c8b50bb22d539a22013..2590c24b38274590f87900ce8a5216a6f5ea372f 100644
|
| --- a/device/bluetooth/bluetooth_adapter_android.cc
|
| +++ b/device/bluetooth/bluetooth_adapter_android.cc
|
| @@ -17,8 +17,10 @@
|
| #include "device/bluetooth/android/wrappers.h"
|
| #include "device/bluetooth/bluetooth_advertisement.h"
|
| #include "device/bluetooth/bluetooth_device_android.h"
|
| +#include "device/bluetooth/bluetooth_discovery_filter.h"
|
| #include "device/bluetooth/bluetooth_discovery_session_outcome.h"
|
| #include "jni/ChromeBluetoothAdapter_jni.h"
|
| +#include "jni/ChromeBluetoothManager_jni.h"
|
|
|
| using base::android::AttachCurrentThread;
|
| using base::android::ConvertJavaStringToUTF8;
|
| @@ -26,6 +28,8 @@ using base::android::AppendJavaStringArrayToStringVector;
|
| using base::android::JavaParamRef;
|
| using base::android::JavaRef;
|
|
|
| +namespace device {
|
| +
|
| namespace {
|
| // The poll interval in ms when there is no active discovery. This
|
| // matches the max allowed advertisting interval for connectable
|
| @@ -35,27 +39,46 @@ enum { kPassivePollInterval = 11000 };
|
| enum { kActivePollInterval = 1000 };
|
| // The delay in ms to wait before purging devices when a scan starts.
|
| enum { kPurgeDelay = 500 };
|
| +
|
| +BluetoothDevice::UUIDList ConvertJavaStringArrayToUUIDList(
|
| + JNIEnv* env,
|
| + const JavaRef<jobjectArray>& j_uuids) {
|
| + std::vector<std::string> uuids_strings;
|
| + AppendJavaStringArrayToStringVector(env, j_uuids.obj(), &uuids_strings);
|
| +
|
| + BluetoothDevice::UUIDList uuids;
|
| + for (std::string& uuid : uuids_strings) {
|
| + uuids.push_back(BluetoothUUID(std::move(uuid)));
|
| + }
|
| +
|
| + return uuids;
|
| }
|
|
|
| -namespace device {
|
| +} // namespace
|
|
|
| // static
|
| base::WeakPtr<BluetoothAdapter> BluetoothAdapter::CreateAdapter(
|
| const InitCallback& init_callback) {
|
| return BluetoothAdapterAndroid::Create(
|
| - BluetoothAdapterWrapper_CreateWithDefaultAdapter());
|
| + BluetoothAdapterWrapper_CreateWithDefaultAdapter(),
|
| + BluetoothManagerWrapper_CreateWithDefaultManager());
|
| }
|
|
|
| // static
|
| base::WeakPtr<BluetoothAdapterAndroid> BluetoothAdapterAndroid::Create(
|
| const JavaRef<jobject>&
|
| - bluetooth_adapter_wrapper) { // Java Type: bluetoothAdapterWrapper
|
| + bluetooth_adapter_wrapper, // Java Type: BluetoothAdapterWrapper
|
| + const JavaRef<jobject>&
|
| + bluetooth_manager_wrapper) { // Java Type: BluetoothManagerWrapper
|
| BluetoothAdapterAndroid* adapter = new BluetoothAdapterAndroid();
|
|
|
| adapter->j_adapter_.Reset(Java_ChromeBluetoothAdapter_create(
|
| AttachCurrentThread(), reinterpret_cast<intptr_t>(adapter),
|
| bluetooth_adapter_wrapper));
|
|
|
| + adapter->j_manager_.Reset(Java_ChromeBluetoothManager_create(
|
| + AttachCurrentThread(), bluetooth_manager_wrapper));
|
| +
|
| adapter->ui_task_runner_ = base::ThreadTaskRunnerHandle::Get();
|
|
|
| return adapter->weak_ptr_factory_.GetWeakPtr();
|
| @@ -124,6 +147,71 @@ bool BluetoothAdapterAndroid::IsDiscovering() const {
|
| j_adapter_);
|
| }
|
|
|
| +std::unordered_map<BluetoothDevice*, BluetoothDevice::UUIDSet>
|
| +BluetoothAdapterAndroid::RetrieveGattConnectedDevicesWithDiscoveryFilter(
|
| + const BluetoothDiscoveryFilter& discovery_filter) {
|
| + JNIEnv* env = AttachCurrentThread();
|
| + ScopedJavaLocalRef<jobjectArray> j_devices_and_uuids =
|
| + Java_ChromeBluetoothManager_retrieveGattConnectedDevices(env, j_manager_);
|
| +
|
| + std::unordered_map<BluetoothDevice*, BluetoothDevice::UUIDSet>
|
| + connected_devices;
|
| + std::set<BluetoothUUID> filter_uuids;
|
| + discovery_filter.GetUUIDs(filter_uuids);
|
| +
|
| + jsize size = env->GetArrayLength(j_devices_and_uuids.obj());
|
| + for (jsize i = 0; i < size; i++) {
|
| + ScopedJavaLocalRef<jobject> j_device_and_uuids(
|
| + env, env->GetObjectArrayElement(j_devices_and_uuids.obj(), i));
|
| +
|
| + ScopedJavaLocalRef<jobject> j_device =
|
| + Java_DeviceAndUUIDs_getDevice(env, j_device_and_uuids.obj());
|
| +
|
| + std::string address = ConvertJavaStringToUTF8(
|
| + Java_DeviceAndUUIDs_getAddress(env, j_device_and_uuids.obj()));
|
| +
|
| + std::unique_ptr<BluetoothDeviceAndroid> device;
|
| + BluetoothDeviceAndroid* device_ptr = nullptr;
|
| +
|
| + auto iter = devices_.find(address);
|
| + const bool new_device = iter == devices_.end();
|
| + if (new_device) {
|
| + device.reset(BluetoothDeviceAndroid::Create(this, j_device));
|
| + device_ptr = device.get();
|
| + } else {
|
| + device_ptr = static_cast<BluetoothDeviceAndroid*>(iter->second);
|
| + }
|
| +
|
| + DCHECK(device_ptr);
|
| +
|
| + ScopedJavaLocalRef<jobjectArray> j_device_uuids =
|
| + Java_DeviceAndUUIDs_getUuids(env, j_device_and_uuids.obj());
|
| +
|
| + BluetoothDevice::UUIDList device_uuids_list =
|
| + ConvertJavaStringArrayToUUIDList(env, j_device_uuids);
|
| + BluetoothDevice::UUIDSet intersection;
|
| + for (BluetoothUUID& uuid : device_uuids_list) {
|
| + if (base::ContainsKey(filter_uuids, uuid)) {
|
| + intersection.insert(uuid);
|
| + }
|
| + }
|
| +
|
| + if (!filter_uuids.empty() && intersection.empty()) {
|
| + continue;
|
| + }
|
| +
|
| + connected_devices[device_ptr] = std::move(intersection);
|
| +
|
| + if (new_device) {
|
| + devices_.add(device_ptr->GetAddress(), std::move(device));
|
| + for (auto& observer : observers_)
|
| + observer.DeviceAdded(this, device_ptr);
|
| + }
|
| + }
|
| +
|
| + return connected_devices;
|
| +}
|
| +
|
| BluetoothAdapter::UUIDList BluetoothAdapterAndroid::GetUUIDs() const {
|
| NOTIMPLEMENTED();
|
| return UUIDList();
|
| @@ -201,13 +289,8 @@ void BluetoothAdapterAndroid::CreateOrUpdateDeviceOnScan(
|
| }
|
| DCHECK(device_android);
|
|
|
| - std::vector<std::string> advertised_uuids_strings;
|
| - AppendJavaStringArrayToStringVector(env, advertised_uuids,
|
| - &advertised_uuids_strings);
|
| - BluetoothDevice::UUIDList advertised_bluetooth_uuids;
|
| - for (std::string& uuid : advertised_uuids_strings) {
|
| - advertised_bluetooth_uuids.push_back(BluetoothUUID(std::move(uuid)));
|
| - }
|
| + BluetoothDevice::UUIDList advertised_bluetooth_uuids =
|
| + ConvertJavaStringArrayToUUIDList(env, advertised_uuids);
|
|
|
| int8_t clamped_tx_power = BluetoothDevice::ClampPower(tx_power);
|
|
|
|
|