OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.device.bluetooth; | 5 package org.chromium.device.bluetooth; |
6 | 6 |
7 import android.Manifest; | 7 import android.Manifest; |
8 import android.annotation.TargetApi; | 8 import android.annotation.TargetApi; |
9 import android.bluetooth.BluetoothAdapter; | 9 import android.bluetooth.BluetoothAdapter; |
10 import android.bluetooth.BluetoothDevice; | 10 import android.bluetooth.BluetoothDevice; |
11 import android.bluetooth.BluetoothGatt; | 11 import android.bluetooth.BluetoothGatt; |
12 import android.bluetooth.BluetoothGattCallback; | 12 import android.bluetooth.BluetoothGattCallback; |
13 import android.bluetooth.BluetoothGattCharacteristic; | 13 import android.bluetooth.BluetoothGattCharacteristic; |
14 import android.bluetooth.BluetoothGattDescriptor; | 14 import android.bluetooth.BluetoothGattDescriptor; |
15 import android.bluetooth.BluetoothGattService; | 15 import android.bluetooth.BluetoothGattService; |
16 import android.bluetooth.le.BluetoothLeScanner; | 16 import android.bluetooth.le.BluetoothLeScanner; |
17 import android.bluetooth.le.ScanCallback; | 17 import android.bluetooth.le.ScanCallback; |
18 import android.bluetooth.le.ScanFilter; | 18 import android.bluetooth.le.ScanFilter; |
19 import android.bluetooth.le.ScanResult; | 19 import android.bluetooth.le.ScanResult; |
20 import android.bluetooth.le.ScanSettings; | 20 import android.bluetooth.le.ScanSettings; |
21 import android.content.BroadcastReceiver; | |
22 import android.content.Context; | 21 import android.content.Context; |
23 import android.content.Intent; | |
24 import android.content.IntentFilter; | |
25 import android.content.pm.PackageManager; | 22 import android.content.pm.PackageManager; |
26 import android.os.Build; | 23 import android.os.Build; |
27 import android.os.ParcelUuid; | 24 import android.os.ParcelUuid; |
28 | 25 |
29 import org.chromium.base.Log; | 26 import org.chromium.base.Log; |
30 import org.chromium.base.annotations.CalledByNative; | 27 import org.chromium.base.annotations.CalledByNative; |
31 import org.chromium.base.annotations.JNINamespace; | 28 import org.chromium.base.annotations.JNINamespace; |
32 import org.chromium.components.location.LocationUtils; | |
33 | 29 |
34 import java.util.ArrayList; | 30 import java.util.ArrayList; |
35 import java.util.HashMap; | 31 import java.util.HashMap; |
36 import java.util.List; | 32 import java.util.List; |
37 import java.util.UUID; | 33 import java.util.UUID; |
38 | 34 |
39 /** | 35 /** |
40 * Wrapper classes around android.bluetooth.* classes that provide an | 36 * Wrapper classes around android.bluetooth.* classes that provide an |
41 * indirection layer enabling fake implementations when running tests. | 37 * indirection layer enabling fake implementations when running tests. |
42 * | 38 * |
43 * Each Wrapper base class accepts an Android API object and passes through | 39 * Each Wrapper base class accepts an Android API object and passes through |
44 * calls to it. When under test, Fake subclasses override all methods that | 40 * calls to it. When under test, Fake subclasses override all methods that |
45 * pass through to the Android object and instead provide fake implementations. | 41 * pass through to the Android object and instead provide fake implementations. |
46 */ | 42 */ |
47 @JNINamespace("device") | 43 @JNINamespace("device") |
48 @TargetApi(Build.VERSION_CODES.LOLLIPOP) | 44 @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
49 class Wrappers { | 45 class Wrappers { |
50 private static final String TAG = "Bluetooth"; | 46 private static final String TAG = "Bluetooth"; |
51 | 47 |
52 public static final int DEVICE_CLASS_UNSPECIFIED = 0x1F00; | 48 public static final int DEVICE_CLASS_UNSPECIFIED = 0x1F00; |
53 | 49 |
54 /** | 50 /** |
55 * Wraps android.bluetooth.BluetoothAdapter. | 51 * Wraps android.bluetooth.BluetoothAdapter. |
56 */ | 52 */ |
57 static class BluetoothAdapterWrapper { | 53 static class BluetoothAdapterWrapper { |
58 private final BluetoothAdapter mAdapter; | 54 private final BluetoothAdapter mAdapter; |
59 protected final ContextWrapper mContext; | 55 protected final Context mContext; |
60 protected BluetoothLeScannerWrapper mScannerWrapper; | 56 protected BluetoothLeScannerWrapper mScannerWrapper; |
61 | 57 |
62 /** | 58 /** |
63 * Creates a BluetoothAdapterWrapper using the default | 59 * Creates a BluetoothAdapterWrapper using the default |
64 * android.bluetooth.BluetoothAdapter. May fail if the default adapter | 60 * android.bluetooth.BluetoothAdapter. May fail if the default adapter |
65 * is not available or if the application does not have sufficient | 61 * is not available or if the application does not have sufficient |
66 * permissions. | 62 * permissions. |
67 */ | 63 */ |
68 @CalledByNative("BluetoothAdapterWrapper") | 64 @CalledByNative("BluetoothAdapterWrapper") |
69 public static BluetoothAdapterWrapper createWithDefaultAdapter(Context c ontext) { | 65 public static BluetoothAdapterWrapper createWithDefaultAdapter(Context c ontext) { |
(...skipping 22 matching lines...) Expand all Loading... | |
92 if (!hasLowEnergyFeature) { | 88 if (!hasLowEnergyFeature) { |
93 Log.i(TAG, "BluetoothAdapterWrapper.create failed: No Low Energy support."); | 89 Log.i(TAG, "BluetoothAdapterWrapper.create failed: No Low Energy support."); |
94 return null; | 90 return null; |
95 } | 91 } |
96 | 92 |
97 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); | 93 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
98 if (adapter == null) { | 94 if (adapter == null) { |
99 Log.i(TAG, "BluetoothAdapterWrapper.create failed: Default adapt er not found."); | 95 Log.i(TAG, "BluetoothAdapterWrapper.create failed: Default adapt er not found."); |
100 return null; | 96 return null; |
101 } else { | 97 } else { |
102 return new BluetoothAdapterWrapper(adapter, new ContextWrapper(c ontext)); | 98 return new BluetoothAdapterWrapper(adapter, context); |
103 } | 99 } |
104 } | 100 } |
105 | 101 |
106 public BluetoothAdapterWrapper(BluetoothAdapter adapter, ContextWrapper context) { | 102 public BluetoothAdapterWrapper(BluetoothAdapter adapter, Context context ) { |
107 mAdapter = adapter; | 103 mAdapter = adapter; |
108 mContext = context; | 104 mContext = context; |
109 } | 105 } |
110 | 106 |
111 public boolean disable() { | 107 public boolean disable() { |
112 return mAdapter.disable(); | 108 return mAdapter.disable(); |
113 } | 109 } |
114 | 110 |
115 public boolean enable() { | 111 public boolean enable() { |
116 return mAdapter.enable(); | 112 return mAdapter.enable(); |
117 } | 113 } |
118 | 114 |
119 public String getAddress() { | 115 public String getAddress() { |
120 return mAdapter.getAddress(); | 116 return mAdapter.getAddress(); |
121 } | 117 } |
122 | 118 |
123 public BluetoothLeScannerWrapper getBluetoothLeScanner() { | 119 public BluetoothLeScannerWrapper getBluetoothLeScanner() { |
124 BluetoothLeScanner scanner = mAdapter.getBluetoothLeScanner(); | 120 BluetoothLeScanner scanner = mAdapter.getBluetoothLeScanner(); |
125 if (scanner == null) { | 121 if (scanner == null) { |
126 return null; | 122 return null; |
127 } | 123 } |
128 if (mScannerWrapper == null) { | 124 if (mScannerWrapper == null) { |
129 mScannerWrapper = new BluetoothLeScannerWrapper(scanner); | 125 mScannerWrapper = new BluetoothLeScannerWrapper(scanner); |
130 } | 126 } |
131 return mScannerWrapper; | 127 return mScannerWrapper; |
132 } | 128 } |
133 | 129 |
134 public ContextWrapper getContext() { | 130 public Context getContext() { |
135 return mContext; | 131 return mContext; |
136 } | 132 } |
137 | 133 |
138 public String getName() { | 134 public String getName() { |
139 return mAdapter.getName(); | 135 return mAdapter.getName(); |
140 } | 136 } |
141 | 137 |
142 public int getScanMode() { | 138 public int getScanMode() { |
143 return mAdapter.getScanMode(); | 139 return mAdapter.getScanMode(); |
144 } | 140 } |
145 | 141 |
146 public boolean isDiscovering() { | 142 public boolean isDiscovering() { |
147 return mAdapter.isDiscovering(); | 143 return mAdapter.isDiscovering(); |
148 } | 144 } |
149 | 145 |
150 public boolean isEnabled() { | 146 public boolean isEnabled() { |
151 return mAdapter.isEnabled(); | 147 return mAdapter.isEnabled(); |
152 } | 148 } |
153 } | 149 } |
154 | 150 |
155 /** | 151 /** |
scheib
2016/06/30 20:52:34
This is just spiffy, thanks for noticing.
| |
156 * Wraps android.content.Context. | |
157 */ | |
158 static class ContextWrapper { | |
159 private final Context mContext; | |
160 | |
161 public ContextWrapper(Context context) { | |
162 mContext = context; | |
163 } | |
164 | |
165 public boolean hasAndroidLocationPermission() { | |
166 return LocationUtils.getInstance().hasAndroidLocationPermission(mCon text); | |
167 } | |
168 | |
169 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { | |
170 return mContext.registerReceiver(receiver, filter); | |
171 } | |
172 | |
173 public void unregisterReceiver(BroadcastReceiver receiver) { | |
174 mContext.unregisterReceiver(receiver); | |
175 } | |
176 } | |
177 | |
178 /** | |
179 * Wraps android.bluetooth.BluetoothLeScanner. | 152 * Wraps android.bluetooth.BluetoothLeScanner. |
180 */ | 153 */ |
181 static class BluetoothLeScannerWrapper { | 154 static class BluetoothLeScannerWrapper { |
182 protected final BluetoothLeScanner mScanner; | 155 protected final BluetoothLeScanner mScanner; |
183 private final HashMap<ScanCallbackWrapper, ForwardScanCallbackToWrapper> mCallbacks; | 156 private final HashMap<ScanCallbackWrapper, ForwardScanCallbackToWrapper> mCallbacks; |
184 | 157 |
185 public BluetoothLeScannerWrapper(BluetoothLeScanner scanner) { | 158 public BluetoothLeScannerWrapper(BluetoothLeScanner scanner) { |
186 mScanner = scanner; | 159 mScanner = scanner; |
187 mCallbacks = new HashMap<ScanCallbackWrapper, ForwardScanCallbackToW rapper>(); | 160 mCallbacks = new HashMap<ScanCallbackWrapper, ForwardScanCallbackToW rapper>(); |
188 } | 161 } |
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
579 | 552 |
580 public byte[] getValue() { | 553 public byte[] getValue() { |
581 return mDescriptor.getValue(); | 554 return mDescriptor.getValue(); |
582 } | 555 } |
583 | 556 |
584 public boolean setValue(byte[] value) { | 557 public boolean setValue(byte[] value) { |
585 return mDescriptor.setValue(value); | 558 return mDescriptor.setValue(value); |
586 } | 559 } |
587 } | 560 } |
588 } | 561 } |
OLD | NEW |