Skip to content

Commit 05f76ca

Browse files
committed
rough ble implementation
1 parent 9d96a77 commit 05f76ca

10 files changed

Lines changed: 867 additions & 0 deletions

File tree

play-services-wearable/core/src/main/java/org/microg/gms/wearable/bluetooth/BleConnectionManager.java

Lines changed: 675 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.microg.gms.wearable.bluetooth;
2+
3+
import com.google.android.gms.wearable.ConnectionConfiguration;
4+
5+
public interface BleConnectionManagerInterface {
6+
void updateConfiguration(ConnectionConfiguration config);
7+
void quit();
8+
void quitSafely();
9+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.microg.gms.wearable.bluetooth;
2+
3+
public class BleException extends Exception {
4+
public static final int CODE_UNKNOWN = -1;
5+
public static final int CODE_GATT_INVALID_HANDLE = 1;
6+
public static final int CODE_GATT_READ_NOT_PERMITTED = 2;
7+
public static final int CODE_GATT_WRITE_NOT_PERMITTED = 3;
8+
9+
public static final int CODE_TIME_SERVICE_NOT_FOUND = 256;
10+
public static final int CODE_MISSING_CLOCKWORK_CHARS = 258;
11+
public static final int CODE_INVALID_DECOMMISSION = 259;
12+
public static final int CODE_SERVICE_NOT_FOUND = 260;
13+
public static final int CODE_TIME_CHAR_INVALID = 261;
14+
public static final int CODE_TIMEZONE_OFFSET_INVALID = 262;
15+
16+
public final int statusCode;
17+
18+
public BleException(String message) {
19+
super(message);
20+
this.statusCode = CODE_UNKNOWN;
21+
}
22+
23+
public BleException(String message, int statusCode) {
24+
super(message);
25+
this.statusCode = statusCode;
26+
}
27+
28+
public BleException(Throwable cause) {
29+
super(cause);
30+
this.statusCode = CODE_UNKNOWN;
31+
}
32+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.microg.gms.wearable.bluetooth;
2+
3+
public interface BleScanner {
4+
boolean isScanning();
5+
void stopScan();
6+
void startScan(String address, ScanListener listener);
7+
8+
interface ScanListener {
9+
void onDeviceFound(String address);
10+
void onScanFailed(int errorCode);
11+
}
12+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.microg.gms.wearable.bluetooth;
2+
3+
public interface BleServicesHandler {
4+
void cleanup();
5+
void updateCurrentTime() throws BleException;
6+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.microg.gms.wearable.bluetooth;
2+
3+
import android.os.Message;
4+
5+
public abstract class BleState {
6+
public abstract String getName();
7+
8+
public void onEnter() {}
9+
10+
public void onExit() {}
11+
12+
public abstract boolean handleMessage(Message msg);
13+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package org.microg.gms.wearable.bluetooth;
2+
3+
import android.os.Handler;
4+
import android.os.Looper;
5+
import android.os.Message;
6+
import android.util.Log;
7+
8+
import java.util.ArrayList;
9+
import java.util.HashMap;
10+
import java.util.List;
11+
import java.util.Map;
12+
13+
public abstract class BleStateMachine extends Handler {
14+
private final String name;
15+
private final Map<BleState, List<BleState>> transitions = new HashMap<>();
16+
private BleState currentState;
17+
private BleState errorState;
18+
19+
protected BleStateMachine(String name, Looper looper) {
20+
super(looper);
21+
this.name = name;
22+
}
23+
24+
protected void addState(BleState state) {
25+
transitions.put(state, new ArrayList<>());
26+
}
27+
28+
protected void addTransition(BleState from, BleState to) {
29+
List<BleState> targets = transitions.get(from);
30+
31+
if (targets == null) {
32+
Log.w(name, "addTransition: unknown source state " + from.getName());
33+
return;
34+
}
35+
36+
if (!targets.contains(to)) {
37+
targets.add(to);
38+
}
39+
}
40+
41+
protected void setErrorState(BleState state){
42+
this.errorState = state;
43+
}
44+
45+
protected void start() {
46+
47+
}
48+
49+
public void transitionTo(BleState next) {
50+
if (currentState != null) {
51+
currentState.onExit();
52+
}
53+
currentState = next;
54+
if (currentState != null) {
55+
currentState.onEnter();
56+
}
57+
}
58+
59+
public BleState currentState() {
60+
return currentState;
61+
}
62+
63+
public void sendMessage(int what) {
64+
sendMessage(obtainMessage(what));
65+
}
66+
67+
public void sendMessageDelayed(int what, long delay) {
68+
sendMessageDelayed(obtainMessage(what), delay);
69+
}
70+
71+
public void sendBtAdapterStateMsg(int state) {
72+
Message msg = obtainMessage(BleConnectionManager.MSG_BT_ADAPTER_STATE_CHANGED);
73+
msg.arg1 = state;
74+
sendMessage(msg);
75+
}
76+
77+
@Override
78+
public void handleMessage(Message msg) {
79+
if (currentState != null && !currentState().handleMessage(msg)) {
80+
Log.w(name, "Unhandled message " + msg.what + " in state " + currentState.getName());
81+
}
82+
}
83+
84+
protected String getMessageName(int what) {
85+
return String.valueOf(what);
86+
}
87+
88+
protected boolean shouldLogMessage(Message msg) {
89+
return true;
90+
}
91+
92+
protected void onQuiting() {}
93+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.microg.gms.wearable.bluetooth;
2+
3+
public class BleTimeoutException extends BleException {
4+
public BleTimeoutException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.microg.gms.wearable.bluetooth;
2+
3+
public interface BluetoothGattHelper {
4+
boolean isConnected();
5+
6+
void disconnect() throws BleException;
7+
void discoverServices() throws BleException;
8+
void refreshGatt();
9+
void setGattEventListener(GattEventListener listener);
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.microg.gms.wearable.bluetooth;
2+
3+
import android.bluetooth.BluetoothGattCharacteristic;
4+
5+
public interface GattEventListener {
6+
void onCharacteristicChanged(BluetoothGattCharacteristic characteristic);
7+
void onServiceChanged();
8+
void onCharacteristicWritten(BluetoothGattCharacteristic characteristic);
9+
void onServicesDiscovered();
10+
}

0 commit comments

Comments
 (0)