Skip to content
This repository was archived by the owner on May 28, 2019. It is now read-only.

Commit e211e77

Browse files
committed
[Unity] Change unity C# code (filter packet data and filter setting)
1 parent ab386fc commit e211e77

File tree

4 files changed

+96
-67
lines changed

4 files changed

+96
-67
lines changed

Assets/Scripts/Bluetooth/Bluetooth.cs

+1-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Collections;
44
using System.Collections.Generic;
55

6-
76
public class Bluetooth {
87

98
private AndroidJavaClass _plugin;
@@ -19,15 +18,13 @@ public static Bluetooth getInstance() {
1918
return instance;
2019
}
2120

22-
2321
// ========================================
2422
// Call Android Method
2523
// ========================================
2624

2725
private void PluginStart() {
2826
_plugin = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
2927
_activityObject = _plugin.GetStatic<AndroidJavaObject>("currentActivity");
30-
Debug.Log(_activityObject);
3128
_activityObject.Call("StartPlugin");
3229
}
3330

@@ -36,8 +33,7 @@ public string Send(string message) {
3633
}
3734

3835
public string SearchDevice() {
39-
Debug.Log("unity -> android | SearchDevice");
40-
return _activityObject.Call<string>("ScanDevice");
36+
return _activityObject.Call<string>("ScanDevice");
4137
}
4238

4339
public string GetDeviceConnectedName() {

Assets/Scripts/Bluetooth/BluetoothModel.cs

+77-27
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,85 @@
55
using System.Text;
66
using System.Text.RegularExpressions;
77

8+
public enum BluetoothState {
9+
UNABLE_TO_CONNECT = 1,
10+
STATE_CONNECTING = 2,
11+
STATE_CONNECTED = 3
12+
}
13+
814
public interface IBtObserver {
915
void OnStateChanged(string _State);
1016
void OnSendMessage(string _Message);
11-
void OnGetMessage(byte[] _packet);
17+
void OnReadPacket(byte[] _Packet);
1218
void OnFoundNoDevice();
1319
void OnScanFinish();
1420
void OnFoundDevice();
1521
}
1622

1723
public abstract class BtObservable : MonoBehaviour {
1824
protected List<IBtObserver> observerList;
19-
public abstract void AddObserver(IBtObserver _btObserver);
20-
public abstract void RemoveObserver(IBtObserver _btObserver);
25+
public abstract void AddObserver(IBtObserver _BTObserver);
26+
public abstract void RemoveObserver(IBtObserver _BTObserver);
2127
}
2228

2329
public class BluetoothModel : BtObservable {
2430

2531
private Bluetooth bluetooth;
2632

27-
[SerializeField]
28-
private int packetSize = 34;
29-
[SerializeField]
30-
private char startChar = '$';
31-
[SerializeField]
32-
private char endChar = '#';
33+
[SerializeField] private bool isPacketedData = true;
34+
[SerializeField] private int packetSize = 34;
35+
[SerializeField] private char startChar = '$';
36+
[SerializeField] private char endChar = '#';
3337

34-
private List<byte> buffer = null;
35-
private bool updateQueue = false;
38+
private List<byte> byteBuffer = null;
3639

3740
public List<string> macAddresses = null;
38-
private StringBuilder rawMessage = null;
41+
42+
private bool isSearchFinished = true;
3943

4044
private void Awake() {
4145
this.bluetooth = Bluetooth.getInstance();
4246

4347
this.observerList = new List<IBtObserver>();
4448
this.macAddresses = new List<string>();
4549

46-
this.buffer = new List<byte>();
50+
this.byteBuffer = new List<byte>();
51+
}
52+
53+
private void CheckBuffer() {
54+
Debug.Log("-- start check -- ");
55+
if(isPacketedData) {
56+
57+
// Check until buffer size less then packetSize
58+
while(byteBuffer.Count >= packetSize) {
59+
// is Packet?
60+
if(Convert.ToChar(byteBuffer[0]) == startChar && Convert.ToChar(byteBuffer[packetSize-1]) == endChar) {
61+
for (int i = 0; i < this.observerList.Count; ++i) {
62+
this.observerList[i].OnReadPacket(byteBuffer.GetRange(0, packetSize).ToArray());
63+
}
64+
byteBuffer.RemoveRange(0,packetSize);
65+
}
66+
else {
67+
byteBuffer.RemoveAt(0);
68+
}
69+
}
70+
}
71+
else {
72+
// Check data contain escape sqence '\n'
73+
if(byteBuffer.Contains((byte)'\n')) {
74+
int sliceIndex = byteBuffer.IndexOf((byte)'\n');
75+
for (int i = 0; i < this.observerList.Count; ++i) {
76+
this.observerList[i].OnReadPacket(byteBuffer.GetRange(0, sliceIndex + 1).ToArray());
77+
}
78+
byteBuffer.RemoveRange(0, sliceIndex + 1);
79+
}
80+
else {
81+
// Do nothing
82+
}
83+
}
4784
}
4885

49-
public void clearMacAddresses() {
86+
public void ClearMacAddresses() {
5087
macAddresses.Clear();
5188
}
5289

@@ -68,49 +105,62 @@ public override void RemoveObserver(IBtObserver _btObserver) {
68105
// Receive Bluetooth Call Back Method
69106
// ========================================
70107

71-
void OnStateChanged(string _State) {
72-
//"STATE_CONNECTED"
73-
//"STATE_CONNECTING"
74-
//"UNABLE TO CONNECT"
108+
void OnStateChanged(string _state) {
75109
for (int i = 0; i < this.observerList.Count; ++i) {
76-
this.observerList[i].OnStateChanged(_State);
110+
this.observerList[i].OnStateChanged(_state);
77111
}
78-
Debug.Log(_State);
112+
113+
Debug.Log("[BlueToothPlugin] - " + _state);
79114
}
80115

81116
void OnSendMessage(string _Message) {
82117
for (int i = 0; i < this.observerList.Count; ++i) {
83118
this.observerList[i].OnSendMessage(_Message);
84119
}
85-
Debug.Log("On Send Message : " + _Message);
120+
121+
Debug.Log("[BlueToothPlugin] - On Send Message : " + _Message);
86122
}
87123

88124
void OnReadMessage(string _Message) {
89125
byte[] temp = bluetooth.GetPacketData();
90-
for (int i = 0; i < this.observerList.Count; ++i) {
91-
this.observerList[i].OnGetMessage(temp);
92-
}
126+
127+
this.byteBuffer.AddRange(temp);
128+
this.CheckBuffer();
129+
130+
Debug.Log("[BlueToothPlugin] - On Read Message : " + _Message);
93131
}
94132

95133
void OnFoundNoDevice(string _s) {
96134
for (int i = 0; i < this.observerList.Count; ++i) {
97135
this.observerList[i].OnFoundNoDevice();
98136
}
99-
Debug.Log("On Found No Device");
137+
138+
Debug.Log("[BlueToothPlugin] - On Found No Device");
100139
}
101140

102141
void OnScanFinish(string _s) {
142+
143+
this.isSearchFinished = true;
144+
103145
for (int i = 0; i < this.observerList.Count; ++i) {
104146
this.observerList[i].OnScanFinish();
105147
}
106-
Debug.Log("On Scan Finish");
148+
149+
Debug.Log("[BlueToothPlugin] - On Scan Finish");
107150
}
108151

109152
void OnFoundDevice(string _Device) {
153+
if (this.isSearchFinished) {
154+
this.macAddresses.Clear();
155+
this.isSearchFinished = false;
156+
}
157+
110158
this.macAddresses.Add(_Device);
159+
111160
for (int i = 0; i < this.observerList.Count; ++i) {
112161
this.observerList[i].OnFoundDevice();
113162
}
114-
Debug.Log("On Found Device");
163+
164+
Debug.Log("[BlueToothPlugin] - On Found Device");
115165
}
116166
}

Assets/Scripts/BluetoothController.cs

+12-29
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,15 @@ public class BluetoothController : MonoBehaviour, IBtObserver, IUiObserver {
99

1010
private Bluetooth bluetooth;
1111

12-
[SerializeField]
13-
private BluetoothModel bluetoothModel;
14-
[SerializeField]
15-
private BluetoothView bluetoothView;
12+
[SerializeField] private BluetoothModel bluetoothModel;
13+
[SerializeField] private UiObservable bluetoothView;
1614

17-
private Quaternion qTemp = new Quaternion();
1815
private Vector3 pTemp = new Vector3();
1916

2017
private Queue<byte[]> messageQueue = null;
2118

22-
float qTemp_w = 0.0f;
23-
float qTemp_x = 0.0f;
24-
float qTemp_y = 0.0f;
25-
float qTemp_z = 0.0f;
26-
2719
float sTemp_x = 0.0f;
2820
float sTemp_y = 0.0f;
29-
float sTemp_z = 0.0f;
3021

3122
private void Awake() {
3223
this.bluetooth = Bluetooth.getInstance();
@@ -41,21 +32,11 @@ private void Start() {
4132
private void Update() {
4233
if(messageQueue.Count > 0) {
4334
byte[] temp = messageQueue.Dequeue();
44-
45-
this.qTemp_w = BitConverter.ToSingle(temp, 1);
46-
this.qTemp_x = BitConverter.ToSingle(temp, 5);
47-
this.qTemp_y = BitConverter.ToSingle(temp, 9);
48-
this.qTemp_z = BitConverter.ToSingle(temp, 13);
49-
50-
this.sTemp_x = BitConverter.ToSingle(temp, 17);
51-
this.sTemp_y = BitConverter.ToSingle(temp, 21);
52-
this.sTemp_z = BitConverter.ToSingle(temp, 25);
35+
Debug.Log(temp[0] + " / " + temp[1] + " / " + temp[2] + " / " + temp[3]);
5336

54-
qTemp.Set(this.qTemp_x, this.qTemp_y, this.qTemp_z, this.qTemp_w);
55-
pTemp.Set(this.sTemp_x, this.sTemp_y, this.sTemp_z);
37+
pTemp = new Vector3(temp[1], temp[2], 0.0f);
5638

57-
bluetoothView.infoUpdate(qTemp, pTemp);
58-
39+
bluetoothView.UpdateInfo(pTemp);
5940
}
6041
}
6142

@@ -65,25 +46,27 @@ public void OnStateChanged(string _State) {
6546
public void OnSendMessage(string _Message) {
6647
}
6748

68-
public void OnGetMessage(byte[] _packet) {
69-
messageQueue.Enqueue(_packet);
49+
public void OnReadPacket(byte[] _Packet) {
50+
messageQueue.Enqueue(_Packet);
7051
}
7152

7253
public void OnFoundNoDevice() {
54+
// DO SOMETHING
7355
}
7456

7557
public void OnScanFinish() {
58+
// DO SOMETHING
7659
}
7760

7861
public void OnFoundDevice() {
79-
this.bluetoothView.GetDeviceList(this.bluetoothModel.macAddresses);
62+
this.bluetoothView.UpdateDeviceList(this.bluetoothModel.macAddresses);
8063
}
8164

8265
public void OnSearchDevice() {
8366
this.bluetooth.SearchDevice();
8467
}
8568

86-
public void OnConnectDevice(string _device) {
87-
this.bluetooth.Connect(_device);
69+
public void OnConnectDevice(string _Device) {
70+
this.bluetooth.Connect(_Device);
8871
}
8972
}

Assets/Scripts/BluetoothView.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public abstract class UiObservable : MonoBehaviour {
1313
protected List<IUiObserver> observerList;
1414
public abstract void AddObserver(IUiObserver _observer);
1515
public abstract void RemoveObserver(IUiObserver _observer);
16-
public abstract void GetDeviceList(List<string> _deviceList);
16+
public abstract void UpdateDeviceList(List<string> _deviceList);
1717

1818
public abstract void UpdateInfo(Vector3 _position);
1919
}
@@ -62,16 +62,16 @@ public override void RemoveObserver(IUiObserver _observer) {
6262
}
6363
}
6464

65-
public override void GetDeviceList(List<string> _deviceList) {
65+
public override void UpdateDeviceList(List<string> _deviceList) {
6666
this.deviceDropdown.ClearOptions();
6767
this.deviceDropdown.AddOptions(_deviceList);
6868
}
6969

70-
public override void UpdateInfo(Vector3 _position) {
71-
infoTexts[1].text = _position.x.ToString();
72-
infoTexts[2].text = _position.y.ToString();
70+
public override void UpdateInfo(Vector3 _velocity) {
71+
infoTexts[0].text = _velocity.x.ToString();
72+
infoTexts[1].text = _velocity.y.ToString();
7373

74-
character.transform.position = _position;
74+
character.GetComponent<Rigidbody>().velocity = _velocity;
7575
}
7676

7777
}

0 commit comments

Comments
 (0)