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 pathBluetoothServer.cs
More file actions
109 lines (95 loc) · 3.85 KB
/
BluetoothServer.cs
File metadata and controls
109 lines (95 loc) · 3.85 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
using System;
using UnityEngine;
namespace UnityAndroidBluetooth {
public class BluetoothServer : Bluetooth {
/* ========== CONSTANTS ========== */
private const string COULD_NOT_ACCEPT = "server.error.COULD_NOT_ACCEPT";
private const string COULD_NOT_LISTEN = "server.error.COULD_NOT_LISTEN";
private const string NOT_LISTENING = "server.not_listening";
private const string LISTENING = "server.listening";
private const string STOPPED = "server.stopped";
/* ========== EVENT HANDLING ========== */
public event EventHandler<ServerStateChangedEventArgs> ServerStateChanged;
public event EventHandler<DeviceInfoEventArgs> ClientConnected;
public event EventHandler<DeviceInfoEventArgs> ClientDisconnected;
protected virtual void OnServerStateChanged(ServerStateChangedEventArgs e)
{
ServerStateChanged?.Invoke(this, e);
}
protected virtual void OnClientConntected(DeviceInfoEventArgs e) {
ClientConnected?.Invoke(this, e);
}
protected virtual void OnClientDisconnected(DeviceInfoEventArgs e) {
ClientDisconnected?.Invoke(this, e);
}
// JNI Interface
protected class OnAndroidServerStatus : Bluetooth.OnAndroidStatus {
BluetoothServer server;
public OnAndroidServerStatus(BluetoothServer s) : base(s) {
server = s;
}
public override void OnStatus(string status){
base.OnStatus(status);
ServerStateChangedEventArgs e = new ServerStateChangedEventArgs();
switch(status) {
case COULD_NOT_LISTEN:
throw new BluetoothException("Server could not start listening");
case LISTENING:
e.State = ServerState.LISTENING;
server.OnServerStateChanged(e);
break;
case NOT_LISTENING:
e.State = ServerState.NOT_LISTENING;
server.OnServerStateChanged(e);
break;
case STOPPED:
e.State = ServerState.STOPPED;
server.OnServerStateChanged(e);
break;
}
string[] tokens = status.Split('.');
DeviceInfoEventArgs e2 = new DeviceInfoEventArgs();
if (tokens[0] == "server" && tokens.Length >= 3) {
e2.Device = Bluetooth.GetDevice(tokens[2]);
if (tokens[1] == "connected") {
server.OnClientConntected(e2);
} else if (tokens[1] == "disconnected") {
server.OnClientDisconnected(e2);
}
}
}
}
public BluetoothServer() : base("com.guevara.bluetooth.BluetoothServer") {
SetOnAndroidStatus(new OnAndroidServerStatus(this));
}
/* ========== JNI METHODS ========== */
public void Start()
{
PluginInstance.Call("start");
}
public void Start(string name)
{
PluginInstance.Call("start", name);
}
public void Start(string name, string uuid)
{
PluginInstance.Call("start", name, uuid);
}
public void Stop()
{
PluginInstance.Call("stop");
}
public void SetMaxConnections(int value)
{
PluginInstance.Call("setMaxConnections", value);
}
public void SendToAll(string message)
{
PluginInstance.Call("sendAll", message);
}
public void SendTo(string address, string message)
{
PluginInstance.Call("sendTo", message, address);
}
}
}