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; |
(...skipping 28 matching lines...) Expand all Loading... | |
39 * | 39 * |
40 * Each Wrapper base class accepts an Android API object and passes through | 40 * Each Wrapper base class accepts an Android API object and passes through |
41 * calls to it. When under test, Fake subclasses override all methods that | 41 * calls to it. When under test, Fake subclasses override all methods that |
42 * pass through to the Android object and instead provide fake implementations. | 42 * pass through to the Android object and instead provide fake implementations. |
43 */ | 43 */ |
44 @JNINamespace("device") | 44 @JNINamespace("device") |
45 @TargetApi(Build.VERSION_CODES.LOLLIPOP) | 45 @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
46 class Wrappers { | 46 class Wrappers { |
47 private static final String TAG = "Bluetooth"; | 47 private static final String TAG = "Bluetooth"; |
48 | 48 |
49 public static final int DEVICE_CLASS_UNSPECIFIED = 0x1F00; | |
50 | |
49 /** | 51 /** |
50 * Wraps android.bluetooth.BluetoothAdapter. | 52 * Wraps android.bluetooth.BluetoothAdapter. |
51 */ | 53 */ |
52 static class BluetoothAdapterWrapper { | 54 static class BluetoothAdapterWrapper { |
53 private final BluetoothAdapter mAdapter; | 55 private final BluetoothAdapter mAdapter; |
54 protected final ContextWrapper mContext; | 56 protected final ContextWrapper mContext; |
55 protected final BluetoothLeScannerWrapper mScanner; | 57 protected BluetoothLeScannerWrapper mScannerWrapper; |
56 | 58 |
57 /** | 59 /** |
58 * Creates a BluetoothAdapterWrapper using the default | 60 * Creates a BluetoothAdapterWrapper using the default |
59 * android.bluetooth.BluetoothAdapter. May fail if the default adapter | 61 * android.bluetooth.BluetoothAdapter. May fail if the default adapter |
60 * is not available or if the application does not have sufficient | 62 * is not available or if the application does not have sufficient |
61 * permissions. | 63 * permissions. |
62 */ | 64 */ |
63 @CalledByNative("BluetoothAdapterWrapper") | 65 @CalledByNative("BluetoothAdapterWrapper") |
64 public static BluetoothAdapterWrapper createWithDefaultAdapter(Context c ontext) { | 66 public static BluetoothAdapterWrapper createWithDefaultAdapter(Context c ontext) { |
65 final boolean hasMinAPI = Build.VERSION.SDK_INT >= Build.VERSION_COD ES.LOLLIPOP; | 67 final boolean hasMinAPI = Build.VERSION.SDK_INT >= Build.VERSION_COD ES.LOLLIPOP; |
(...skipping 21 matching lines...) Expand all Loading... | |
87 if (!hasLowEnergyFeature) { | 89 if (!hasLowEnergyFeature) { |
88 Log.i(TAG, "BluetoothAdapterWrapper.create failed: No Low Energy support."); | 90 Log.i(TAG, "BluetoothAdapterWrapper.create failed: No Low Energy support."); |
89 return null; | 91 return null; |
90 } | 92 } |
91 | 93 |
92 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); | 94 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
93 if (adapter == null) { | 95 if (adapter == null) { |
94 Log.i(TAG, "BluetoothAdapterWrapper.create failed: Default adapt er not found."); | 96 Log.i(TAG, "BluetoothAdapterWrapper.create failed: Default adapt er not found."); |
95 return null; | 97 return null; |
96 } else { | 98 } else { |
97 return new BluetoothAdapterWrapper(adapter, new ContextWrapper(c ontext), | 99 return new BluetoothAdapterWrapper(adapter, new ContextWrapper(c ontext)); |
98 new BluetoothLeScannerWrapper(adapter.getBluetoothLeScan ner())); | |
99 } | 100 } |
100 } | 101 } |
101 | 102 |
102 public BluetoothAdapterWrapper(BluetoothAdapter adapter, ContextWrapper context, | 103 public BluetoothAdapterWrapper(BluetoothAdapter adapter, ContextWrapper context) { |
103 BluetoothLeScannerWrapper scanner) { | |
104 mAdapter = adapter; | 104 mAdapter = adapter; |
105 mContext = context; | 105 mContext = context; |
106 mScanner = scanner; | 106 } |
107 | |
108 public String getAddress() { | |
109 return mAdapter.getAddress(); | |
110 } | |
111 | |
112 public BluetoothLeScannerWrapper getBluetoothLeScanner() { | |
113 BluetoothLeScanner scanner = mAdapter.getBluetoothLeScanner(); | |
114 if (scanner == null) { | |
115 return null; | |
116 } | |
117 if (mScannerWrapper == null) { | |
118 mScannerWrapper = new BluetoothLeScannerWrapper(scanner); | |
119 } | |
120 return mScannerWrapper; | |
107 } | 121 } |
108 | 122 |
109 public ContextWrapper getContext() { | 123 public ContextWrapper getContext() { |
110 return mContext; | 124 return mContext; |
111 } | 125 } |
112 | 126 |
113 public BluetoothLeScannerWrapper getBluetoothLeScanner() { | |
114 return mScanner; | |
115 } | |
116 | |
117 public boolean isEnabled() { | |
118 return mAdapter.isEnabled(); | |
119 } | |
120 | |
121 public String getAddress() { | |
122 return mAdapter.getAddress(); | |
123 } | |
124 | |
125 public String getName() { | 127 public String getName() { |
126 return mAdapter.getName(); | 128 return mAdapter.getName(); |
127 } | 129 } |
128 | 130 |
129 public int getScanMode() { | 131 public int getScanMode() { |
130 return mAdapter.getScanMode(); | 132 return mAdapter.getScanMode(); |
131 } | 133 } |
132 | 134 |
133 public boolean isDiscovering() { | 135 public boolean isDiscovering() { |
134 return mAdapter.isDiscovering(); | 136 return mAdapter.isDiscovering(); |
135 } | 137 } |
138 | |
139 public boolean isEnabled() { | |
140 return mAdapter.isEnabled(); | |
141 } | |
142 | |
143 public boolean setPowered(boolean powered) { | |
scheib
2016/02/05 18:06:19
The wrapper should mimic the Android API as closel
perja
2016/02/08 14:48:31
Done.
| |
144 if (powered) { | |
145 return mAdapter.enable(); | |
146 } else { | |
147 return mAdapter.disable(); | |
148 } | |
149 } | |
136 } | 150 } |
137 | 151 |
138 /** | 152 /** |
139 * Wraps android.content.Context. | 153 * Wraps android.content.Context. |
140 */ | 154 */ |
141 static class ContextWrapper { | 155 static class ContextWrapper { |
142 private final Context mContext; | 156 private final Context mContext; |
143 | 157 |
144 public ContextWrapper(Context context) { | 158 public ContextWrapper(Context context) { |
145 mContext = context; | 159 mContext = context; |
146 } | 160 } |
147 | 161 |
148 public boolean checkPermission(String permission) { | 162 public boolean checkPermission(String permission) { |
149 return mContext.checkPermission(permission, Process.myPid(), Process .myUid()) | 163 return mContext.checkPermission(permission, Process.myPid(), Process .myUid()) |
150 == PackageManager.PERMISSION_GRANTED; | 164 == PackageManager.PERMISSION_GRANTED; |
151 } | 165 } |
152 } | 166 } |
153 | 167 |
154 /** | 168 /** |
155 * Wraps android.bluetooth.BluetoothLeScanner. | 169 * Wraps android.bluetooth.BluetoothLeScanner. |
156 */ | 170 */ |
157 static class BluetoothLeScannerWrapper { | 171 static class BluetoothLeScannerWrapper { |
158 private final BluetoothLeScanner mScanner; | 172 protected final BluetoothLeScanner mScanner; |
159 private final HashMap<ScanCallbackWrapper, ForwardScanCallbackToWrapper> mCallbacks; | 173 private final HashMap<ScanCallbackWrapper, ForwardScanCallbackToWrapper> mCallbacks; |
160 | 174 |
161 public BluetoothLeScannerWrapper(BluetoothLeScanner scanner) { | 175 public BluetoothLeScannerWrapper(BluetoothLeScanner scanner) { |
162 mScanner = scanner; | 176 mScanner = scanner; |
163 mCallbacks = new HashMap<ScanCallbackWrapper, ForwardScanCallbackToW rapper>(); | 177 mCallbacks = new HashMap<ScanCallbackWrapper, ForwardScanCallbackToW rapper>(); |
164 } | 178 } |
165 | 179 |
166 public void startScan( | 180 public void startScan( |
167 List<ScanFilter> filters, int scanSettingsScanMode, ScanCallback Wrapper callback) { | 181 List<ScanFilter> filters, int scanSettingsScanMode, ScanCallback Wrapper callback) { |
168 ScanSettings settings = | 182 ScanSettings settings = |
169 new ScanSettings.Builder().setScanMode(scanSettingsScanMode) .build(); | 183 new ScanSettings.Builder().setScanMode(scanSettingsScanMode) .build(); |
170 | 184 |
171 ForwardScanCallbackToWrapper callbackForwarder = | 185 ForwardScanCallbackToWrapper callbackForwarder = |
172 new ForwardScanCallbackToWrapper(callback); | 186 new ForwardScanCallbackToWrapper(callback); |
173 mCallbacks.put(callback, callbackForwarder); | 187 mCallbacks.put(callback, callbackForwarder); |
174 | |
175 mScanner.startScan(filters, settings, callbackForwarder); | 188 mScanner.startScan(filters, settings, callbackForwarder); |
scheib
2016/02/05 18:06:19
Leave the blank line.
perja
2016/02/08 14:48:31
Done.
| |
176 } | 189 } |
177 | 190 |
178 public void stopScan(ScanCallbackWrapper callback) { | 191 public void stopScan(ScanCallbackWrapper callback) { |
179 ForwardScanCallbackToWrapper callbackForwarder = mCallbacks.remove(c allback); | 192 ForwardScanCallbackToWrapper callbackForwarder = mCallbacks.remove(c allback); |
180 mScanner.stopScan(callbackForwarder); | 193 mScanner.stopScan(callbackForwarder); |
181 } | 194 } |
182 } | 195 } |
183 | 196 |
184 /** | 197 /** |
185 * Implements android.bluetooth.le.ScanCallback and forwards calls through t o a | 198 * Implements android.bluetooth.le.ScanCallback and forwards calls through t o a |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
269 mDevice.connectGatt(context, autoConnect, | 282 mDevice.connectGatt(context, autoConnect, |
270 new ForwardBluetoothGattCallbackToWrapper(callback, this)), | 283 new ForwardBluetoothGattCallbackToWrapper(callback, this)), |
271 this); | 284 this); |
272 } | 285 } |
273 | 286 |
274 public String getAddress() { | 287 public String getAddress() { |
275 return mDevice.getAddress(); | 288 return mDevice.getAddress(); |
276 } | 289 } |
277 | 290 |
278 public int getBluetoothClass_getDeviceClass() { | 291 public int getBluetoothClass_getDeviceClass() { |
292 if (mDevice == null || mDevice.getBluetoothClass() == null) { | |
293 // BluetoothDevice.getBluetoothClass() returns null if adapter h as been powered off. | |
294 // Return DEVICE_CLASS_UNSPECIFIED in these cases. | |
295 return DEVICE_CLASS_UNSPECIFIED; | |
296 } | |
279 return mDevice.getBluetoothClass().getDeviceClass(); | 297 return mDevice.getBluetoothClass().getDeviceClass(); |
280 } | 298 } |
281 | 299 |
282 public int getBondState() { | 300 public int getBondState() { |
283 return mDevice.getBondState(); | 301 return mDevice.getBondState(); |
284 } | 302 } |
285 | 303 |
286 public String getName() { | 304 public String getName() { |
287 return mDevice.getName(); | 305 return mDevice.getName(); |
288 } | 306 } |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
532 | 550 |
533 public byte[] getValue() { | 551 public byte[] getValue() { |
534 return mDescriptor.getValue(); | 552 return mDescriptor.getValue(); |
535 } | 553 } |
536 | 554 |
537 public boolean setValue(byte[] value) { | 555 public boolean setValue(byte[] value) { |
538 return mDescriptor.setValue(value); | 556 return mDescriptor.setValue(value); |
539 } | 557 } |
540 } | 558 } |
541 } | 559 } |
OLD | NEW |