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.SuppressLint; | 8 import android.annotation.SuppressLint; |
9 import android.annotation.TargetApi; | 9 import android.annotation.TargetApi; |
10 import android.bluetooth.BluetoothAdapter; | 10 import android.bluetooth.BluetoothAdapter; |
11 import android.bluetooth.BluetoothDevice; | 11 import android.bluetooth.BluetoothDevice; |
12 import android.bluetooth.BluetoothGatt; | 12 import android.bluetooth.BluetoothGatt; |
13 import android.bluetooth.BluetoothGattCallback; | 13 import android.bluetooth.BluetoothGattCallback; |
14 import android.bluetooth.BluetoothGattCharacteristic; | 14 import android.bluetooth.BluetoothGattCharacteristic; |
15 import android.bluetooth.BluetoothGattDescriptor; | 15 import android.bluetooth.BluetoothGattDescriptor; |
16 import android.bluetooth.BluetoothGattService; | 16 import android.bluetooth.BluetoothGattService; |
17 import android.bluetooth.le.BluetoothLeScanner; | 17 import android.bluetooth.le.BluetoothLeScanner; |
18 import android.bluetooth.le.ScanCallback; | 18 import android.bluetooth.le.ScanCallback; |
19 import android.bluetooth.le.ScanFilter; | 19 import android.bluetooth.le.ScanFilter; |
20 import android.bluetooth.le.ScanResult; | 20 import android.bluetooth.le.ScanResult; |
21 import android.bluetooth.le.ScanSettings; | 21 import android.bluetooth.le.ScanSettings; |
22 import android.content.Context; | 22 import android.content.Context; |
23 import android.content.pm.PackageManager; | 23 import android.content.pm.PackageManager; |
24 import android.os.Build; | 24 import android.os.Build; |
25 import android.os.ParcelUuid; | 25 import android.os.ParcelUuid; |
26 | 26 |
| 27 import org.chromium.base.ContextUtils; |
27 import org.chromium.base.Log; | 28 import org.chromium.base.Log; |
28 import org.chromium.base.ThreadUtils; | 29 import org.chromium.base.ThreadUtils; |
29 import org.chromium.base.annotations.CalledByNative; | 30 import org.chromium.base.annotations.CalledByNative; |
30 import org.chromium.base.annotations.JNINamespace; | 31 import org.chromium.base.annotations.JNINamespace; |
31 | 32 |
32 import java.util.ArrayList; | 33 import java.util.ArrayList; |
33 import java.util.HashMap; | 34 import java.util.HashMap; |
34 import java.util.List; | 35 import java.util.List; |
35 import java.util.UUID; | 36 import java.util.UUID; |
36 | 37 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 protected final Context mContext; | 104 protected final Context mContext; |
104 protected BluetoothLeScannerWrapper mScannerWrapper; | 105 protected BluetoothLeScannerWrapper mScannerWrapper; |
105 | 106 |
106 /** | 107 /** |
107 * Creates a BluetoothAdapterWrapper using the default | 108 * Creates a BluetoothAdapterWrapper using the default |
108 * android.bluetooth.BluetoothAdapter. May fail if the default adapter | 109 * android.bluetooth.BluetoothAdapter. May fail if the default adapter |
109 * is not available or if the application does not have sufficient | 110 * is not available or if the application does not have sufficient |
110 * permissions. | 111 * permissions. |
111 */ | 112 */ |
112 @CalledByNative("BluetoothAdapterWrapper") | 113 @CalledByNative("BluetoothAdapterWrapper") |
113 public static BluetoothAdapterWrapper createWithDefaultAdapter(Context c
ontext) { | 114 public static BluetoothAdapterWrapper createWithDefaultAdapter() { |
114 final boolean hasMinAPI = Build.VERSION.SDK_INT >= Build.VERSION_COD
ES.M; | 115 final boolean hasMinAPI = Build.VERSION.SDK_INT >= Build.VERSION_COD
ES.M; |
115 if (!hasMinAPI) { | 116 if (!hasMinAPI) { |
116 Log.i(TAG, "BluetoothAdapterWrapper.create failed: SDK version (
%d) too low.", | 117 Log.i(TAG, "BluetoothAdapterWrapper.create failed: SDK version (
%d) too low.", |
117 Build.VERSION.SDK_INT); | 118 Build.VERSION.SDK_INT); |
118 return null; | 119 return null; |
119 } | 120 } |
120 | 121 |
121 final boolean hasPermissions = | 122 final boolean hasPermissions = |
122 context.checkCallingOrSelfPermission(Manifest.permission.BLU
ETOOTH) | 123 ContextUtils.getApplicationContext().checkCallingOrSelfPermi
ssion( |
| 124 Manifest.permission.BLUETOOTH) |
123 == PackageManager.PERMISSION_GRANTED | 125 == PackageManager.PERMISSION_GRANTED |
124 && context.checkCallingOrSelfPermission(Manifest.permission.
BLUETOOTH_ADMIN) | 126 && ContextUtils.getApplicationContext().checkCallingOrSelfPe
rmission( |
| 127 Manifest.permission.BLUETOOTH_ADMIN) |
125 == PackageManager.PERMISSION_GRANTED; | 128 == PackageManager.PERMISSION_GRANTED; |
126 if (!hasPermissions) { | 129 if (!hasPermissions) { |
127 Log.w(TAG, "BluetoothAdapterWrapper.create failed: Lacking Bluet
ooth permissions."); | 130 Log.w(TAG, "BluetoothAdapterWrapper.create failed: Lacking Bluet
ooth permissions."); |
128 return null; | 131 return null; |
129 } | 132 } |
130 | 133 |
131 // Only Low Energy currently supported, see BluetoothAdapterAndroid
class note. | 134 // Only Low Energy currently supported, see BluetoothAdapterAndroid
class note. |
132 final boolean hasLowEnergyFeature = | 135 final boolean hasLowEnergyFeature = |
133 Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 | 136 Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 |
134 && context.getPackageManager().hasSystemFeature( | 137 && ContextUtils.getApplicationContext().getPackageManager().
hasSystemFeature( |
135 PackageManager.FEATURE_BLUETOOTH_LE); | 138 PackageManager.FEATURE_BLUETOOTH_LE); |
136 if (!hasLowEnergyFeature) { | 139 if (!hasLowEnergyFeature) { |
137 Log.i(TAG, "BluetoothAdapterWrapper.create failed: No Low Energy
support."); | 140 Log.i(TAG, "BluetoothAdapterWrapper.create failed: No Low Energy
support."); |
138 return null; | 141 return null; |
139 } | 142 } |
140 | 143 |
141 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); | 144 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
142 if (adapter == null) { | 145 if (adapter == null) { |
143 Log.i(TAG, "BluetoothAdapterWrapper.create failed: Default adapt
er not found."); | 146 Log.i(TAG, "BluetoothAdapterWrapper.create failed: Default adapt
er not found."); |
144 return null; | 147 return null; |
145 } else { | 148 } else { |
146 return new BluetoothAdapterWrapper(adapter, context); | 149 return new BluetoothAdapterWrapper(adapter, ContextUtils.getAppl
icationContext()); |
147 } | 150 } |
148 } | 151 } |
149 | 152 |
150 public BluetoothAdapterWrapper(BluetoothAdapter adapter, Context context
) { | 153 public BluetoothAdapterWrapper(BluetoothAdapter adapter, Context context
) { |
151 mAdapter = adapter; | 154 mAdapter = adapter; |
152 mContext = context; | 155 mContext = context; |
153 } | 156 } |
154 | 157 |
155 public boolean disable() { | 158 public boolean disable() { |
156 return mAdapter.disable(); | 159 return mAdapter.disable(); |
(...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
609 | 612 |
610 public byte[] getValue() { | 613 public byte[] getValue() { |
611 return mDescriptor.getValue(); | 614 return mDescriptor.getValue(); |
612 } | 615 } |
613 | 616 |
614 public boolean setValue(byte[] value) { | 617 public boolean setValue(byte[] value) { |
615 return mDescriptor.setValue(value); | 618 return mDescriptor.setValue(value); |
616 } | 619 } |
617 } | 620 } |
618 } | 621 } |
OLD | NEW |