示例#1
0
        public void SendMessage(CBlueToothManage btm, byte[] msg, int maxSendRetryCount = 5)
        {
            var cnt = 0;

            do
            {
                if (cnt >= maxSendRetryCount)
                {
                    throw new Exception($"超过最大尝试发送次数,发送失败。");
                }
                if (!btm.SendMessage(msg))
                {
                    Console.WriteLine($"向蓝牙设备发送指令失败。");
                    cnt++;
                }
                else
                {
                    break;
                }
            }while (true);
        }
示例#2
0
        public CBlueToothManage ConnectToBluetooth(string blueName, string pwd, int maxConnectRetryCount = 5)
        {
            var btm = new CBlueToothManage();
            List <CBluetooth> lst = null;
            int cnt = 0;

            do
            {
                if (cnt >= maxConnectRetryCount)
                {
                    throw new Exception($"超过最大连接次数,无法连接到蓝牙设备。");
                }
                Console.WriteLine("Searching...");
                lst = btm.Search();
                if (lst == null || lst.Count() == 0)
                {
                    Console.WriteLine($"指定设备不存在。");
                    cnt++;
                    continue;
                }
                Console.WriteLine($"Blue Name         BlueAddress");
                Console.WriteLine($"----------------------------------------");
                foreach (var item in lst)
                {
                    Console.WriteLine($"{item.blueName}, {item.blueAddress}");
                }
                if (!btm.ConnectTo(lst, blueName, pwd))
                {
                    Console.WriteLine($"连接失败。指定设备不存在,或密码错误。");
                    cnt++;
                    continue;
                }

                break;
            } while (true);



            return(btm);
        }