Skip to content
This repository was archived by the owner on Aug 9, 2025. It is now read-only.

Commit 9f9ea8b

Browse files
author
TebbeUbben
committed
Better bluetooth disconnect detection
1 parent af1325f commit 9f9ea8b

6 files changed

Lines changed: 80 additions & 20 deletions

File tree

app/src/main/AndroidManifest.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@
101101
android:name="sugar.free.sightparser.handling.SightService"
102102
android:exported="true"
103103
android:process="sugar.free.sightparser.handling.SightService" />
104+
105+
<receiver android:name=".receivers.ACLReceiver">
106+
<intent-filter>
107+
<action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
108+
</intent-filter>
109+
</receiver>
104110
</application>
105111

106112
</manifest>

app/src/main/java/sugar/free/sightremote/SightRemote.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@
1111
import lombok.Getter;
1212
import sugar.free.sightparser.handling.HistoryBroadcast;
1313
import sugar.free.sightparser.handling.SightService;
14+
import sugar.free.sightparser.handling.SightServiceConnector;
1415
import sugar.free.sightremote.services.AlertService;
1516
import sugar.free.sightremote.services.HistorySyncService;
1617

1718
public class SightRemote extends Application {
1819

1920
@Getter
2021
private static SightRemote instance;
21-
22+
@Getter
23+
private SightServiceConnector serviceConnector;
2224

2325
@Override
2426
public void onCreate() {
@@ -28,6 +30,9 @@ public void onCreate() {
2830
PreferenceManager.setDefaultValues(this, R.xml.settings, true);
2931

3032
instance = this;
33+
serviceConnector = new SightServiceConnector(this);
34+
serviceConnector.connectToService();
35+
3136
startService(new Intent(this, SightService.class));
3237
startService(new Intent(this, HistorySyncService.class));
3338
startService(new Intent(this, AlertService.class));
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package sugar.free.sightremote.receivers;
2+
3+
import android.bluetooth.BluetoothDevice;
4+
import android.content.BroadcastReceiver;
5+
import android.content.Context;
6+
import android.content.Intent;
7+
import android.util.Log;
8+
import sugar.free.sightparser.handling.SightServiceConnector;
9+
import sugar.free.sightremote.SightRemote;
10+
11+
public class ACLReceiver extends BroadcastReceiver {
12+
@Override
13+
public void onReceive(Context context, Intent intent) {
14+
String mac = ((BluetoothDevice) intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE")).getAddress();
15+
SightServiceConnector serviceConnector = SightRemote.getInstance().getServiceConnector();
16+
if (serviceConnector.isConnectedToService()) serviceConnector.aclDisconnect(mac);
17+
}
18+
}

sightparser/src/main/aidl/sugar/free/sightparser/handling/ISightService.aidl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ interface ISightService {
1717
void setPassword(String password);
1818
void setAuthorized(String packageName, boolean allowed);
1919
void reset();
20+
void aclDisconnect(String mac);
2021
}

sightparser/src/main/java/sugar/free/sightparser/handling/SightService.java

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -134,27 +134,30 @@ public void requestMessage(byte[] message, IMessageCallback callback) throws Rem
134134
@Override
135135
public long registerStatusCallback(final IStatusCallback callback) throws RemoteException {
136136
final long id = ++statusIdCounter;
137-
DeathRecipient deathRecipient = new DeathRecipient() {
138-
@Override
139-
public void binderDied() {
140-
Log.d("SightService", "CLIENT DIED - STATUS");
141-
callback.asBinder().unlinkToDeath(statusCallbackDeathRecipients.get(callback), 0);
142-
statusCallbackDeathRecipients.remove(callback);
137+
DeathRecipient deathRecipient = () -> {
138+
Log.d("SightService", "CLIENT DIED - STATUS");
139+
callback.asBinder().unlinkToDeath(statusCallbackDeathRecipients.get(callback), 0);
140+
statusCallbackDeathRecipients.remove(callback);
141+
synchronized (statusCallbackIds) {
143142
statusCallbackIds.remove(id);
144143
}
145144
};
146-
statusCallbackDeathRecipients.put(callback, deathRecipient);
147-
statusCallbackIds.put(id, callback);
148-
callback.asBinder().linkToDeath(deathRecipient, 0);
145+
synchronized (statusCallbackIds) {
146+
statusCallbackDeathRecipients.put(callback, deathRecipient);
147+
statusCallbackIds.put(id, callback);
148+
callback.asBinder().linkToDeath(deathRecipient, 0);
149+
}
149150
return id;
150151
}
151152

152153
@Override
153154
public void unregisterStatusCallback(long id) throws RemoteException {
154-
IStatusCallback callback = statusCallbackIds.get(id);
155-
callback.asBinder().unlinkToDeath(statusCallbackDeathRecipients.get(callback), 0);
156-
statusCallbackDeathRecipients.remove(callback);
157-
statusCallbackIds.remove(id);
155+
synchronized (statusCallbackIds) {
156+
IStatusCallback callback = statusCallbackIds.get(id);
157+
callback.asBinder().unlinkToDeath(statusCallbackDeathRecipients.get(callback), 0);
158+
statusCallbackDeathRecipients.remove(callback);
159+
statusCallbackIds.remove(id);
160+
}
158161
}
159162

160163
@Override
@@ -248,6 +251,24 @@ public void reset() throws RemoteException {
248251
throw new RemoteException("Not authorized");
249252
}
250253
}
254+
255+
@Override
256+
public void aclDisconnect(String mac) throws RemoteException {
257+
if (verifyAdminCaller("aclDisconnect")) {
258+
if (getDataStorage().get("DEVICEMAC").equalsIgnoreCase(mac)) {
259+
try {
260+
if (bluetoothSocket.isConnected()) {
261+
Log.d("SightService", "Received ACL disconnect, closing socket...");
262+
bluetoothSocket.close();
263+
}
264+
} catch (IOException e) {
265+
e.printStackTrace();
266+
}
267+
}
268+
} else {
269+
throw new RemoteException("Not authorized");
270+
}
271+
}
251272
};
252273
private StatusCallback statusCallback = new StatusCallback() {
253274
@Override
@@ -262,12 +283,13 @@ public void onStatusChange(Status status) {
262283
reconnect = true;
263284
}
264285
}
265-
Iterator<IStatusCallback> sc = statusCallbackIds.values().iterator();
266-
while (sc.hasNext()) {
267-
try {
268-
sc.next().onStatusChange(status.name());
269-
} catch (Exception e) {
270-
e.printStackTrace();
286+
synchronized (statusCallbackIds) {
287+
for (IStatusCallback sc : statusCallbackIds.values()) {
288+
try {
289+
sc.onStatusChange(status.name());
290+
} catch (Exception e) {
291+
e.printStackTrace();
292+
}
271293
}
272294
}
273295
Answers.getInstance().logCustom(new CustomEvent("Connection Status Changed")

sightparser/src/main/java/sugar/free/sightparser/handling/SightServiceConnector.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,12 @@ public void reset() {
219219
e.printStackTrace();
220220
}
221221
}
222+
223+
public void aclDisconnect(String mac) {
224+
try {
225+
boundService.aclDisconnect(mac);
226+
} catch (RemoteException e) {
227+
e.printStackTrace();
228+
}
229+
}
222230
}

0 commit comments

Comments
 (0)