This repository has been archived by the owner on Mar 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 658
Add support for ble scanning to speed up reconnection #1416
Open
andyboeh
wants to merge
1
commit into
Freeyourgadget:master
Choose a base branch
from
andyboeh:ble_reconnect_scan
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...va/nodomain/freeyourgadget/gadgetbridge/externalevents/BluetoothScanCallbackReceiver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* Copyright (C) 2019 Andreas Böhler | ||
|
||
This file is part of Gadgetbridge. | ||
|
||
Gadgetbridge is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Affero General Public License as published | ||
by the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
|
||
Gadgetbridge is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Affero General Public License for more details. | ||
|
||
You should have received a copy of the GNU Affero General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. */ | ||
package nodomain.freeyourgadget.gadgetbridge.externalevents; | ||
|
||
import android.annotation.TargetApi; | ||
import android.app.PendingIntent; | ||
import android.bluetooth.BluetoothAdapter; | ||
import android.bluetooth.BluetoothDevice; | ||
import android.bluetooth.le.BluetoothLeScanner; | ||
import android.bluetooth.le.ScanResult; | ||
import android.content.BroadcastReceiver; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.os.Build; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.ArrayList; | ||
|
||
|
||
import nodomain.freeyourgadget.gadgetbridge.GBApplication; | ||
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper; | ||
|
||
public class BluetoothScanCallbackReceiver extends BroadcastReceiver { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(BluetoothScanCallbackReceiver.class); | ||
private String mSeenScanCallbackUUID = ""; | ||
|
||
@TargetApi(Build.VERSION_CODES.O) | ||
@Override | ||
public void onReceive(Context context, Intent intent) { | ||
String action = intent.getAction(); | ||
if(!action.equals("nodomain.freeyourgadget.gadgetbridge.blescancallback") || !intent.hasExtra("address") || !intent.hasExtra("uuid")) { | ||
return; | ||
} | ||
|
||
String wantedAddress = intent.getExtras().getString("address"); | ||
String uuid = intent.getExtras().getString("uuid"); | ||
|
||
int bleCallbackType = intent.getIntExtra(BluetoothLeScanner.EXTRA_CALLBACK_TYPE, -1); | ||
if(bleCallbackType != -1) { | ||
//LOG.debug("Passive background scan callback type: " + bleCallbackType); | ||
ArrayList<ScanResult> scanResults = intent.getParcelableArrayListExtra(BluetoothLeScanner.EXTRA_LIST_SCAN_RESULT); | ||
for(ScanResult result: scanResults) { | ||
BluetoothDevice device = result.getDevice(); | ||
if(device.getAddress().equals(wantedAddress) && !mSeenScanCallbackUUID.equals(uuid)) { | ||
mSeenScanCallbackUUID = uuid; | ||
LOG.info("ScanCallbackReceiver has found " + device.getAddress() + "(" + device.getName() + ")"); | ||
BluetoothAdapter.getDefaultAdapter().getBluetoothLeScanner().stopScan(getScanCallbackIntent(GBApplication.getContext(), wantedAddress, uuid)); | ||
GBApplication.deviceService().connect(DeviceHelper.getInstance().toSupportedDevice(device)); | ||
|
||
} | ||
} | ||
} | ||
} | ||
|
||
@TargetApi(Build.VERSION_CODES.O) | ||
public static PendingIntent getScanCallbackIntent(Context context, String address, String uuid) { | ||
Intent intent = new Intent(context, BluetoothScanCallbackReceiver.class); | ||
intent.setAction("nodomain.freeyourgadget.gadgetbridge.blescancallback"); | ||
intent.putExtra("address", address); | ||
intent.putExtra("uuid", uuid); | ||
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the lifecycle of this receiver? Is it running all the time, basically as a singleton? Is that intended? What if there were two devices, waiting for reconnection?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does GB support multiple devices in a waiting for reconnection state already?
As far as I have understood the Android-concept here, the receiver is always active and acting like a singleton. Since the address to match and a uuid are given as extras to the intent when starting a scan, it shouldn't be a problem to have several devices in a waiting for reconnect state.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, not yet, I'm just checking for the future. There's even a WIP PR for multi device PR, but we haven't gotten around at making it ready for prime time. :-(
I think I understand the intention now, maybe deserves a javadoc explanation :-)