ibeacon ios教學


ibeacon 是一個藍芽的發射器, ios或是android 可以透過搜尋ibeacon的訊號強度來判別手機是否進入ibeacon的範圍內,以達到

1. 手機是否進入某個範圍 (將ibeacon 放置特定區域)
2. 點名附近所有ibeacon ( 將超小型的ibeacon放置於需要點名的物品上 例如鑰匙 小孩 錢包
)

ibeacon 使用藍芽的ble技術,所以非常省電通常一顆鈕扣電池可以使用3個月不關機,如果使用大一點的電池可以撐上一兩年不需要更換電池.


一開發ibeacon之前 我們需要一台ibeacon發射器, 不花錢方案或是網路買一個

1.用iphone 或是ipad模擬
 
     例如這個
https://itunes.apple.com/us/app/beacon-simulator/id1098267039?mt=8

2.用android 模擬

例如這個
https://play.google.com/store/apps/details?id=net.alea.beaconsimulator&hl=zh_TW

3.用樹莓派模擬

     如這個網站介紹
http://cheng-min-i-taiwan.blogspot.tw/2015/03/raspberry-pi-40ibeacon.html
   指令如下
sudo hciconfig hci0 up
sudo hciconfig hci0 noscan
sudo hciconfig hci0 leadv

sudo hcitool -i hci0 cmd 0x08 0x0008 1E 02 01 1A 1A FF 4C 00 02 15 E2 C5 6D B5 DF FB 48 D2 B0 60 D0 F5 A7 10 96 E0 00 00 00 00 C8 00
關閉如下

sudo hciconfig hci0 down
二 ios 開發

在ios11開發 ibeacon 需要在info.plist中註冊三件事情

NSLocationAlwaysAndWhenInUseUsageDescription
NSLocationAlwaysUsageDescription
NSLocationWhenInUseUsageDescription

ios11 改變了需要註冊這三個才能開發,

CODE
using System.Diagnostics;

using CoreLocation;using Foundation;using UIKit;using UserNotifications;

namespace testBeacon{    // The UIApplicationDelegate for the application. This class is responsible for launching the    // User Interface of the application, as well as listening (and optionally responding) to application events from iOS.    [Register("AppDelegate")]    public class AppDelegate : UIApplicationDelegate    {        // class-level declarations        CLLocationManager mLocationManager;        CLBeaconRegion mRegion;        public override UIWindow Window        {            get;            set;        }
        public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)        {
            UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert, (approved, err) => {                // Handle approval            });
            UIApplication.SharedApplication.RegisterForRemoteNotifications();
            mLocationManager = new CLLocationManager();            mLocationManager.AuthorizationChanged += MLocationManager_AuthorizationChanged;            mLocationManager.RequestAlwaysAuthorization();                      mLocationManager.RegionEntered += MLocationManager_RegionEntered;            mLocationManager.RegionLeft += MLocationManager_RegionLeft;            mLocationManager.DidRangeBeacons += MLocationManager_DidRangeBeacons;
            return true;        }
        public override void OnResignActivation(UIApplication application)        {            // Invoked when the application is about to move from active to inactive state.            // This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message)             // or when the user quits the application and it begins the transition to the background state.            // Games should use this method to pause the game.        }
        public override void DidEnterBackground(UIApplication application)        {            // Use this method to release shared resources, save user data, invalidate timers and store the application state.            // If your application supports background exection this method is called instead of WillTerminate when the user quits.        }
        public override void WillEnterForeground(UIApplication application)        {            // Called as part of the transiton from background to active state.            // Here you can undo many of the changes made on entering the background.        }
        public override void OnActivated(UIApplication application)        {            // Restart any tasks that were paused (or not yet started) while the application was inactive.             // If the application was previously in the background, optionally refresh the user interface.        }
        public override void WillTerminate(UIApplication application)        {            // Called when the application is about to terminate. Save data, if needed. See also DidEnterBackground.        }



        void MLocationManager_AuthorizationChanged(object sender, CLAuthorizationChangedEventArgs e)        {            Debug.WriteLine("AuthorizationChanged" + e.Status.ToString());            if (e.Status == CLAuthorizationStatus.AuthorizedAlways || e.Status == CLAuthorizationStatus.AuthorizedWhenInUse)            {                mRegion = new CLBeaconRegion(new NSUuid("E2C56DB5-DFFB-48D2-B060-D0F5A71096E0"),"iBeacon");                //mRegion = new CLBeaconRegion(new NSUuid("1C43E644-E0EA-D00E-3193-971613064EA4"), "AkingRaspberry");                mRegion.NotifyEntryStateOnDisplay = true;                mRegion.NotifyOnExit = true;                mRegion.NotifyOnEntry = true;                mLocationManager.StartRangingBeacons(mRegion);                mLocationManager.StartMonitoring(mRegion);            }        }
        void MLocationManager_RegionEntered(object sender, CLRegionEventArgs e)        {            ShowMessage("阿鏘通知""我亂入了","跟你說我真的進來了");
        }

        private void ShowMessage(string Tilte , string SubTitle,string Body)        {            var content = new UNMutableNotificationContent();            content.Title = Tilte;            content.Subtitle = SubTitle;            content.Body = Body;            content.Badge = 1;
            var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(5false);            var requestID = "sampleRequest";            var request = UNNotificationRequest.FromIdentifier(requestID, content, trigger);
            UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) => {                if (err != null)                {                    // Do something with error...                }            });        }
        void MLocationManager_RegionLeft(object sender, CLRegionEventArgs e)        {            ShowMessage("阿鏘通知""我離開了","我真的離開了");        }
        void MLocationManager_DidRangeBeacons(object sender, CLRegionBeaconsRangedEventArgs e)        {            foreachvar b in e.Beacons)                Debug.WriteLine("Catch{0}{1}" , b.ProximityUuid.AsString() , b.Rssi);        }    }}




留言

熱門文章