コード例 #1
0
ファイル: SearchDevice.cs プロジェクト: UMI64/-LED-
        public void ScanDeviceThread()
        {
            int BEGANNum = 0; //开始扫描的起始
            int ENDNum   = 0; //开始扫描的结束
            int mTheardNum;

            lock (this)
            {
                mTheardNum = TheardNum++;//当前是第几个线程
            }
            BEGANNum = mTheardNum * 256 / ScanDeviceTheardNum;
            ENDNum   = BEGANNum + 256 / ScanDeviceTheardNum;
            ConnectivityManager cm            = (ConnectivityManager)context.GetSystemService(Context.ConnectivityService);
            NetworkInfo         activeNetwork = cm.ActiveNetworkInfo;
            WifiManager         wm            = (WifiManager)context.GetSystemService(Context.WifiService);

            WifiInfo connectionInfo = wm.ConnectionInfo;
            int      ipAddress      = connectionInfo.IpAddress;
            string   ipString       = Formatter.FormatIpAddress(ipAddress);

            string prefix = ipString.Substring(0, ipString.LastIndexOf(".") + 1);

            for (int i = BEGANNum; i < ENDNum; i++)//执行扫描
            {
                string      testIp    = prefix + i.ToString();
                InetAddress address   = InetAddress.GetByName(testIp);
                bool        reachable = address.IsReachable(800);
                string      hostName  = address.CanonicalHostName;
                if (reachable)//如果Ping通 吧IP加入
                {
                    lock (this)
                    {
                        DeviceIP.Add(testIp, "");
                    }
                }
            }
            TheardComplete++;                          //线程完成数加1
            if (TheardComplete >= ScanDeviceTheardNum) //如果这个是最后一个完成的线程
            {
                Log.Info("TCP", "StartConnectDevice : " + DeviceIP.Count.ToString());
                TheardComplete = 0;
                TheardNum      = 0;
                ConnectDevice();//开始尝试连接
            }
        }