private BluetoothAdapter mBluetoothAdapter;
private WifiManager mWifiManager;
+ private BluetoothLeScanner mBLEScanner;
private ScanSettings mDefaultScanSettings = new ScanSettings.Builder().build();
+
private List<DeviceFilter<?>> mFilters;
private List<BluetoothLEDeviceFilter> mBLEFilters;
private List<BluetoothDeviceFilter> mBluetoothFilters;
private List<WifiDeviceFilter> mWifiFilters;
private List<ScanFilter> mBLEScanFilters;
+
AssociationRequest mRequest;
List<DeviceFilterPair> mDevicesFound;
DeviceFilterPair mSelectedDevice;
DevicesAdapter mDevicesAdapter;
IFindDeviceCallback mFindCallback;
+
ICompanionDeviceDiscoveryServiceCallback mServiceCallback;
private final ICompanionDeviceDiscoveryService mBinder =
}
};
- private final ScanCallback mBLEScanCallback = new ScanCallback() {
- @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 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);
- }
- }
-
- }
- };
+ private ScanCallback mBLEScanCallback;
+ private BluetoothBroadcastReceiver mBluetoothBroadcastReceiver;
+ private WifiBroadcastReceiver mWifiBroadcastReceiver;
@Override
public IBinder onBind(Intent intent) {
intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
intentFilter.addAction(BluetoothDevice.ACTION_DISAPPEARED);
- registerReceiver(mBluetoothDeviceFoundBroadcastReceiver, intentFilter);
+ mBluetoothBroadcastReceiver = new BluetoothBroadcastReceiver();
+ registerReceiver(mBluetoothBroadcastReceiver, intentFilter);
mBluetoothAdapter.startDiscovery();
}
if (shouldScan(mBLEFilters)) {
+ mBLEScanCallback = new BLEScanCallback();
mBLEScanner.startScan(mBLEScanFilters, mDefaultScanSettings, mBLEScanCallback);
}
if (shouldScan(mWifiFilters)) {
- registerReceiver(mWifiDeviceFoundBroadcastReceiver,
+ mWifiBroadcastReceiver = new WifiBroadcastReceiver();
+ registerReceiver(mWifiBroadcastReceiver,
new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
mWifiManager.startScan();
}
private void reset() {
if (DEBUG) Log.i(LOG_TAG, "reset()");
+ stopScan();
mDevicesFound.clear();
mSelectedDevice = null;
mDevicesAdapter.notifyDataSetChanged();
}
private void stopScan() {
- if (DEBUG) Log.i(LOG_TAG, "stopScan() called");
+ if (DEBUG) Log.i(LOG_TAG, "stopScan()");
- if (shouldScan(mBluetoothFilters)) {
- mBluetoothAdapter.cancelDiscovery();
- unregisterReceiver(mBluetoothDeviceFoundBroadcastReceiver);
+ mBluetoothAdapter.cancelDiscovery();
+ if (mBluetoothBroadcastReceiver != null) {
+ 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) {
return;
}
- if (DEBUG) Log.i(LOG_TAG, "Found device " + device.getDisplayName() + " "
- + getDeviceMacAddress(device.device));
+ if (DEBUG) Log.i(LOG_TAG, "Found device " + device);
if (mDevicesFound.isEmpty()) {
onReadyToShowUI();
}
void onCancel() {
+ if (DEBUG) Log.i(LOG_TAG, "onCancel()");
try {
mServiceCallback.onDeviceSelectionCancel();
} catch (RemoteException e) {
public int hashCode() {
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);
+ }
+ }
+
+ }
}
}