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 boolean disable() { |
| 109 return mAdapter.disable(); |
| 110 } |
| 111 |
| 112 public boolean enable() { |
| 113 return mAdapter.enable(); |
| 114 } |
| 115 |
| 116 public String getAddress() { |
| 117 return mAdapter.getAddress(); |
| 118 } |
| 119 |
| 120 public BluetoothLeScannerWrapper getBluetoothLeScanner() { |
| 121 BluetoothLeScanner scanner = mAdapter.getBluetoothLeScanner(); |
| 122 if (scanner == null) { |
| 123 return null; |
| 124 } |
| 125 if (mScannerWrapper == null) { |
| 126 mScannerWrapper = new BluetoothLeScannerWrapper(scanner); |
| 127 } |
| 128 return mScannerWrapper; |
107 } | 129 } |
108 | 130 |
109 public ContextWrapper getContext() { | 131 public ContextWrapper getContext() { |
110 return mContext; | 132 return mContext; |
111 } | 133 } |
112 | 134 |
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() { | 135 public String getName() { |
126 return mAdapter.getName(); | 136 return mAdapter.getName(); |
127 } | 137 } |
128 | 138 |
129 public int getScanMode() { | 139 public int getScanMode() { |
130 return mAdapter.getScanMode(); | 140 return mAdapter.getScanMode(); |
131 } | 141 } |
132 | 142 |
133 public boolean isDiscovering() { | 143 public boolean isDiscovering() { |
134 return mAdapter.isDiscovering(); | 144 return mAdapter.isDiscovering(); |
135 } | 145 } |
| 146 |
| 147 public boolean isEnabled() { |
| 148 return mAdapter.isEnabled(); |
| 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 = |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 mDevice.connectGatt(context, autoConnect, | 283 mDevice.connectGatt(context, autoConnect, |
270 new ForwardBluetoothGattCallbackToWrapper(callback,
this)), | 284 new ForwardBluetoothGattCallbackToWrapper(callback,
this)), |
271 this); | 285 this); |
272 } | 286 } |
273 | 287 |
274 public String getAddress() { | 288 public String getAddress() { |
275 return mDevice.getAddress(); | 289 return mDevice.getAddress(); |
276 } | 290 } |
277 | 291 |
278 public int getBluetoothClass_getDeviceClass() { | 292 public int getBluetoothClass_getDeviceClass() { |
| 293 if (mDevice == null || mDevice.getBluetoothClass() == null) { |
| 294 // BluetoothDevice.getBluetoothClass() returns null if adapter h
as been powered off. |
| 295 // Return DEVICE_CLASS_UNSPECIFIED in these cases. |
| 296 return DEVICE_CLASS_UNSPECIFIED; |
| 297 } |
279 return mDevice.getBluetoothClass().getDeviceClass(); | 298 return mDevice.getBluetoothClass().getDeviceClass(); |
280 } | 299 } |
281 | 300 |
282 public int getBondState() { | 301 public int getBondState() { |
283 return mDevice.getBondState(); | 302 return mDevice.getBondState(); |
284 } | 303 } |
285 | 304 |
286 public String getName() { | 305 public String getName() { |
287 return mDevice.getName(); | 306 return mDevice.getName(); |
288 } | 307 } |
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
536 | 555 |
537 public byte[] getValue() { | 556 public byte[] getValue() { |
538 return mDescriptor.getValue(); | 557 return mDescriptor.getValue(); |
539 } | 558 } |
540 | 559 |
541 public boolean setValue(byte[] value) { | 560 public boolean setValue(byte[] value) { |
542 return mDescriptor.setValue(value); | 561 return mDescriptor.setValue(value); |
543 } | 562 } |
544 } | 563 } |
545 } | 564 } |
OLD | NEW |