2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 namespace Tizen.Location.Geofence
22 /// Geofence defines a virtual perimeter for a real-world geographic area.
23 /// If you create a geofence, you can trigger some activities when a device enters (or exits) the geofences defined by you.
24 /// You can create a geofence with the information of the Geopoint, Wi-Fi, or BT.
26 /// <item>Geopoint: Geofence is specified by the coordinates (Latitude and Longitude) and radius.</item>
27 /// <item>WIFI: Geofence is specified by the BSSID of the Wi-Fi access point.</item>
28 /// <item>BT: Geofence is specified by the Bluetooth address.</item>
30 /// The Basic service set identifier (BSSID) is the MAC address of the wireless access point (WAP) generated by combining the 24-bit Organization Unique Identifier (the manufacturer's identity)
31 /// and the manufacturer's assigned 24-bit identifier for the radio chipset in the WAP.
33 /// <since_tizen> 3 </since_tizen>
34 public class Fence : IDisposable
36 private bool _disposed = false;
38 internal IntPtr Handle
44 internal Fence(IntPtr handle)
55 /// Gets the type of geofence.
57 /// <since_tizen> 3 </since_tizen>
63 GeofenceError ret = (GeofenceError)Interop.Geofence.FenceType(Handle, out val);
64 if (ret != GeofenceError.None)
66 Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get GeofenceType");
74 /// Gets the ID of the place.
76 /// <since_tizen> 3 </since_tizen>
82 GeofenceError ret = (GeofenceError)Interop.Geofence.FencePlaceID(Handle, out result);
83 if (ret != GeofenceError.None)
85 Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get PlaceId");
93 /// Gets the longitude of geofence.
95 /// <since_tizen> 3 </since_tizen>
96 public double Longitude
101 GeofenceError ret = (GeofenceError)Interop.Geofence.FenceLongitude(Handle, out result);
102 if (ret != GeofenceError.None)
104 Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get Longitude");
113 /// Gets the latitude of geofence.
115 /// <since_tizen> 3 </since_tizen>
116 public double Latitude
121 GeofenceError ret = (GeofenceError)Interop.Geofence.FenceLatitude(Handle, out result);
122 if (ret != GeofenceError.None)
124 Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get Latitude");
132 /// Gets the radius of geofence.
134 /// <since_tizen> 3 </since_tizen>
140 GeofenceError ret = (GeofenceError)Interop.Geofence.FenceRadius(Handle, out result);
141 if (ret != GeofenceError.None)
143 Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get Radius");
151 /// Gets the address of geofence.
153 /// <since_tizen> 3 </since_tizen>
154 public string Address
159 GeofenceError ret = (GeofenceError)Interop.Geofence.FenceAddress(Handle, out result);
160 if (ret != GeofenceError.None)
162 Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get Adress");
170 /// Gets the BSSID of geofence.
172 /// <since_tizen> 3 </since_tizen>
178 GeofenceError ret = (GeofenceError)Interop.Geofence.FenceBSSID(Handle, out result);
179 if (ret != GeofenceError.None)
181 Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get Bssid");
189 /// Gets the SSID of geofence.
191 /// <since_tizen> 3 </since_tizen>
197 GeofenceError ret = (GeofenceError)Interop.Geofence.FenceSSID(Handle, out result);
198 if (ret != GeofenceError.None)
200 Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get Ssid");
208 /// Creates a geopoint type of the new geofence.
210 /// <since_tizen> 3 </since_tizen>
211 /// <param name="placeId">The current place ID.</param>
212 /// <param name="latitude">Specifies the value of latitude of the geofence [-90.0 ~ 90.0] (degrees).</param>
213 /// <param name="longitude">Specifies the value of longitude of the geofence [-180.0 ~ 180.0] (degrees).</param>
214 /// <param name="radius">Specifies the value of radius of the geofence [100 ~ 500](meter).</param>
215 /// <param name="address">Specifies the value of the address.</param>
216 /// <returns>The newly created geofence instance.</returns>
217 /// <exception cref="ArgumentException">In case of an invalid parameter.</exception>
218 /// <exception cref="InvalidOperationException">In case of any system error.</exception>
219 /// <exception cref="NotSupportedException">In case the geofence is not supported.</exception>
220 public static Fence CreateGPSFence(int placeId, int latitude, int longitude, int radius, string address)
222 IntPtr handle = IntPtr.Zero;
223 GeofenceError ret = (GeofenceError)Interop.Geofence.CreateGPSFence(placeId, latitude, longitude, radius,address, out handle);
224 if (ret != GeofenceError.None)
226 throw GeofenceErrorFactory.CreateException(ret, "Failed to create Geofence from GPS Data for " + placeId);
229 return new Fence(handle);
233 /// Creates a Wi-Fi type of the new geofence.
235 /// <since_tizen> 3 </since_tizen>
236 /// <param name="placeId">The current place ID.</param>
237 /// <param name="bssid">Specifies the value of BSSID of the Wi-Fi MAC address.</param>
238 /// <param name="ssid"> Specifies the value of SSID of the Wi-Fi device.</param>
239 /// <returns>The newly created geofence instance.</returns>
240 /// <exception cref="ArgumentException">In case of an invalid parameter.</exception>
241 /// <exception cref="InvalidOperationException">In case of any system error.</exception>
242 /// <exception cref="NotSupportedException">In case the geofence is not supported.</exception>
243 public static Fence CreateWifiFence(int placeId, string bssid, string ssid)
245 IntPtr handle = IntPtr.Zero;
246 GeofenceError ret = (GeofenceError)Interop.Geofence.CreateWiFiFence(placeId, bssid, ssid, out handle);
247 if (ret != GeofenceError.None)
249 throw GeofenceErrorFactory.CreateException(ret, "Failed to create Geofence from Wifi Data for " + placeId);
252 return new Fence(handle);
256 /// Creates a Bluetooth type of the new geofence.
258 /// <since_tizen> 3 </since_tizen>
259 /// <param name="placeId">The current place ID.</param>
260 /// <param name="bssid">Specifies the value of BSSID of BT MAC address.</param>
261 /// <param name="ssid"> Specifies the value of SSID of BT Device.</param>
262 /// <returns>The newly created geofence instance.</returns>
263 /// <exception cref="ArgumentException">In case of an invalid parameter.</exception>
264 /// <exception cref="InvalidOperationException">In case of any system error.</exception>
265 /// <exception cref="NotSupportedException">In case the geofence is not supported.</exception>
266 public static Fence CreateBTFence(int placeId, string bssid, string ssid)
268 IntPtr handle = IntPtr.Zero;
269 GeofenceError ret = (GeofenceError)Interop.Geofence.CreateBTFence(placeId, bssid, ssid, out handle);
270 if (ret != GeofenceError.None)
272 throw GeofenceErrorFactory.CreateException(ret, "Failed to create Geofence from Bluetooth Data for " + placeId);
275 return new Fence(handle);
279 /// The overloaded Dispose API for destroying the fence handle.
281 /// <since_tizen> 3 </since_tizen>
282 public void Dispose()
285 GC.SuppressFinalize(this);
288 private void Dispose(bool disposing)
293 if (Handle != IntPtr.Zero)
295 Interop.Geofence.Destroy(Handle);
296 Handle = IntPtr.Zero;