黄学彪
2021-03-15 2f05a4874e64b3c2e60055e1246025f8a0367f78
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
using HDL_ON.Stan;
using Shared;
using System;
using System.Collections.Generic;
using System.Text;
 
namespace HDL_ON.UI
{
    /// <summary>
    /// 添加Evoyo的Mini智能遥控器步骤2界面
    /// </summary>
    public class AddMiniRemoteControlDirection2Page : EditorCommonForm
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// wifi名
        /// </summary>
        private string wifiName = string.Empty;
        /// <summary>
        /// wifi密码
        /// </summary>
        private string wifiPassword = string.Empty;
        /// <summary>
        /// 蓝牙设备
        /// </summary>
        private List<HdlBluetoothLogic.BluetoothInfo> listDevice = new List<HdlBluetoothLogic.BluetoothInfo>();
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        /// <param name="i_wifiName">wifi名(别的界面传过来的)</param>
        /// <param name="i_wifiPassword">(别的界面传过来的)</param>
        public void ShowForm(string i_wifiName, string i_wifiPassword)
        {
            this.wifiName = i_wifiName;
            this.wifiPassword = i_wifiPassword;
 
            //设置头部信息
            base.SetTitleText(Language.StringByID(StringId.AddInfraredRemoteControl));
            //这个界面的背景需要白色
            bodyFrameLayout.BackgroundColor = UI.CSS.CSS_Color.MainBackgroundColor;
 
            //初始化中部信息
            this.InitMiddleFrame();
        }
 
        /// <summary>
        /// 初始化中部信息
        /// </summary>
        private void InitMiddleFrame()
        {
            //清空body
            this.ClearBodyFrame();
 
            //进度条
            var btnProgress = new ProgressRowBar(180, 6);
            btnProgress.Y = Application.GetRealHeight(218);
            btnProgress.Gravity = Gravity.CenterHorizontal;
            bodyFrameLayout.AddChidren(btnProgress);
            btnProgress.StartMode1(true);
 
            //设备搜索中...
            var btnSearch = new NormalViewControl(bodyFrameLayout.Width, Application.GetRealHeight(20), false);
            btnSearch.Y = btnProgress.Bottom + Application.GetRealHeight(40);
            btnSearch.TextColor = CSS.CSS_Color.FirstLevelTitleColor;
            btnSearch.TextID = StringId.SearchingDevice;
            btnSearch.TextAlignment = TextAlignment.Center;
            bodyFrameLayout.AddChidren(btnSearch);
 
            //请确保您的蓝牙已开启并处于可以被搜索状态
            var strMsg = Language.StringByID(StringId.AddInfraredRemoteControlMsg2);
            this.AddListMsgControls(bodyFrameLayout, strMsg, CSS.CSS_FontSize.TextFontSize,
                CSS.CSS_Color.FirstLevelTitleColor, Application.GetRealHeight(20), btnSearch.Bottom + Application.GetRealHeight(4));
 
            //开启进度条特效
            this.StartProgressSpecialEffects(btnProgress);
        }
 
        #endregion
 
        #region ■ 搜索蓝牙___________________________
 
        /// <summary>
        /// 搜索蓝牙
        /// </summary>
        private void StartSearchBluetooth()
        {
            //开始搜索蓝牙
            HdlBluetoothLogic.Current.ScanBluetooth(2, (listBluetooth) =>
            {
                foreach (var device in listBluetooth)
                {
                    //只有这个标识,才是红外宝
                    if (device.Name.StartsWith("MIR01R-LK.10") == true)
                    {
                        //是指定的红外宝设备
                        listDevice.Add(device);
                    }
                }
                if (listDevice.Count == 0)
                {
                    //摧毁蓝牙缓存
                    HdlBluetoothLogic.Current.Dispone();
                }
                else if (listDevice.Count == 1)
                {
                    //如果只检测到一个,则直接连接
                    HdlBluetoothLogic.Current.ContectBluetooth(listDevice[0], (result) =>
                    {
                        if (result == true && this.Parent != null)
                        {
                            //连接成功,则跳转到下一个界面
                            HdlThreadLogic.Current.RunMain(() =>
                            {
                                this.CloseForm();
                                var form = new AddMiniRemoteControlDirection3Page();
                                form.AddForm(this.wifiName, this.wifiPassword);
                            });
                        }
                        else
                        {
                            HdlBluetoothLogic.Current.Dispone();
                        }
                    });
                }
                else
                {
                    var listName = new List<string>();
                    foreach (var device in listDevice)
                    {
                        listName.Add(device.Name);
                    }
                    //显示选择蓝牙列表的界面(当匹配到多个蓝牙的时候使用)
                    this.ShowBluetoothListView(listName, (index) =>
                    {
                        //如果只检测到一个,则直接连接
                        HdlBluetoothLogic.Current.ContectBluetooth(listDevice[index], (result) =>
                        {
                            if (result == true)
                            {
                                //连接成功,则跳转到下一个界面
                                HdlThreadLogic.Current.RunMain(() =>
                                {
                                    this.CloseForm();
                                    var form = new AddMiniRemoteControlDirection3Page();
                                    form.AddForm(this.wifiName, this.wifiPassword);
                                });
                            }
                            else
                            {
                                HdlBluetoothLogic.Current.Dispone();
                            }
                        });
                    });
                }
            });
        }
 
