Skip to content

Commit c138530

Browse files
committed
SPenActions: Handle all known S Pen types automatically
Change-Id: I776a7b49bb17dd24f66bcf7983bcaf86dbc4a876
1 parent 0395710 commit c138530

4 files changed

Lines changed: 53 additions & 22 deletions

File tree

packages/SPenActions/res/values/config.xml

Lines changed: 0 additions & 9 deletions
This file was deleted.

packages/SPenActions/src/org/lineageos/spenactions/BluetoothUtils.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2022 The LineageOS Project
2+
* SPDX-FileCopyrightText: 2022-2026 The LineageOS Project
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

@@ -22,6 +22,7 @@
2222
import android.os.UserHandle;
2323
import android.util.Log;
2424

25+
import java.util.ArrayList;
2526
import java.util.List;
2627
import java.util.UUID;
2728

@@ -62,15 +63,17 @@ public void onScanResult(int callbackType, ScanResult result) {
6263
simulateReconnection(context);
6364
}
6465
};
65-
ScanFilter filter = new ScanFilter.Builder()
66-
.setServiceUuid(new ParcelUuid(UUID.fromString(
67-
context.getResources().getString(R.string.config_sPenServiceUuid))))
68-
.build();
66+
List<ScanFilter> filters = new ArrayList<>();
67+
for (UUID uuid : SPenIdentity.getServiceUUIDs()) {
68+
filters.add(new ScanFilter.Builder()
69+
.setServiceUuid(new ParcelUuid(uuid))
70+
.build());
71+
}
6972
ScanSettings settings = new ScanSettings.Builder()
7073
.setCallbackType(ScanSettings.CALLBACK_TYPE_FIRST_MATCH)
7174
.build();
7275

73-
scanner.startScan(List.of(filter), settings, callback);
76+
scanner.startScan(filters, settings, callback);
7477
new Handler(Looper.getMainLooper()).postDelayed(() -> {
7578
scanner.stopScan(callback);
7679
}, 60 * 1000); // Cancel scan after 60s

packages/SPenActions/src/org/lineageos/spenactions/SPenGattCallback.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ public class SPenGattCallback extends BluetoothGattCallback {
4848
private final Context mContext;
4949
private final SPenConnectionManager mConnectionManager;
5050
private final List<UUID> mEnableCharacteristics = new ArrayList<>();
51-
private final UUID mServiceUUID;
51+
52+
private UUID mServiceUUID;
5253

5354
private boolean mButtonHadGesture = false;
5455
private long mButtonDownTime = 0;
@@ -59,9 +60,6 @@ public SPenGattCallback(SPenConnectionManager connectionManager, Context context
5960

6061
mEnableCharacteristics.add(BATTERY_LEVEL);
6162
mEnableCharacteristics.add(BUTTON_EVENT);
62-
63-
mServiceUUID = UUID.fromString(
64-
context.getResources().getString(R.string.config_sPenServiceUuid));
6563
}
6664

6765
@Override
@@ -85,13 +83,21 @@ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState
8583

8684
@Override
8785
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
88-
BluetoothGattService service = gatt.getService(mServiceUUID);
89-
if (service != null) {
86+
for (BluetoothGattService service : gatt.getServices()) {
87+
UUID uuid = service.getUuid();
88+
if (SPenIdentity.isSPenService(uuid)) {
89+
mServiceUUID = uuid;
90+
break;
91+
}
92+
}
93+
94+
if (mServiceUUID != null) {
9095
Log.i(LOG_TAG, "Found S Pen GATT service: " + mServiceUUID);
9196
if (mEnableCharacteristics.size() > 0) {
9297
Log.i(LOG_TAG, "Enabling characteristic notifications");
9398
setCharacteristicNotification(gatt,
94-
service.getCharacteristic(mEnableCharacteristics.get(0)), true);
99+
gatt.getService(mServiceUUID)
100+
.getCharacteristic(mEnableCharacteristics.get(0)), true);
95101
}
96102
} else {
97103
Log.e(LOG_TAG, "Unable to find S Pen GATT service!");
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2026 The LineageOS Project
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.lineageos.spenactions;
7+
8+
import java.util.Set;
9+
import java.util.UUID;
10+
11+
public final class SPenIdentity {
12+
13+
// List of known S Pen GATT services
14+
private static final Set<UUID> SERVICE_UUIDS = Set.of(
15+
UUID.fromString("0000fd6c-0000-1000-8000-00805f9b34fb"), // EXT1/CANVAS/STICKY
16+
UUID.fromString("dc6bb0a8-202e-487c-8f1b-53b37cc837c6"), // DAVINCI
17+
UUID.fromString("edfec62e-9910-0bac-5241-d8bda6932a2f"), // GENERIC_BUNDLED/CROWN
18+
UUID.fromString("edfec62e-9910-0bac-5241-d8bda6932a30") // GENERIC_UNBUNDLED
19+
);
20+
21+
private SPenIdentity() {
22+
}
23+
24+
public static boolean isSPenService(UUID uuid) {
25+
return SERVICE_UUIDS.contains(uuid);
26+
}
27+
28+
public static Set<UUID> getServiceUUIDs() {
29+
return SERVICE_UUIDS;
30+
}
31+
}

0 commit comments

Comments
 (0)