Skip to content

Commit b5a962f

Browse files
committed
蓝牙相关api-demo
1 parent 82c79c8 commit b5a962f

17 files changed

+2065
-5
lines changed

Demo/API_V2/Assets/API/Facility/BLE.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using LitJson;
4+
using UnityEngine;
5+
using WeChatWASM;
6+
public class BLE : Details
7+
{
8+
9+
private bool _isListening = false;
10+
11+
private readonly Action<OnBLEMTUChangeListenerResult> _onBLEMTUChange = (res) => {
12+
var result = "onBLEMTUChange\n" + JsonMapper.ToJson(res);
13+
GameManager.Instance.detailsController.AddResult(new ResultData()
14+
{
15+
initialContentText = result
16+
});
17+
};
18+
19+
private readonly Action<OnBLEConnectionStateChangeListenerResult> _onBLEConnectionStateChange = (res) => {
20+
var result = "onBLEConnectionStateChange\n" + JsonMapper.ToJson(res);
21+
GameManager.Instance.detailsController.AddResult(new ResultData()
22+
{
23+
initialContentText = result
24+
});
25+
};
26+
27+
private readonly Action<OnBLECharacteristicValueChangeListenerResult> _onBLECharacteristicValueChange = (res) => {
28+
var result = "onBLECharacteristicValueChange\n" + JsonMapper.ToJson(res);
29+
GameManager.Instance.detailsController.AddResult(new ResultData()
30+
{
31+
initialContentText = result
32+
});
33+
};
34+
35+
36+
private void Start()
37+
{
38+
// 监听蓝牙低功耗的最大传输单元变化事件(仅安卓触发)
39+
WX.OnBLEMTUChange(_onBLEMTUChange);
40+
41+
// 监听蓝牙低功耗连接状态改变事件。包括开发者主动连接或断开连接,设备丢失,连接异常断开等等
42+
WX.OnBLEConnectionStateChange(_onBLEConnectionStateChange);
43+
44+
// 监听蓝牙低功耗设备的特征值变化事件。
45+
WX.OnBLECharacteristicValueChange(_onBLECharacteristicValueChange);
46+
47+
GameManager.Instance.detailsController.BindExtraButtonAction(0, writeBLECharacteristicValue);
48+
GameManager.Instance.detailsController.BindExtraButtonAction(1, setBLEMTU);
49+
GameManager.Instance.detailsController.BindExtraButtonAction(2, readBLECharacteristicValue);
50+
GameManager.Instance.detailsController.BindExtraButtonAction(3, notifyBLECharacteristicValueChange);
51+
GameManager.Instance.detailsController.BindExtraButtonAction(4, getBLEMTU);
52+
GameManager.Instance.detailsController.BindExtraButtonAction(5, getBLEDeviceServices);
53+
GameManager.Instance.detailsController.BindExtraButtonAction(6, getBLEDeviceRSSI);
54+
GameManager.Instance.detailsController.BindExtraButtonAction(7, getBLEDeviceCharacteristics);
55+
}
56+
57+
// 测试 API
58+
protected override void TestAPI(string[] args)
59+
{
60+
createBLEConnection();
61+
}
62+
63+
public void createBLEConnection() {
64+
if (!_isListening) {
65+
WX.CreateBLEConnection(new CreateBLEConnectionOption
66+
{
67+
deviceId = "xxx",
68+
timeout = 20000,
69+
success = (res) => {
70+
Debug.Log("success " + JsonMapper.ToJson(res));
71+
},
72+
fail = (res) => {
73+
Debug.Log("fail" + res.errMsg);
74+
},
75+
complete = (res) => {
76+
Debug.Log("complete");
77+
}
78+
});
79+
} else {
80+
WX.CloseBLEConnection(new CloseBLEConnectionOption
81+
{
82+
deviceId = "xxx",
83+
success = (res) => {
84+
Debug.Log("success " + JsonMapper.ToJson(res));
85+
},
86+
fail = (res) => {
87+
Debug.Log("fail" + res.errMsg);
88+
},
89+
complete = (res) => {
90+
Debug.Log("complete");
91+
}
92+
});
93+
}
94+
_isListening = !_isListening;
95+
GameManager.Instance.detailsController.ChangeInitialButtonText(_isListening ? "断开连接" : "开始连接");
96+
}
97+
98+
// 目前会报param.value类型错误,已知问题,等待修复
99+
public void writeBLECharacteristicValue() {
100+
WX.WriteBLECharacteristicValue(new WriteBLECharacteristicValueOption
101+
{
102+
deviceId = "xxx",
103+
serviceId = "xxx",
104+
characteristicId = "xxx",
105+
value = new byte[] {1, 2, 3},
106+
success = (res) => {
107+
Debug.Log("success " + JsonMapper.ToJson(res));
108+
},
109+
fail = (res) => {
110+
Debug.Log("fail" + res.errMsg);
111+
},
112+
complete = (res) => {
113+
Debug.Log("complete");
114+
}
115+
});
116+
}
117+
118+
public void setBLEMTU() {
119+
WX.SetBLEMTU(new SetBLEMTUOption
120+
{
121+
deviceId = "xx",
122+
mtu = 100,
123+
success = (res) => {
124+
Debug.Log("success " + JsonMapper.ToJson(res));
125+
},
126+
fail = (res) => {
127+
Debug.Log("fail" + res.mtu);
128+
},
129+
complete = (res) => {
130+
Debug.Log("complete");
131+
}
132+
});
133+
}
134+
135+
public void readBLECharacteristicValue() {
136+
WX.ReadBLECharacteristicValue(new ReadBLECharacteristicValueOption
137+
{
138+
deviceId = "xx",
139+
serviceId = "xx",
140+
characteristicId = "xx",
141+
success = (res) => {
142+
Debug.Log("success " + JsonMapper.ToJson(res));
143+
},
144+
fail = (res) => {
145+
Debug.Log("fail" + res.errMsg);
146+
},
147+
complete = (res) => {
148+
Debug.Log("complete");
149+
}
150+
});
151+
}
152+
153+
public void notifyBLECharacteristicValueChange() {
154+
WX.NotifyBLECharacteristicValueChange(new NotifyBLECharacteristicValueChangeOption {
155+
deviceId = "xx",
156+
serviceId = "xx",
157+
characteristicId = "xx",
158+
state = true,
159+
type = "indication",
160+
success = (res) => {
161+
Debug.Log("success " + JsonMapper.ToJson(res));
162+
},
163+
fail = (res) => {
164+
Debug.Log("fail" + res.errMsg);
165+
},
166+
complete = (res) => {
167+
Debug.Log("complete");
168+
}
169+
});
170+
}
171+
172+
public void getBLEMTU() {
173+
WX.GetBLEMTU(new GetBLEMTUOption
174+
{
175+
deviceId = "xx",
176+
success = (res) => {
177+
Debug.Log("success " + JsonMapper.ToJson(res));
178+
},
179+
fail = (res) => {
180+
Debug.Log("fail" + res.errMsg);
181+
},
182+
complete = (res) => {
183+
Debug.Log("complete");
184+
}
185+
});
186+
}
187+
188+
public void getBLEDeviceServices() {
189+
WX.GetBLEDeviceServices(new GetBLEDeviceServicesOption
190+
{
191+
deviceId = "xx",
192+
success = (res) => {
193+
Debug.Log("success " + JsonMapper.ToJson(res));
194+
},
195+
fail = (res) => {
196+
Debug.Log("fail" + res.errMsg);
197+
},
198+
complete = (res) => {
199+
Debug.Log("complete");
200+
}
201+
});
202+
}
203+
204+
public void getBLEDeviceRSSI() {
205+
WX.GetBLEDeviceRSSI(new GetBLEDeviceRSSIOption
206+
{
207+
deviceId = "xx",
208+
success = (res) => {
209+
Debug.Log("success " + JsonMapper.ToJson(res));
210+
},
211+
fail = (res) => {
212+
Debug.Log("fail" + res.errMsg);
213+
},
214+
complete = (res) => {
215+
Debug.Log("complete");
216+
}
217+
});
218+
}
219+
220+
public void getBLEDeviceCharacteristics() {
221+
WX.GetBLEDeviceCharacteristics(new GetBLEDeviceCharacteristicsOption
222+
{
223+
deviceId = "xx",
224+
serviceId = "xx",
225+
success = (res) => {
226+
Debug.Log("success " + JsonMapper.ToJson(res));
227+
},
228+
fail = (res) => {
229+
Debug.Log("fail" + res.errMsg);
230+
},
231+
complete = (res) => {
232+
Debug.Log("complete");
233+
}
234+
});
235+
}
236+
}
237+