        /// <summary>
        /// 开启进度条特效
        /// </summary>
        private void StartProgressSpecialEffects(ProgressRowBar btnProgress)
        {
            //搜索时间(秒)
            int searchTime = 180;
            HdlThreadLogic.Current.RunThread(() =>
            {
                //进度条特效
                for (int i = 1; i <= searchTime; i++)
                {
                    System.Threading.Thread.Sleep(1000);
                    HdlThreadLogic.Current.RunMain(() =>
                    {
                        //进度条特效
                        btnProgress.SetValue(i, searchTime);
                    });
 
                    //界面关闭
                    if (this.Parent == null)
                    {
                        break;
                    }
                    //如果已经搜索到蓝牙,则直接到100%
                    if (listDevice.Count > 0)
                    {
                        HdlThreadLogic.Current.RunMain(() =>
                        {
                            btnProgress.SetValue(searchTime, searchTime);
                        });
                        break;
                    }
                    if (i % 5 == 1)
                    {
                        //每5秒搜索一次
                        this.StartSearchBluetooth();
                    }
                    //超时
                    if (i >= searchTime)
                    {
                        System.Threading.Thread.Sleep(3000);
                        //最后的时间还是搜不到的话
                        if (listDevice.Count == 0)
                        {
                            //显示失败界面
                            HdlThreadLogic.Current.RunMain(() =>
                            {
                                this.ShowFailView();
                            });
                        }
                        break;
                    }
                }
            });
        }
 
        #endregion
 
        #region ■ 显示选择蓝牙列表的界面_____________
 
        /// <summary>
        /// 显示选择蓝牙列表的界面(当匹配到多个蓝牙的时候使用)
        /// </summary>
        /// <param name="listName">蓝牙名字列表</param>
        /// <param name="selectEvent">选择事件(考虑到Ios和安卓,所以这样定参数)</param>
        private void ShowBluetoothListView(List<string> listName, Action<int> selectEvent)
        {
            if (this.Parent == null) { return; }
 
            HdlThreadLogic.Current.RunMain(() =>
            {
                var contr = new BottomItemSelectControl(listName.Count, Language.StringByID(StringId.ChooseInfraredRemoteControl), false);
                //初始化
                contr.AddRowMenu(listName, new List<int>());
                contr.FinishOnlyEvent += (div, value) =>
                {
                    if (div == 0)
                    {
                        //如果点击了取消,则关闭这个界面,不管了
                        this.CloseForm();
                    }
                    else
                    {
                        //调用回调函数
                        selectEvent?.Invoke(value);
                    }
                };
            });
        }
 
        #endregion
 
        #region ■ 显示失败界面_______________________
 
        /// <summary>
        /// 显示失败界面
        /// </summary>
        private void ShowFailView()
        {
            //清空body
            this.ClearBodyFrame();
            //图片
            var btnPic = new PicViewControl(180, 180);
            btnPic.Y = Application.GetRealHeight(48);
            btnPic.Gravity = Gravity.CenterHorizontal;
            btnPic.UnSelectedImagePath = "Public/TipIcon_Failed.png";
            bodyFrameLayout.AddChidren(btnPic);
            //没有发现红外遥控器
            var btnMsgNot = new NormalViewControl(bodyFrameLayout.Width, Application.GetRealHeight(22), false);
            btnMsgNot.Y = btnPic.Bottom + Application.GetRealHeight(16);
            btnMsgNot.TextAlignment = TextAlignment.Center;
            btnMsgNot.TextColor = CSS.CSS_Color.AuxiliaryColor2;
            btnMsgNot.TextSize = CSS.CSS_FontSize.SubheadingFontSize;
            btnMsgNot.TextID = StringId.NoInfraredRemoteControlFound;
            bodyFrameLayout.AddChidren(btnMsgNot);
            //1、请检查设备是否正常通电
            //2、请检查蓝牙功能是否正常开启
            //3、请检查指示灯是否蓝色快闪状态
            var strMsg = Language.StringByID(StringId.AddInfraredRemoteControlMsg3);
            this.AddListMsgControls(bodyFrameLayout, strMsg, CSS.CSS_FontSize.TextFontSize,
                CSS.CSS_Color.TextualColor, Application.GetRealHeight(20), btnMsgNot.Bottom + Application.GetRealHeight(8),
                 TextAlignment.Center, true);
 
            //重新搜索
            var btnReSearch = this.AddBottomClickButton(Language.StringByID(StringId.ReSearch));
            btnReSearch.ButtonClickEvent += (sender, e) =>
            {
                //检测蓝牙需要的东西
                btnReSearch.CanClick = false;
                HdlBluetoothLogic.Current.CheckCanScanBluetooth((result) =>
                {
                    btnReSearch.CanClick = true;
                    if (result == true)
                    {
                        //初始化中部信息
                        this.InitMiddleFrame();
                    }
                });
            };
        }
 
        #endregion
    }
}