Merge "StopScan before each new Companion device scan" into oc-dev

This commit is contained in:
Eugene Susla
2017-04-20 17:50:55 +00:00
committed by Android (Google) Code Review
3 changed files with 120 additions and 74 deletions

View File

@@ -83,6 +83,14 @@ public final class AssociationRequest implements Parcelable {
return Objects.hash(mSingleDevice, mDeviceFilters); return Objects.hash(mSingleDevice, mDeviceFilters);
} }
@Override
public String toString() {
return "AssociationRequest{" +
"mSingleDevice=" + mSingleDevice +
", mDeviceFilters=" + mDeviceFilters +
'}';
}
@Override @Override
public void writeToParcel(Parcel dest, int flags) { public void writeToParcel(Parcel dest, int flags) {
dest.writeByte((byte) (mSingleDevice ? 1 : 0)); dest.writeByte((byte) (mSingleDevice ? 1 : 0));

View File

@@ -225,6 +225,23 @@ public final class BluetoothLEDeviceFilter implements DeviceFilter<ScanResult> {
return 0; return 0;
} }
@Override
public String toString() {
return "BluetoothLEDeviceFilter{" +
"mNamePattern=" + mNamePattern +
", mScanFilter=" + mScanFilter +
", mRawDataFilter=" + Arrays.toString(mRawDataFilter) +
", mRawDataFilterMask=" + Arrays.toString(mRawDataFilterMask) +
", mRenamePrefix='" + mRenamePrefix + '\'' +
", mRenameSuffix='" + mRenameSuffix + '\'' +
", mRenameBytesFrom=" + mRenameBytesFrom +
", mRenameBytesTo=" + mRenameBytesTo +
", mRenameNameFrom=" + mRenameNameFrom +
", mRenameNameTo=" + mRenameNameTo +
", mRenameBytesReverseOrder=" + mRenameBytesReverseOrder +
'}';
}
public static final Creator<BluetoothLEDeviceFilter> CREATOR public static final Creator<BluetoothLEDeviceFilter> CREATOR
= new Creator<BluetoothLEDeviceFilter>() { = new Creator<BluetoothLEDeviceFilter>() {
@Override @Override

View File

@@ -75,17 +75,21 @@ public class DeviceDiscoveryService extends Service {
private BluetoothAdapter mBluetoothAdapter; private BluetoothAdapter mBluetoothAdapter;
private WifiManager mWifiManager; private WifiManager mWifiManager;
private BluetoothLeScanner mBLEScanner;
private ScanSettings mDefaultScanSettings = new ScanSettings.Builder().build(); private ScanSettings mDefaultScanSettings = new ScanSettings.Builder().build();
private List<DeviceFilter<?>> mFilters; private List<DeviceFilter<?>> mFilters;
private List<BluetoothLEDeviceFilter> mBLEFilters; private List<BluetoothLEDeviceFilter> mBLEFilters;
private List<BluetoothDeviceFilter> mBluetoothFilters; private List<BluetoothDeviceFilter> mBluetoothFilters;
private List<WifiDeviceFilter> mWifiFilters; private List<WifiDeviceFilter> mWifiFilters;
private List<ScanFilter> mBLEScanFilters; private List<ScanFilter> mBLEScanFilters;
AssociationRequest mRequest; AssociationRequest mRequest;
List<DeviceFilterPair> mDevicesFound; List<DeviceFilterPair> mDevicesFound;
DeviceFilterPair mSelectedDevice; DeviceFilterPair mSelectedDevice;
DevicesAdapter mDevicesAdapter; DevicesAdapter mDevicesAdapter;
IFindDeviceCallback mFindCallback; IFindDeviceCallback mFindCallback;
ICompanionDeviceDiscoveryServiceCallback mServiceCallback; ICompanionDeviceDiscoveryServiceCallback mServiceCallback;
private final ICompanionDeviceDiscoveryService mBinder = private final ICompanionDeviceDiscoveryService mBinder =
@@ -107,65 +111,9 @@ public class DeviceDiscoveryService extends Service {
} }
}; };
private final ScanCallback mBLEScanCallback = new ScanCallback() { private ScanCallback mBLEScanCallback;
@Override private BluetoothBroadcastReceiver mBluetoothBroadcastReceiver;
public void onScanResult(int callbackType, ScanResult result) { private WifiBroadcastReceiver mWifiBroadcastReceiver;
if (DEBUG) {
Log.i(LOG_TAG,
"BLE.onScanResult(callbackType = " + callbackType + ", result = " + result
+ ")");
}
final DeviceFilterPair<ScanResult> deviceFilterPair
= DeviceFilterPair.findMatch(result, mBLEFilters);
if (deviceFilterPair == null) return;
if (callbackType == ScanSettings.CALLBACK_TYPE_MATCH_LOST) {
onDeviceLost(deviceFilterPair);
} else {
onDeviceFound(deviceFilterPair);
}
}
};
private BluetoothLeScanner mBLEScanner;
private BroadcastReceiver mBluetoothDeviceFoundBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (DEBUG) {
Log.i(LOG_TAG,
"BL.onReceive(context = " + context + ", intent = " + intent + ")");
}
final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
final DeviceFilterPair<BluetoothDevice> deviceFilterPair
= DeviceFilterPair.findMatch(device, mBluetoothFilters);
if (deviceFilterPair == null) return;
if (intent.getAction().equals(BluetoothDevice.ACTION_FOUND)) {
onDeviceFound(deviceFilterPair);
} else {
onDeviceLost(deviceFilterPair);
}
}
};
private BroadcastReceiver mWifiDeviceFoundBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
List<android.net.wifi.ScanResult> scanResults = mWifiManager.getScanResults();
if (DEBUG) {
Log.i(LOG_TAG, "Wifi scan results: " + TextUtils.join("\n", scanResults));
}
for (int i = 0; i < scanResults.size(); i++) {
DeviceFilterPair<android.net.wifi.ScanResult> deviceFilterPair =
DeviceFilterPair.findMatch(scanResults.get(i), mWifiFilters);
if (deviceFilterPair != null) onDeviceFound(deviceFilterPair);
}
}
}
};
@Override @Override
public IBinder onBind(Intent intent) { public IBinder onBind(Intent intent) {
@@ -211,16 +159,19 @@ public class DeviceDiscoveryService extends Service {
intentFilter.addAction(BluetoothDevice.ACTION_FOUND); intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
intentFilter.addAction(BluetoothDevice.ACTION_DISAPPEARED); intentFilter.addAction(BluetoothDevice.ACTION_DISAPPEARED);
registerReceiver(mBluetoothDeviceFoundBroadcastReceiver, intentFilter); mBluetoothBroadcastReceiver = new BluetoothBroadcastReceiver();
registerReceiver(mBluetoothBroadcastReceiver, intentFilter);
mBluetoothAdapter.startDiscovery(); mBluetoothAdapter.startDiscovery();
} }
if (shouldScan(mBLEFilters)) { if (shouldScan(mBLEFilters)) {
mBLEScanCallback = new BLEScanCallback();
mBLEScanner.startScan(mBLEScanFilters, mDefaultScanSettings, mBLEScanCallback); mBLEScanner.startScan(mBLEScanFilters, mDefaultScanSettings, mBLEScanCallback);
} }
if (shouldScan(mWifiFilters)) { if (shouldScan(mWifiFilters)) {
registerReceiver(mWifiDeviceFoundBroadcastReceiver, mWifiBroadcastReceiver = new WifiBroadcastReceiver();
registerReceiver(mWifiBroadcastReceiver,
new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
mWifiManager.startScan(); mWifiManager.startScan();
} }
@@ -232,6 +183,7 @@ public class DeviceDiscoveryService extends Service {
private void reset() { private void reset() {
if (DEBUG) Log.i(LOG_TAG, "reset()"); if (DEBUG) Log.i(LOG_TAG, "reset()");
stopScan();
mDevicesFound.clear(); mDevicesFound.clear();
mSelectedDevice = null; mSelectedDevice = null;
mDevicesAdapter.notifyDataSetChanged(); mDevicesAdapter.notifyDataSetChanged();
@@ -244,20 +196,18 @@ public class DeviceDiscoveryService extends Service {
} }
private void stopScan() { private void stopScan() {
if (DEBUG) Log.i(LOG_TAG, "stopScan() called"); if (DEBUG) Log.i(LOG_TAG, "stopScan()");
if (shouldScan(mBluetoothFilters)) { mBluetoothAdapter.cancelDiscovery();
mBluetoothAdapter.cancelDiscovery(); if (mBluetoothBroadcastReceiver != null) {
unregisterReceiver(mBluetoothDeviceFoundBroadcastReceiver); unregisterReceiver(mBluetoothBroadcastReceiver);
mBluetoothBroadcastReceiver = null;
} }
if (shouldScan(mBLEFilters)) { mBLEScanner.stopScan(mBLEScanCallback);
mBLEScanner.stopScan(mBLEScanCallback); if (mWifiBroadcastReceiver != null) {
unregisterReceiver(mWifiBroadcastReceiver);
mWifiBroadcastReceiver = null;
} }
if (shouldScan(mWifiFilters)) {
unregisterReceiver(mWifiDeviceFoundBroadcastReceiver);
}
stopSelf();
} }
private void onDeviceFound(@Nullable DeviceFilterPair device) { private void onDeviceFound(@Nullable DeviceFilterPair device) {
@@ -265,8 +215,7 @@ public class DeviceDiscoveryService extends Service {
return; return;
} }
if (DEBUG) Log.i(LOG_TAG, "Found device " + device.getDisplayName() + " " if (DEBUG) Log.i(LOG_TAG, "Found device " + device);
+ getDeviceMacAddress(device.device));
if (mDevicesFound.isEmpty()) { if (mDevicesFound.isEmpty()) {
onReadyToShowUI(); onReadyToShowUI();
@@ -306,6 +255,7 @@ public class DeviceDiscoveryService extends Service {
} }
void onCancel() { void onCancel() {
if (DEBUG) Log.i(LOG_TAG, "onCancel()");
try { try {
mServiceCallback.onDeviceSelectionCancel(); mServiceCallback.onDeviceSelectionCancel();
} catch (RemoteException e) { } catch (RemoteException e) {
@@ -430,5 +380,76 @@ public class DeviceDiscoveryService extends Service {
public int hashCode() { public int hashCode() {
return Objects.hash(getDeviceMacAddress(device)); return Objects.hash(getDeviceMacAddress(device));
} }
@Override
public String toString() {
return "DeviceFilterPair{" +
"device=" + device +
", filter=" + filter +
'}';
}
}
private class BLEScanCallback extends ScanCallback {
public BLEScanCallback() {
if (DEBUG) Log.i(LOG_TAG, "new BLEScanCallback() -> " + this);
}
@Override
public void onScanResult(int callbackType, ScanResult result) {
if (DEBUG) {
Log.i(LOG_TAG,
"BLE.onScanResult(callbackType = " + callbackType + ", result = " + result
+ ")");
}
final DeviceFilterPair<ScanResult> deviceFilterPair
= DeviceFilterPair.findMatch(result, mBLEFilters);
if (deviceFilterPair == null) return;
if (callbackType == ScanSettings.CALLBACK_TYPE_MATCH_LOST) {
onDeviceLost(deviceFilterPair);
} else {
onDeviceFound(deviceFilterPair);
}
}
}
private class BluetoothBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (DEBUG) {
Log.i(LOG_TAG,
"BL.onReceive(context = " + context + ", intent = " + intent + ")");
}
final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
final DeviceFilterPair<BluetoothDevice> deviceFilterPair
= DeviceFilterPair.findMatch(device, mBluetoothFilters);
if (deviceFilterPair == null) return;
if (intent.getAction().equals(BluetoothDevice.ACTION_FOUND)) {
onDeviceFound(deviceFilterPair);
} else {
onDeviceLost(deviceFilterPair);
}
}
}
private class WifiBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
List<android.net.wifi.ScanResult> scanResults = mWifiManager.getScanResults();
if (DEBUG) {
Log.i(LOG_TAG, "Wifi scan results: " + TextUtils.join("\n", scanResults));
}
for (int i = 0; i < scanResults.size(); i++) {
DeviceFilterPair<android.net.wifi.ScanResult> deviceFilterPair =
DeviceFilterPair.findMatch(scanResults.get(i), mWifiFilters);
if (deviceFilterPair != null) onDeviceFound(deviceFilterPair);
}
}
}
} }
} }