Demo/API_V2/Assets/API/Facility/BLE/BLE.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!114 &11400000
4+
MonoBehaviour:
5+
m_ObjectHideFlags: 0
6+
m_CorrespondingSourceObject: {fileID: 0}
7+
m_PrefabInstance: {fileID: 0}
8+
m_PrefabAsset: {fileID: 0}
9+
m_GameObject: {fileID: 0}
10+
m_Enabled: 1
11+
m_EditorHideFlags: 0
12+
m_Script: {fileID: 11500000, guid: fb48e4613a53bb941a20036d7c08fefb, type: 3}
13+
m_Name: BLESO
14+
m_EditorClassIdentifier:
15+
entryScriptTypeName: BLE
16+
entryName: "\u84DD\u7259-\u4F4E\u529F\u8017\u4E2D\u5FC3\u8BBE\u5907"
17+
entryAPI: "\u84DD\u7259-\u4F4E\u529F\u8017\u4E2D\u5FC3\u8BBE\u5907\u76F8\u5173api"
18+
entryDescription:
19+
optionList: []
20+
initialButtonText: "\u5F00\u59CB\u8FDE\u63A5"
21+
extraButtonList:
22+
- buttonText: "\u5199\u5165\u6570\u636E"
23+
- buttonText: "\u8BBE\u7F6EMTU"
24+
- buttonText: "\u8BFB\u53D6\u6570\u636E"
25+
- buttonText: "\u542F\u7528 notify \u529F\u80FD"
26+
- buttonText: "\u83B7\u53D6\u6700\u5927\u4F20\u8F93\u5355\u5143"
27+
- buttonText: "\u83B7\u53D6\u84DD\u7259\u8BBE\u5907\u6240\u6709\u670D\u52A1 "
28+
- buttonText: "\u83B7\u53D6\u4FE1\u53F7\u5F3A\u5EA6 "
29+
- buttonText: "\u83B7\u53D6\u6240\u6709\u7279\u5F81 "
30+
initialResultList: []

Demo/API_V2/Assets/API/Facility/BLE/BLESO.asset.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Demo/API_V2/Assets/API/Facility/BLEPeripheral.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)