/// <summary> /// 获取蓝牙命令 /// </summary> /// <param name="cmd">命令枚举</param> /// <returns>串口命令字符串</returns> public string[] GetBleCmd(enumBleCmd cmd) { List <string> listBleCmd = new List <string>(); setSerialBleCmd(cmd); switch (cmd) { case enumBleCmd.Init: setSerialBleStatus(enumBleStatus.Stop); listBleCmd.Add("AT"); break; case enumBleCmd.Find: if (SerialBleStatus == enumBleStatus.Ready) { setSerialBleStatus(enumBleStatus.Find); listBleCmd.Add("AT+DISC?"); } else { EventBleLog("搜索失败请重试"); return(GetBleCmd(enumBleCmd.Init)); } break; case enumBleCmd.Link: if (SerialBleStatus == enumBleStatus.Ready) { setSerialBleStatus(enumBleStatus.Link); if (ComboBle.Items.Count > 0 && ComboBle.SelectedIndex >= 0) { setSerialBleStatus(enumBleStatus.Link); //listBleCmd.Add("AT+CONN" + ComboBle.SelectedIndex); listBleCmd.Add("AT+CON" + DicListBle[ComboBle.SelectedItem.ToString()]); _bakBleLinkData = ComboBle.SelectedItem.ToString(); } } else { Console.WriteLine("请先搜索蓝牙设备。"); EventBleLog("请先搜索蓝牙设备"); } break; case enumBleCmd.Reset: setSerialBleStatus(enumBleStatus.Stop); listBleCmd.Add("AT"); listBleCmd.Add("AT+RESET"); break; case enumBleCmd.ReLink: if (ComboBle.Items.Count > 0 && DicListBle.ContainsKey(_bakBleLinkData)) { ComboBle.SelectedIndex = ComboBle.Items.IndexOf(_bakBleLinkData); return(GetBleCmd(enumBleCmd.Link)); } break; default: break; } return(listBleCmd.ToArray()); }
/// <summary> /// 接收蓝牙串口数据 /// </summary> /// <param name="strBleData"></param> public void ReceiveSerialBle(string strBleData) { //过滤 strBleData = Regex.Replace(strBleData, @"\s", ""); if (strBleData == "OK") { setSerialBleStatus(enumBleStatus.Ready); WriteBleCmd(enumBleCmd.Find); } else if (strBleData.IndexOf("OK+DISCS") >= 0) { //开启搜索蓝牙 setSerialBleStatus(enumBleStatus.Find); DicListBle.Clear(); DicListBleRssi.Clear(); ComboBle.Items.Clear(); ComboBle.Enabled = false; EventBleLog("蓝牙搜索中"); Console.WriteLine("搜索中。。。"); //测试 //string str = "OK+DIS0:04AC448009BDOK+NAME:Tomozaki02\r\nOK + DIS0:04AC448001F6OK + NAME:Tomozaki01"; //Console.WriteLine("测试:" + str); //ReceiveSerialBle(str); } else if (strBleData.IndexOf("OK+DISCE") >= 0) { //搜索蓝牙结束 if (DicListBle.Count > 0) { setSerialBleStatus(enumBleStatus.Ready); ComboBle.Enabled = true; ComboBle.Items.AddRange(LinqDictionary(DicListBleRssi, DicListBle)); ComboBle.SelectedIndex = 0; EventBleLog("蓝牙搜索完成"); if (IsAutoReLink && (_bakBleLinkData != "")) { if (DicListBle.ContainsKey(_bakBleLinkData)) { WriteBleCmd(enumBleCmd.ReLink); } else { WriteBleCmd(enumBleCmd.Find); } } else if (IsAutoLink) { WriteBleCmd(enumBleCmd.Link); } } else { setSerialBleStatus(enumBleStatus.Stop); ComboBle.Enabled = false; EventBleLog("未搜索到蓝牙"); Console.WriteLine("未搜索到蓝牙"); } } else if (strBleData.IndexOf("OK+DIS0") >= 0) { string[] arrBleData = Regex.Split(strBleData, @"OK\+DIS0\:", RegexOptions.IgnoreCase); for (int i = 0; i < arrBleData.Length; i++) { Int32 cnt = arrBleData[i].Length; string strMac = ""; Int32 numRSSI = 0; if (cnt >= 12) { if (arrBleData[i].IndexOf("OK+DIS") == 0) { strMac = arrBleData[i].Substring(8, 12); } else { strMac = arrBleData[i].Substring(0, 12); } cnt = arrBleData[i].IndexOf("OK+RSSI:"); if (cnt > 0) { try { numRSSI = Convert.ToInt32(arrBleData[i].Substring(cnt + 8, 4)); DicListBleRssi.Add(strMac, Convert.ToInt32(numRSSI)); } catch (Exception err) { Console.WriteLine("RSSI:" + err.Message); } } cnt = arrBleData[i].IndexOf("OK+NAME:"); if (cnt >= 0) { string strName = arrBleData[i].Substring(cnt + 8); addListBle(strMac, strName, numRSSI); Console.WriteLine("搜索蓝牙:" + strName + ":" + strMac + ":" + numRSSI.ToString()); } } } } else if (strBleData == "OK+CONN" + ComboBle.SelectedIndex || strBleData == "OK+CONNA") { //正在连接蓝牙 setSerialBleStatus(enumBleStatus.Link); EventBleLog("正在连接蓝牙"); Console.WriteLine("正在连接蓝牙:" + ComboBle.SelectedText); } else if (strBleData == "OK+CONNF") { //连接失败 setSerialBleStatus(enumBleStatus.Stop); ComboBle.Enabled = false; EventBleLog("连接蓝牙失败"); Console.WriteLine("连接蓝牙失败" + ComboBle.SelectedText); } else if (strBleData == "OK+CONN") { //连接成功 setSerialBleStatus(enumBleStatus.Run); ComboBle.Enabled = false; EventBleLog("连接蓝牙成功"); Console.WriteLine("连接蓝牙成功" + ComboBle.SelectedText); } else if (strBleData.IndexOf("OK+LOST") >= 0) { setSerialBleStatus(enumBleStatus.Stop); WriteBleCmd(enumBleCmd.Init); EventBleLog("蓝牙连接断开"); } }