This repository was archived by the owner on Nov 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBluetoothControl.cs
More file actions
131 lines (110 loc) · 3.88 KB
/
BluetoothControl.cs
File metadata and controls
131 lines (110 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
namespace UnityAndroidBluetooth {
public class BluetoothControl : MonoBehaviour
{
private static BluetoothControl _instance;
private BluetoothServer server;
private List<ControlButton> buttons;
public static BluetoothControl Instance {
get { return _instance; }
}
public static BluetoothServer Server {
get {
return _instance.server;
}
}
public ControlButton GetButton(string name){
foreach(ControlButton btn in buttons){
if (btn.Name == name) return btn;
}
return null;
}
public void StartServer() {
server.Start();
}
public void StopServer(){
server.Stop();
}
void Awake()
{
if (Application.platform != RuntimePlatform.Android) return;
if (_instance != null && _instance != this) {
DestroyImmediate(this.gameObject);
} else {
_instance = this;
_instance.server = new BluetoothServer();
DontDestroyOnLoad(this.gameObject);
}
}
void Start()
{
Server.MessageReceived += MessageReceivedHandler;
}
void MessageReceivedHandler(object sender, MessageReceivedEventArgs e) {
string[] message = e.Message.Split(':');
BluetoothDevice from = e.Sender;
foreach (ControlButton btn in buttons) {
if (message[0] == btn.SymbolicName) {
if (message[1] == "1") {
btn.IsClicked = true;
btn.IsPressed = true;
StartCoroutine(ToggleClick(btn));
break;
} else {
btn.IsPressed = false;
}
if (btn is ControlJoystick) {
ControlJoystick joystick = btn as ControlJoystick;
joystick.DeltaX = double.Parse(message[2]);
joystick.DeltaY = double.Parse(message[3]);
} else if (btn is ControlTrigger) {
ControlTrigger trigger = btn as ControlTrigger;
trigger.Value = double.Parse(message[2]);
}
}
}
}
IEnumerator ToggleClick(ControlButton btn) {
if (btn.IsClicked) {
yield return null;
btn.IsClicked = false;
}
}
}
public class ControlButton {
public string Name { get; set; }
public string SymbolicName { get; set; }
public bool IsPressed { get; set; }
public bool IsClicked { get; set; }
public ControlButton(string name, string symbolicName) {
Name = name;
SymbolicName = symbolicName;
IsPressed = false;
IsClicked = false;
}
public ControlButton(string name) : this(name, name) { }
}
public class ControlJoystick : ControlButton
{
public ControlJoystick(string name) : this(name, name) { }
public ControlJoystick(string name, string symbolicName) : base(name, symbolicName)
{
DeltaX = 0;
DeltaY = 0;
}
public double DeltaX { get; set; }
public double DeltaY { get; set; }
}
public class ControlTrigger : ControlButton
{
public ControlTrigger(string name) : this(name, name) { }
public ControlTrigger(string name, string symbolicName) : base(name, symbolicName)
{
Value = 0;
}
public double Value { get; set; }
}
}