/// <summary> /// 3,对外接口:连接BLE设备 /// </summary> /// <param name="MacAddress"></param> /// <returns></returns> public string ConnectDevice(string MacAddress) { RecordLog.GetInstance().WriteLog(Level.Info, string.Format("开始配对蓝牙地址,Mac:{0}", MacAddress)); LicenseHelper licenseHelper = new LicenseHelper(); if (!licenseHelper.IsHaveListenersFile()) { MessageBox.Show("授权文件不存在,请联系系统管理员", "系统提示"); return("Error"); } List <string> CheckData = licenseHelper.ReturnCheckData(); if (CheckData.Count == 0) { MessageBox.Show("授权文件信息不存在,请联系系统管理员", "系统提示"); return("Error"); } string name = string.Empty; string mac = string.Empty; string outCheckValue = string.Empty; //连接地址的时候判断授权文件 foreach (var s in EventClass.GetInstance()._dicdeviceAddress) { if (s.Value.ToString().Equals(MacAddress)) { bool res = licenseHelper.EncrptyData(s.Key.ToString(), s.Value.ToString(), out outCheckValue); if (res == true) { bool res2 = licenseHelper.CheckLockDecValue(outCheckValue, CheckData); if (res2 == true) { RecordLog.GetInstance().WriteLog(Level.Info, "开始连接设备: " + MacAddress.ToString()); EventClass.GetInstance()._ConnectBleAddress = MacAddress; //EventClass.GetInstance()._eventDelegate.OnHandlerConnect(); //return "Success"; string msg = string.Empty; List <lockDetail> objlist = RedvelopRecord.GetInstance().GetlockSetting(out msg); if (objlist == null) { MessageBox.Show("系统尚未配置门锁文件", "系统提示"); return("Error"); } foreach (lockDetail lockDetail in objlist) { if (lockDetail._id.Equals(EventClass.GetInstance()._ConnectBleAddress)) { OpenAppDo.GetInstance().ModifyLockTime(lockDetail._modifyName, lockDetail._id, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); //OpenAppDo.GetInstance().AddLockMap(lockDetail._modifyName, lockDetail._id, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); } } EventClass.GetInstance()._client.Address = Convert.ToInt64(MacAddress, 16); wclBluetoothRadio radio = EventClass.GetInstance()._devices[MacAddress]; if (EventClass.GetInstance()._client.Connect(radio) != 0) { RecordLog.GetInstance().WriteLog(Level.Info, "设备连接失败"); MessageBox.Show("设备连接失败", "提示"); return("Error"); } RecordLog.GetInstance().WriteLog(Level.Info, "设备连接成功"); EventClass.GetInstance()._dicdeviceAddress.Clear(); return("Success"); } else { MessageBox.Show(string.Format("{0}门锁未授权,请联系系统管理员", s.Key.ToString()), "系统提示"); return("Error"); } } else { MessageBox.Show(string.Format("处理授权文件失败,请联系系统管理员"), "系统提示"); return("Error"); } } } MessageBox.Show("未找到适配的蓝牙设备", "系统提示"); return("Error"); }
/// <summary> /// 2,对外接口:查看已经搜索到的所有BLE设备 /// </summary> /// <returns></returns> public string ViewDevice() { List <outParam> outdata = new List <outParam>(); //string outParam = string.Empty; if (EventClass.GetInstance()._processFlags == 1) { RecordLog.GetInstance().WriteLog(Level.Info, "查找结束"); } if (EventClass.GetInstance()._deviceAddress.Count <= 0) { return(""); } RecordDeviceInfo.GetInstance().DeletDeviceInfoFile(); string lastmac = string.Empty; string lastTemp = string.Empty; int status = 0; int ret = OpenAppDo.GetInstance().judelkconnect(out status); if (ret == 0) { if (status != 0) { RecordLog.GetInstance().WriteLog(Level.Info, "发现已经存在蓝牙信息"); List <string> outStrList = EventClass.GetInstance().knowsLock; foreach (string splitparam in EventClass.GetInstance()._deviceAddress) { foreach (string lockInfo in outStrList) { if (lockInfo.Split(',')[1].ToString().Equals(splitparam.Split('|')[1].ToString())) { //mac相同才会显示 outParam temp = new outParam() { Name = lockInfo.Split(',')[0].ToString(), Mac = lockInfo.Split(',')[1].ToString(), SetTime = lockInfo.Split(',')[2].ToString(), }; outdata.Add(temp); if (lastmac == lockInfo.Split(',')[1].ToString()) { continue; } lastmac = temp.Mac; //判断上次地址和当前地址是否一致 EventClass.GetInstance()._dicdeviceAddress.Add(splitparam.Split('|')[0].ToString(), splitparam.Split('|')[1].ToString()); RecordDeviceInfo.GetInstance().WriteLocalInfo(splitparam.Split('|')[0].ToString(), splitparam.Split('|')[1].ToString(), FingerPrint.Value().ToString()); } } } EventClass.GetInstance().knowsLock.Clear(); } else { foreach (string splitparam in EventClass.GetInstance()._deviceAddress) { //记录本次地址 start string msg = string.Empty; if (lastTemp != splitparam.Split('|')[1].ToString()) { RedvelopRecord.GetInstance().RegeditDoorSetting(out msg, splitparam.Split('|')[1].ToString()); lastTemp = splitparam.Split('|')[1].ToString(); } //记录本次地址 end EventClass.GetInstance()._dicdeviceAddress.Add(splitparam.Split('|')[0].ToString(), splitparam.Split('|')[1].ToString()); RecordDeviceInfo.GetInstance().WriteLocalInfo(splitparam.Split('|')[0].ToString(), splitparam.Split('|')[1].ToString(), FingerPrint.Value().ToString()); outParam temp = new outParam() { Name = "", Mac = splitparam.Split('|')[1].ToString(), SetTime = "", }; outdata.Add(temp); } } } //[{"Name":"前门","Mac":"0CAE7DAB5F72","SetTime":"2018-10-16 03:00:45"}] string json = JsonConvert.SerializeObject(outdata); return(json); }