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

Commit 8df6262

Browse files
committed
Add View class (in MVC Model) and Get byte[] from android (not String)
1 parent 5538902 commit 8df6262

File tree

5 files changed

+173
-57
lines changed

5 files changed

+173
-57
lines changed

Assets/Scripts/Bluetooth/Bluetooth.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,12 @@ public void Stop() {
7676
_activityObject.Call("stopThread");
7777
}
7878

79-
public void showMessage(string mes) {
79+
public void ShowMessage(string mes) {
8080
_activityObject.Call("showMessage",mes);
8181
}
82-
82+
83+
public byte[] GetPacketData() {
84+
return _activityObject.Call<byte[]>("GetPacketData");
85+
}
86+
8387
}

Assets/Scripts/Bluetooth/BluetoothModel.cs

+18-30
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
using System.Collections;
44
using System.Collections.Generic;
55
using System.Text;
6+
using System.Text.RegularExpressions;
67

78
public interface IBtObserver {
89
void OnStateChanged(string _State);
910
void OnSendMessage(string _Message);
10-
void OnGetMessage(string _Message);
11+
void OnGetMessage(byte[] _packet);
1112
void OnFoundNoDevice();
1213
void OnScanFinish();
1314
void OnFoundDevice();
@@ -20,49 +21,35 @@ public abstract class BtObservable : MonoBehaviour {
2021
}
2122

2223
public class BluetoothModel : BtObservable {
24+
25+
private Bluetooth bluetooth;
2326

2427
[SerializeField]
25-
private int bufferSize = 256;
28+
private int packetSize = 34;
2629
[SerializeField]
2730
private char startChar = '$';
2831
[SerializeField]
2932
private char endChar = '#';
3033

34+
private List<byte> buffer = null;
35+
private bool updateQueue = false;
36+
3137
public List<string> macAddresses = null;
3238
private StringBuilder rawMessage = null;
3339

34-
void Awake() {
35-
this.observerList = new List<IBtObserver>();
40+
private void Awake() {
41+
this.bluetooth = Bluetooth.getInstance();
3642

43+
this.observerList = new List<IBtObserver>();
3744
this.macAddresses = new List<string>();
38-
this.rawMessage = new StringBuilder(this.bufferSize);
39-
}
4045

46+
this.buffer = new List<byte>();
47+
}
48+
4149
public void clearMacAddresses() {
4250
macAddresses.Clear();
4351
}
4452

45-
private void CheckMessageFormat() {
46-
int startPos = -1;
47-
int endPos = -1;
48-
49-
for(int i = 0; i < rawMessage.Length; ++i) {
50-
if(startPos == -1 && rawMessage[i] == this.startChar) {
51-
startPos = i;
52-
}
53-
else if(endPos == -1 && rawMessage[i] == this.endChar) {
54-
endPos = i;
55-
}
56-
}
57-
58-
if(startPos != -1 && endPos != -1) {
59-
for (int i = 0; i < this.observerList.Count; ++i) {
60-
this.observerList[i].OnGetMessage(rawMessage.ToString(startPos, endPos - startPos + 1));
61-
}
62-
rawMessage.Remove(0, endPos+1);
63-
}
64-
}
65-
6653
// ========================================
6754
// Pattern Method
6855
// ========================================
@@ -99,9 +86,10 @@ void OnSendMessage(string _Message) {
9986
}
10087

10188
void OnReadMessage(string _Message) {
102-
this.rawMessage.Append(_Message);
103-
this.CheckMessageFormat();
104-
Debug.Log("On Read Message : " + _Message);
89+
byte[] temp = bluetooth.GetPacketData();
90+
for (int i = 0; i < this.observerList.Count; ++i) {
91+
this.observerList[i].OnGetMessage(temp);
92+
}
10593
}
10694

10795
void OnFoundNoDevice(string _s) {

Assets/Scripts/BluetoothController.cs

+47-25
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,59 @@
44
using System.Collections;
55
using System.Collections.Generic;
66

7-
public class BluetoothController : MonoBehaviour, IBtObserver {
7+
8+
public class BluetoothController : MonoBehaviour, IBtObserver, IUiObserver {
89

910
private Bluetooth bluetooth;
1011

1112
[SerializeField]
1213
private BluetoothModel bluetoothModel;
13-
1414
[SerializeField]
15-
private Dropdown deviceDropdown;
15+
private BluetoothView bluetoothView;
1616

17-
[SerializeField]
18-
private Button searchButton;
17+
private Quaternion qTemp = new Quaternion();
18+
private Vector3 pTemp = new Vector3();
1919

20-
[SerializeField]
21-
private Button connectButton;
20+
private Queue<byte[]> messageQueue = null;
2221

23-
[SerializeField]
24-
public Text bluetoothMessage;
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+
27+
float sTemp_x = 0.0f;
28+
float sTemp_y = 0.0f;
29+
float sTemp_z = 0.0f;
2530

2631
private void Awake() {
2732
this.bluetooth = Bluetooth.getInstance();
33+
messageQueue = new Queue<byte[]>();
2834
}
2935

3036
private void Start() {
3137
this.bluetoothModel.AddObserver(this);
32-
this.deviceDropdown.ClearOptions();
33-
34-
this.searchButton.onClick.AddListener(
35-
() => {
36-
this.bluetooth.SearchDevice();
37-
});
38+
this.bluetoothView.AddObserver(this);
39+
}
3840

39-
this.connectButton.onClick.AddListener(
40-
() => {
41-
this.bluetooth.Connect(this.deviceDropdown.options[this.deviceDropdown.value].text);
42-
});
41+
private void Update() {
42+
if(messageQueue.Count > 0) {
43+
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);
53+
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);
56+
57+
bluetoothView.infoUpdate(qTemp, pTemp);
58+
59+
}
4360
}
4461

4562
public void OnStateChanged(string _State) {
@@ -48,9 +65,8 @@ public void OnStateChanged(string _State) {
4865
public void OnSendMessage(string _Message) {
4966
}
5067

51-
public void OnGetMessage(string _Message) {
52-
this.bluetoothMessage.text = _Message;
53-
Debug.Log(_Message);
68+
public void OnGetMessage(byte[] _packet) {
69+
messageQueue.Enqueue(_packet);
5470
}
5571

5672
public void OnFoundNoDevice() {
@@ -60,8 +76,14 @@ public void OnScanFinish() {
6076
}
6177

6278
public void OnFoundDevice() {
63-
// Clear and Get new List
64-
deviceDropdown.ClearOptions();
65-
deviceDropdown.AddOptions(this.bluetoothModel.macAddresses);
79+
this.bluetoothView.GetDeviceList(this.bluetoothModel.macAddresses);
80+
}
81+
82+
public void OnSearchDevice() {
83+
this.bluetooth.SearchDevice();
84+
}
85+
86+
public void OnConnectDevice(string _device) {
87+
this.bluetooth.Connect(_device);
6688
}
6789
}

Assets/Scripts/BluetoothView.cs

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using UnityEngine;
2+
using UnityEngine.UI;
3+
using System;
4+
using System.Collections;
5+
using System.Collections.Generic;
6+
7+
public interface IUiObserver {
8+
void OnSearchDevice();
9+
void OnConnectDevice(string _device);
10+
}
11+
12+
public abstract class UiObservable : MonoBehaviour {
13+
protected List<IUiObserver> observerList;
14+
public abstract void AddObserver(IUiObserver _observer);
15+
public abstract void RemoveObserver(IUiObserver _observer);
16+
public abstract void GetDeviceList(List<string> _deviceList);
17+
}
18+
19+
public class BluetoothView : UiObservable {
20+
21+
[SerializeField]
22+
private Dropdown deviceDropdown;
23+
24+
[SerializeField]
25+
private Button searchButton;
26+
27+
[SerializeField]
28+
private Button connectButton;
29+
30+
[SerializeField]
31+
public List<Text> infoTexts;
32+
33+
[SerializeField]
34+
private GameObject cube;
35+
36+
private void Awake() {
37+
this.observerList = new List<IUiObserver>();
38+
}
39+
40+
// Use this for initialization
41+
private void Start () {
42+
this.deviceDropdown.ClearOptions();
43+
44+
this.searchButton.onClick.AddListener(() => {
45+
for (int i = 0; i < this.observerList.Count; ++i) {
46+
this.observerList[i].OnSearchDevice();
47+
}
48+
});
49+
50+
this.connectButton.onClick.AddListener(() => {
51+
for (int i = 0; i < this.observerList.Count; ++i) {
52+
this.observerList[i].OnConnectDevice(this.deviceDropdown.options[this.deviceDropdown.value].text);
53+
}
54+
});
55+
}
56+
57+
void Update () {
58+
59+
}
60+
61+
public void infoUpdate(Quaternion _rotation, Vector3 _position) {
62+
infoTexts[0].text = _rotation.x.ToString();
63+
infoTexts[1].text = _rotation.y.ToString();
64+
infoTexts[2].text = _rotation.z.ToString();
65+
66+
infoTexts[3].text = _position.x.ToString();
67+
infoTexts[4].text = _position.y.ToString();
68+
infoTexts[5].text = _position.z.ToString();
69+
cube.transform.rotation = _rotation;
70+
}
71+
72+
// ========================================
73+
// Pattern Method
74+
// ========================================
75+
76+
public override void AddObserver(IUiObserver _observer) {
77+
observerList.Add(_observer);
78+
}
79+
80+
public override void RemoveObserver(IUiObserver _observer) {
81+
if (observerList.Contains(_observer)) {
82+
this.observerList.Remove(_observer);
83+
}
84+
}
85+
86+
public override void GetDeviceList(List<string> _deviceList) {
87+
this.deviceDropdown.ClearOptions();
88+
this.deviceDropdown.AddOptions(_deviceList);
89+
}
90+
}

Assets/Scripts/BluetoothView.cs.meta

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)