Merge "Separate LE scanner from GATT client (1/4)" am: 87c997ca21 am: 81d5ecb7a6
am: 61d0088923
Change-Id: I83c82b17064ee958fff6a98d90a0b351205ccf54
This commit is contained in:
@@ -127,6 +127,7 @@ LOCAL_SRC_FILES += \
|
||||
core/java/android/bluetooth/IBluetoothGattCallback.aidl \
|
||||
core/java/android/bluetooth/IBluetoothGattServerCallback.aidl \
|
||||
core/java/android/bluetooth/le/IAdvertiserCallback.aidl \
|
||||
core/java/android/bluetooth/le/IScannerCallback.aidl \
|
||||
core/java/android/content/IClipboard.aidl \
|
||||
core/java/android/content/IContentService.aidl \
|
||||
core/java/android/content/IIntentReceiver.aidl \
|
||||
|
||||
@@ -135,7 +135,7 @@ public final class BluetoothGatt implements BluetoothProfile {
|
||||
* Bluetooth GATT callbacks. Overrides the default BluetoothGattCallback implementation.
|
||||
*/
|
||||
private final IBluetoothGattCallback mBluetoothGattCallback =
|
||||
new BluetoothGattCallbackWrapper() {
|
||||
new IBluetoothGattCallback.Stub() {
|
||||
/**
|
||||
* Application interface registered - app is ready to go
|
||||
* @hide
|
||||
|
||||
@@ -1,96 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2014 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package android.bluetooth;
|
||||
|
||||
import android.bluetooth.le.AdvertiseSettings;
|
||||
import android.bluetooth.le.ScanResult;
|
||||
import android.bluetooth.BluetoothGattService;
|
||||
import android.os.ParcelUuid;
|
||||
import android.os.RemoteException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Wrapper class for default implementation of IBluetoothGattCallback.
|
||||
*
|
||||
* @hide
|
||||
*/
|
||||
public class BluetoothGattCallbackWrapper extends IBluetoothGattCallback.Stub {
|
||||
|
||||
@Override
|
||||
public void onClientRegistered(int status, int clientIf) throws RemoteException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClientConnectionState(int status, int clientIf, boolean connected, String address)
|
||||
throws RemoteException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onScanResult(ScanResult scanResult) throws RemoteException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBatchScanResults(List<ScanResult> batchResults) throws RemoteException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSearchComplete(String address, List<BluetoothGattService> services,
|
||||
int status) throws RemoteException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCharacteristicRead(String address, int status, int handle, byte[] value)
|
||||
throws RemoteException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCharacteristicWrite(String address, int status, int handle) throws RemoteException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onExecuteWrite(String address, int status) throws RemoteException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDescriptorRead(String address, int status, int handle, byte[] value) throws RemoteException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDescriptorWrite(String address, int status, int handle) throws RemoteException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNotify(String address, int handle, byte[] value) throws RemoteException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReadRemoteRssi(String address, int rssi, int status) throws RemoteException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConfigureMTU(String address, int mtu, int status) throws RemoteException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFoundOrLost(boolean onFound, ScanResult scanResult) throws RemoteException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onScanManagerErrorCallback(int errorCode) throws RemoteException {
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,7 @@ import android.os.WorkSource;
|
||||
import android.bluetooth.IBluetoothGattCallback;
|
||||
import android.bluetooth.IBluetoothGattServerCallback;
|
||||
import android.bluetooth.le.IAdvertiserCallback;
|
||||
import android.bluetooth.le.IScannerCallback;
|
||||
|
||||
/**
|
||||
* API for interacting with BLE / GATT
|
||||
@@ -37,11 +38,12 @@ import android.bluetooth.le.IAdvertiserCallback;
|
||||
interface IBluetoothGatt {
|
||||
List<BluetoothDevice> getDevicesMatchingConnectionStates(in int[] states);
|
||||
|
||||
void startScan(in int appIf, in boolean isServer, in ScanSettings settings,
|
||||
in List<ScanFilter> filters, in WorkSource workSource, in List scanStorages,
|
||||
in String callingPackage);
|
||||
void stopScan(in int appIf, in boolean isServer);
|
||||
void flushPendingBatchResults(in int appIf, in boolean isServer);
|
||||
void registerScanner(in IScannerCallback callback);
|
||||
void unregisterScanner(in int scannerId);
|
||||
void startScan(in int scannerId, in ScanSettings settings, in List<ScanFilter> filters,
|
||||
in WorkSource workSource, in List scanStorages, in String callingPackage);
|
||||
void stopScan(in int scannerId);
|
||||
void flushPendingBatchResults(in int scannerId);
|
||||
|
||||
void registerAdvertiser(in IAdvertiserCallback callback);
|
||||
void unregisterAdvertiser(in int advertiserId);
|
||||
|
||||
@@ -17,8 +17,6 @@ package android.bluetooth;
|
||||
|
||||
import android.os.ParcelUuid;
|
||||
import android.bluetooth.BluetoothGattService;
|
||||
import android.bluetooth.le.AdvertiseSettings;
|
||||
import android.bluetooth.le.ScanResult;
|
||||
|
||||
/**
|
||||
* Callback definitions for interacting with BLE / GATT
|
||||
@@ -28,8 +26,6 @@ oneway interface IBluetoothGattCallback {
|
||||
void onClientRegistered(in int status, in int clientIf);
|
||||
void onClientConnectionState(in int status, in int clientIf,
|
||||
in boolean connected, in String address);
|
||||
void onScanResult(in ScanResult scanResult);
|
||||
void onBatchScanResults(in List<ScanResult> batchResults);
|
||||
void onSearchComplete(in String address, in List<BluetoothGattService> services, in int status);
|
||||
void onCharacteristicRead(in String address, in int status, in int handle, in byte[] value);
|
||||
void onCharacteristicWrite(in String address, in int status, in int handle);
|
||||
@@ -38,7 +34,5 @@ oneway interface IBluetoothGattCallback {
|
||||
void onDescriptorWrite(in String address, in int status, in int handle);
|
||||
void onNotify(in String address, in int handle, in byte[] value);
|
||||
void onReadRemoteRssi(in String address, in int rssi, in int status);
|
||||
void onScanManagerErrorCallback(in int errorCode);
|
||||
void onConfigureMTU(in String address, in int mtu, in int status);
|
||||
void onFoundOrLost(in boolean onFound, in ScanResult scanResult);
|
||||
}
|
||||
|
||||
@@ -23,7 +23,6 @@ import android.bluetooth.BluetoothGattService;
|
||||
*/
|
||||
oneway interface IBluetoothGattServerCallback {
|
||||
void onServerRegistered(in int status, in int serverIf);
|
||||
void onScanResult(in String address, in int rssi, in byte[] advData);
|
||||
void onServerConnectionState(in int status, in int serverIf,
|
||||
in boolean connected, in String address);
|
||||
void onServiceAdded(in int status, in BluetoothGattService service);
|
||||
|
||||
@@ -18,7 +18,6 @@ package android.bluetooth.le;
|
||||
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothGatt;
|
||||
import android.bluetooth.BluetoothGattCallbackWrapper;
|
||||
import android.bluetooth.BluetoothUuid;
|
||||
import android.bluetooth.IBluetoothGatt;
|
||||
import android.bluetooth.IBluetoothManager;
|
||||
|
||||
@@ -22,9 +22,9 @@ import android.annotation.SystemApi;
|
||||
import android.app.ActivityThread;
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothGatt;
|
||||
import android.bluetooth.BluetoothGattCallbackWrapper;
|
||||
import android.bluetooth.IBluetoothGatt;
|
||||
import android.bluetooth.IBluetoothManager;
|
||||
import android.bluetooth.le.IScannerCallback;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.ParcelUuid;
|
||||
@@ -267,7 +267,7 @@ public final class BluetoothLeScanner {
|
||||
/**
|
||||
* Bluetooth GATT interface callbacks
|
||||
*/
|
||||
private class BleScanCallbackWrapper extends BluetoothGattCallbackWrapper {
|
||||
private class BleScanCallbackWrapper extends IScannerCallback.Stub {
|
||||
private static final int REGISTRATION_CALLBACK_TIMEOUT_MILLIS = 2000;
|
||||
|
||||
private final ScanCallback mScanCallback;
|
||||
@@ -280,7 +280,7 @@ public final class BluetoothLeScanner {
|
||||
// mLeHandle 0: not registered
|
||||
// -1: scan stopped or registration failed
|
||||
// > 0: registered and scan started
|
||||
private int mClientIf;
|
||||
private int mScannerId;
|
||||
|
||||
public BleScanCallbackWrapper(IBluetoothGatt bluetoothGatt,
|
||||
List<ScanFilter> filters, ScanSettings settings,
|
||||
@@ -291,28 +291,27 @@ public final class BluetoothLeScanner {
|
||||
mSettings = settings;
|
||||
mWorkSource = workSource;
|
||||
mScanCallback = scanCallback;
|
||||
mClientIf = 0;
|
||||
mScannerId = 0;
|
||||
mResultStorages = resultStorages;
|
||||
}
|
||||
|
||||
public void startRegisteration() {
|
||||
synchronized (this) {
|
||||
// Scan stopped.
|
||||
if (mClientIf == -1) return;
|
||||
if (mScannerId == -1) return;
|
||||
try {
|
||||
UUID uuid = UUID.randomUUID();
|
||||
mBluetoothGatt.registerClient(new ParcelUuid(uuid), this);
|
||||
mBluetoothGatt.registerScanner(this);
|
||||
wait(REGISTRATION_CALLBACK_TIMEOUT_MILLIS);
|
||||
} catch (InterruptedException | RemoteException e) {
|
||||
Log.e(TAG, "application registeration exception", e);
|
||||
postCallbackError(mScanCallback, ScanCallback.SCAN_FAILED_INTERNAL_ERROR);
|
||||
}
|
||||
if (mClientIf > 0) {
|
||||
if (mScannerId > 0) {
|
||||
mLeScanClients.put(mScanCallback, this);
|
||||
} else {
|
||||
// Registration timed out or got exception, reset clientIf to -1 so no
|
||||
// Registration timed out or got exception, reset scannerId to -1 so no
|
||||
// subsequent operations can proceed.
|
||||
if (mClientIf == 0) mClientIf = -1;
|
||||
if (mScannerId == 0) mScannerId = -1;
|
||||
postCallbackError(mScanCallback,
|
||||
ScanCallback.SCAN_FAILED_APPLICATION_REGISTRATION_FAILED);
|
||||
}
|
||||
@@ -321,28 +320,28 @@ public final class BluetoothLeScanner {
|
||||
|
||||
public void stopLeScan() {
|
||||
synchronized (this) {
|
||||
if (mClientIf <= 0) {
|
||||
Log.e(TAG, "Error state, mLeHandle: " + mClientIf);
|
||||
if (mScannerId <= 0) {
|
||||
Log.e(TAG, "Error state, mLeHandle: " + mScannerId);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
mBluetoothGatt.stopScan(mClientIf, false);
|
||||
mBluetoothGatt.unregisterClient(mClientIf);
|
||||
mBluetoothGatt.stopScan(mScannerId);
|
||||
mBluetoothGatt.unregisterScanner(mScannerId);
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, "Failed to stop scan and unregister", e);
|
||||
}
|
||||
mClientIf = -1;
|
||||
mScannerId = -1;
|
||||
}
|
||||
}
|
||||
|
||||
void flushPendingBatchResults() {
|
||||
synchronized (this) {
|
||||
if (mClientIf <= 0) {
|
||||
Log.e(TAG, "Error state, mLeHandle: " + mClientIf);
|
||||
if (mScannerId <= 0) {
|
||||
Log.e(TAG, "Error state, mLeHandle: " + mScannerId);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
mBluetoothGatt.flushPendingBatchResults(mClientIf, false);
|
||||
mBluetoothGatt.flushPendingBatchResults(mScannerId);
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, "Failed to get pending scan results", e);
|
||||
}
|
||||
@@ -353,28 +352,28 @@ public final class BluetoothLeScanner {
|
||||
* Application interface registered - app is ready to go
|
||||
*/
|
||||
@Override
|
||||
public void onClientRegistered(int status, int clientIf) {
|
||||
Log.d(TAG, "onClientRegistered() - status=" + status +
|
||||
" clientIf=" + clientIf + " mClientIf=" + mClientIf);
|
||||
public void onScannerRegistered(int status, int scannerId) {
|
||||
Log.d(TAG, "onScannerRegistered() - status=" + status +
|
||||
" scannerId=" + scannerId + " mScannerId=" + mScannerId);
|
||||
synchronized (this) {
|
||||
if (status == BluetoothGatt.GATT_SUCCESS) {
|
||||
try {
|
||||
if (mClientIf == -1) {
|
||||
if (mScannerId == -1) {
|
||||
// Registration succeeds after timeout, unregister client.
|
||||
mBluetoothGatt.unregisterClient(clientIf);
|
||||
mBluetoothGatt.unregisterClient(scannerId);
|
||||
} else {
|
||||
mClientIf = clientIf;
|
||||
mBluetoothGatt.startScan(mClientIf, false, mSettings, mFilters,
|
||||
mScannerId = scannerId;
|
||||
mBluetoothGatt.startScan(mScannerId, mSettings, mFilters,
|
||||
mWorkSource, mResultStorages,
|
||||
ActivityThread.currentOpPackageName());
|
||||
}
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, "fail to start le scan: " + e);
|
||||
mClientIf = -1;
|
||||
mScannerId = -1;
|
||||
}
|
||||
} else {
|
||||
// registration failed
|
||||
mClientIf = -1;
|
||||
mScannerId = -1;
|
||||
}
|
||||
notifyAll();
|
||||
}
|
||||
@@ -391,7 +390,7 @@ public final class BluetoothLeScanner {
|
||||
|
||||
// Check null in case the scan has been stopped
|
||||
synchronized (this) {
|
||||
if (mClientIf <= 0) return;
|
||||
if (mScannerId <= 0) return;
|
||||
}
|
||||
Handler handler = new Handler(Looper.getMainLooper());
|
||||
handler.post(new Runnable() {
|
||||
@@ -423,7 +422,7 @@ public final class BluetoothLeScanner {
|
||||
|
||||
// Check null in case the scan has been stopped
|
||||
synchronized (this) {
|
||||
if (mClientIf <= 0)
|
||||
if (mScannerId <= 0)
|
||||
return;
|
||||
}
|
||||
Handler handler = new Handler(Looper.getMainLooper());
|
||||
@@ -447,7 +446,7 @@ public final class BluetoothLeScanner {
|
||||
Log.d(TAG, "onScanManagerErrorCallback() - errorCode = " + errorCode);
|
||||
}
|
||||
synchronized (this) {
|
||||
if (mClientIf <= 0)
|
||||
if (mScannerId <= 0)
|
||||
return;
|
||||
}
|
||||
postCallbackError(mScanCallback, errorCode);
|
||||
|
||||
31
core/java/android/bluetooth/le/IScannerCallback.aidl
Normal file
31
core/java/android/bluetooth/le/IScannerCallback.aidl
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (C) 2016 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package android.bluetooth.le;
|
||||
|
||||
import android.bluetooth.le.ScanResult;
|
||||
|
||||
/**
|
||||
* Callback definitions for interacting with Advertiser
|
||||
* @hide
|
||||
*/
|
||||
oneway interface IScannerCallback {
|
||||
void onScannerRegistered(in int status, in int scannerId);
|
||||
|
||||
void onScanResult(in ScanResult scanResult);
|
||||
void onBatchScanResults(in List<ScanResult> batchResults);
|
||||
void onFoundOrLost(in boolean onFound, in ScanResult scanResult);
|
||||
void onScanManagerErrorCallback(in int errorCode);
|
||||
}
|
||||
Reference in New Issue
Block a user