|
| 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 | + |
0 commit comments