Beispiel #1
0
        private void Dispose(bool disposing)
        {
            // Check to see if Dispose has already been called.
            if (!disposed)
            {
                // If disposing equals true, dispose all managed and unmanaged resources.
                if (disposing)
                {
                    if (WlanInterface != null)
                    {
                        // If the current access point is different from the original, disconnect from the current one and reconnect
                        // to the original one.
                        if (!CurrentAccessPoint.Equals(OriginalAccessPoint))
                        {
                            // Disconnect from the wifi access point we are currently connected to.
                            DisconnectWifi();

                            // If there was an existing wifi connection when the wifi class was constructed and that access point is different
                            // from the current access point, rejoin the original access point.
                            if (!NotConnected.Equals(OriginalAccessPoint.SSID))
                            {
                                WifiAccessPoint currentAccessPoint = CurrentAccessPoint;
                                if (!currentAccessPoint.SSID.Equals(OriginalAccessPoint.SSID))
                                {
                                    JoinWifi(OriginalAccessPoint.SSID, OriginalAccessPoint.BssType);
                                }
                            }
                        }
                    }
                }

                // Note disposing has been done.
                disposed = true;
            }
        }
        public Location GetLocation()
        {
            WlanClient.WlanClient client = new WlanClient.WlanClient();
            List<WifiAccessPoint> wifiAccessPoints = new List<WifiAccessPoint>();
            foreach (WlanClient.WlanClient.WlanInterface wlanInterface in client.Interfaces)
            {
                Wlan.WlanBssEntry[] bssList = wlanInterface.GetNetworkBssList();
                foreach (Wlan.WlanBssEntry bss in bssList)
                {
                    WifiAccessPoint accessPoint = new WifiAccessPoint();
                    accessPoint.macAddress = ByteArrayToMacAddress(bss.dot11Bssid);
                    wifiAccessPoints.Add(accessPoint);
                }
            }
            LocationRequest locationRequest = new LocationRequest();
            locationRequest.wifiAccessPoints = wifiAccessPoints.ToArray();
            string requestJson = JsonConvert.SerializeObject(locationRequest);
            byte[] requestBytes = GetBytes(requestJson);

            string url = "https://www.googleapis.com/geolocation/v1/geolocate";

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.Method = "POST";
            webRequest.ContentType = "application/json";
            webRequest.Accept = "*/*";
            webRequest.UserAgent = Program.APP_USER_AGENT;
            webRequest.ContentLength = requestBytes.Length;

            Stream requestStream = webRequest.GetRequestStream();
            requestStream.Write(requestBytes, 0, requestBytes.Length);
            requestStream.Close();

            WebResponse response = webRequest.GetResponse();

            Stream responseStream = response.GetResponseStream();
            StreamReader responseStreamReader = new StreamReader(responseStream);

            string responseJson = responseStreamReader.ReadToEnd();
            responseStreamReader.Close();
            responseStream.Close();

            return JsonConvert.DeserializeObject<LocationResponse>(responseJson).location;
        }