Skip to content

Commit

Permalink
Merge pull request #123 from c19x/feature-122
Browse files Browse the repository at this point in the history
Transmitter stop simplification
Signed-off-by: Adam Fowler <[email protected]>
  • Loading branch information
adamfowleruk authored Dec 26, 2020
2 parents 701552d + 5ec7bd2 commit f37bcfb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
15 changes: 12 additions & 3 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,16 @@ private long timeSincelastStateChange(final long now) {
public void bleTimer(final long now) {
if (!isSupported() || bluetoothStateManager.state() == BluetoothState.poweredOff) {
if (advertLoopState != AdvertLoopState.stopped) {
advertiseCallback = null;
bluetoothGattServer = null;
state(now, AdvertLoopState.stopped);
logger.debug("advertLoopTask, stop advert (advert={}ms)", timeSincelastStateChange(now));
logger.debug("advertLoopTask, stopping advert following bluetooth state change (isSupported={},bluetoothPowerOff={})", isSupported(), bluetoothStateManager.state() == BluetoothState.poweredOff);
stopAdvert(bluetoothLeAdvertiser(), advertiseCallback, bluetoothGattServer, new Callback<Boolean>() {
@Override
public void accept(Boolean value) {
advertiseCallback = null;
bluetoothGattServer = null;
state(now, AdvertLoopState.stopped);
logger.debug("advertLoopTask, stop advert (advert={}ms)", timeSincelastStateChange(now));
}
});
}
return;
}
Expand Down Expand Up @@ -206,29 +212,28 @@ public void accept(Boolean value) {

// MARK:- Start and stop advert

private void stopExistingGattServer() {
if (null != bluetoothGattServer) {
// Stop old version, if there's already a proxy reference
try {
bluetoothGattServer.clearServices();
bluetoothGattServer.close();
bluetoothGattServer = null;
} catch (Throwable e2) {
logger.fault("stopGattServer failed to stop EXISTING GATT server", e2);
bluetoothGattServer = null;
}
}
}

private void startAdvert(final BluetoothLeAdvertiser bluetoothLeAdvertiser, final Callback<Triple<Boolean, AdvertiseCallback, BluetoothGattServer>> callback) {
logger.debug("startAdvert");
operationQueue.execute(new Runnable() {
@Override
public void run() {
boolean result = true;

stopExistingGattServer();

// Stop existing advert if there is already a proxy reference.
// This should never happen because only the AdvertLoopTask calls
// startAdvert and it should only call startAdvert after stopAdvert
// has been called previously. Logging this condition to verify if
// this condition can ever occur to support investigation.
if (bluetoothGattServer != null) {
logger.fault("startAdvert found existing GATT server");
try {
bluetoothGattServer.clearServices();
bluetoothGattServer.close();
} catch (Throwable e) {
logger.fault("startAdvert found existing GATT server but failed to stop the server", e);
}
bluetoothGattServer = null;
}
// Start new GATT server
try {
bluetoothGattServer = startGattServer(logger, context, payloadDataSupplier, database);
} catch (Throwable e) {
Expand Down Expand Up @@ -289,7 +294,7 @@ private void stopAdvert(final BluetoothLeAdvertiser bluetoothLeAdvertiser, final
public void run() {
boolean result = true;
try {
if (advertiseCallback != null) {
if (bluetoothLeAdvertiser != null && advertiseCallback != null) {
bluetoothLeAdvertiser.stopAdvertising(advertiseCallback);
}
} catch (Throwable e) {
Expand Down

0 comments on commit f37bcfb

Please sign in to comment.