{"id":3344,"date":"2021-07-01T17:06:52","date_gmt":"2021-07-01T09:06:52","guid":{"rendered":"http:\/\/123.57.164.21\/?p=3344"},"modified":"2021-07-01T17:06:52","modified_gmt":"2021-07-01T09:06:52","slug":"swift-ibeacon-%e5%bc%80%e5%8f%91","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=3344","title":{"rendered":"Swift iBeacon \u5f00\u53d1"},"content":{"rendered":"\n<h5 class=\"wp-block-heading\" id=\"ibeacon\u662f\u4ec0\u4e48\">iBeacon\u662f\u4ec0\u4e48?<\/h5>\n\n\n\n<p>\u7ef4\u57fa\u767e\u79d1\uff1aiBeacon\u662f\u82f9\u679c\u516c\u53f8\u63d0\u51fa\u7684\u201d\u4e00\u79cd\u53ef\u4ee5\u8ba9\u9644\u8fd1\u624b\u6301\u7535\u5b50\u8bbe\u5907\u68c0\u6d4b\u5230\u7684\u4e00\u79cd\u65b0\u7684\u4f4e\u529f\u8017\u3001\u4f4e\u6210\u672c\u4fe1\u53f7\u4f20\u9001\u5668\u201d\u7684\u4e00\u5957\u53ef\u7528\u4e8e\u5ba4\u5185\u5b9a\u4f4d\u7cfb\u7edf\u7684\u534f\u8bae\u3002\u8fd9\u79cd\u6280\u672f\u53ef\u4ee5\u4f7f\u4e00\u4e2a\u667a\u80fd\u624b\u673a\u6216\u5176\u4ed6\u88c5\u7f6e\u5728\u4e00\u4e2aiBeacon\u57fa\u7ad9\u7684\u611f\u5e94\u8303\u56f4\u5185\u6267\u884c\u76f8\u5e94\u7684\u547d\u4ee4\u3002<\/p>\n\n\n\n<p>\u767e\u5ea6\u767e\u79d1\uff1aiBeacon\u662f\u82f9\u679c\u516c\u53f82013\u5e749\u6708\u53d1\u5e03\u7684\u79fb\u52a8\u8bbe\u5907OS(iOS7)\u4e0a\u914d\u5907\u7684\u65b0\u529f\u80fd\u3002\u5176\u5de5\u4f5c\u65b9\u5f0f\u662f\uff0c\u914d\u5907\u6709\u4f4e\u529f\u8017\u84dd\u7259(BLE)\u901a\u4fe1\u529f\u80fd\u7684\u8bbe\u5907\u4f7f\u7528BLE\u6280\u672f\u5411\u5468\u56f4\u53d1\u9001\u81ea\u5df1\u7279\u6709\u7684ID\uff0c\u63a5\u6536\u5230\u8be5ID\u7684\u5e94\u7528\u8f6f\u4ef6\u4f1a\u6839\u636e\u8be5ID\u91c7\u53d6\u4e00\u4e9b\u884c\u52a8\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u4f7f\u7528\u573a\u666f:<\/h5>\n\n\n\n<ul><li>\u63a8\u9001\u611f\u77e5\u573a\u666f\u7684\u4fe1\u606f<\/li><\/ul>\n\n\n\n<p>\u5f53\u7528\u6237\u79fb\u52a8\u5230\u4ed6\u4eec\u611f\u5174\u8da3\u7684\u533a\u57df\u65f6\uff0ciBeacons \u53ef\u4ee5\u7528\u6765\u7ed9\u4ed6\u4eec\u63a8\u9001\u4fe1\u606f\u3002\u535a\u7269\u9986\u5c31\u662f\u4e00\u4e2a\u5178\u578b\u7684\u4f8b\u5b50\uff0c\u8bbe\u60f3\u5728\u6bcf\u4e00\u4e2a\u5c55\u54c1\u7684\u4f4d\u7f6e\u653e\u7f6e\u4e00\u4e2a iBeacons \u8bbe\u5907\uff0c\u5f53\u7528\u6237\u8d70\u8fd1\u5c55\u54c1\u7684\u65f6\u5019\uff0c\u624b\u673a\u5e94\u7528\u81ea\u52a8\u5c55\u793a\u770b\u5230\u5c55\u54c1\u7684\u66f4\u591a\u4fe1\u606f\u4f1a\u6709\u591a\u68d2! \u8fd9\u5c31\u9700\u8981\u624b\u673a\u5e94\u7528\u4fa6\u542c\u53d1\u5c04\u5668\u6765\u4e86\u89e3\u6709\u54ea\u4e9b\u53d1\u5c04\u5668\u63a5\u8fd1\u54ea\u4e9b\u5c55\u54c1\uff0c\u901a\u8fc7\u8fd9\u6837\u7684\u5339\u914d\uff0c\u53d1\u5c04\u5668\u5c06\u5b9a\u4f4d\u7528\u6237\u5728\u535a\u7269\u9986\u4e2d\u7684\u4f4d\u7f6e\u5e76\u8ba9\u5e94\u7528\u505a\u51fa\u5408\u7406\u7684\u54cd\u5e94\u3002<\/p>\n\n\n\n<ul><li>\u5b9a\u4f4d\u8ffd\u8e2a<\/li><\/ul>\n\n\n\n<p>\u4f5c\u4e3a\u63a8\u9001\u611f\u77e5\u573a\u666f\u7684\u4fe1\u606f\u6982\u5ff5\u7684\u6269\u5c55\uff0c\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 iBeacons \u4f5c\u4e3a\u4e00\u79cd\u8ffd\u8e2a\u7528\u6237\u7684\u65b9\u5f0f\u3002\u8bbe\u60f3\u5728\u4e00\u4e2a\u535a\u7269\u9986\u6216\u8005\u6742\u8d27\u5e97\u7684\u5efa\u7b51\u4e2d\u904d\u5e03 iBeacons \uff0c\u968f\u7740\u7528\u6237\u7684\u79fb\u52a8\uff0c\u7531\u4ed6\u4eec\u901a\u8fc7\u53d1\u5c04\u5668\u7684\u987a\u5e8f\uff0c\u4f60\u53ef\u4ee5\u4fa6\u6d4b\u51fa\u4ed6\u4eec\u7684\u79fb\u52a8\u8f68\u8ff9\u3002\u8fd9\u5141\u8bb8\u4f60\u8ffd\u8e2a\u7528\u6237\u7684\u884c\u8e2a\uff0c\u5e76\u4e14\u603b\u7ed3\u51fa\u6700\u666e\u904d\u7684\u884c\u8fdb\u8def\u7ebf\u548c\u884c\u8fdb\u6a21\u5f0f\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u4ece\u5f00\u53d1\u8005\u89d2\u5ea6\u7684\u601d\u8003:<\/h5>\n\n\n\n<p>iBeacon\u5411\u56db\u9762\u516b\u65b9\u4e0d\u505c\u5730\u5e7f\u64ad\u4fe1\u53f7\uff0c\u5c31\u50cf\u662f\u5f80\u5e73\u9759\u7684\u6c34\u9762\u4e0a\u6254\u4e86\u4e00\u5757\u77f3\u5b50\uff0c\u6cdb\u8d77\u5c42\u5c42\u6d9f\u6f2a(\u4fd7\u79f0\u6c34\u6ce2)\uff0c\u6ce2\u5cf0\u76f8\u5f53\u4e8eiBeacon\u7684RSSI(\u63a5\u53d7\u4fe1\u53f7\u5f3a\u5ea6\u6307\u793a)\uff0c\u8d8a\u9760\u8fd1\u4e2d\u5fc3\u70b9\u7684\u5730\u65b9\u6ce2\u5cf0\u8d8a\u9ad8(RSSI\u8d8a\u5927)\uff0c\u8fd9\u4e2a\u6ce2\u5cf0\u7684\u5927\u5c0f(RSSI\u7684\u503c)\u53d7\u5230\u6254\u77f3\u5b50\u65f6\u7528\u529b\u5927\u5c0f(\u53d1\u5c04\u529f\u7387)\u548c\u6c34\u8d28(\u5468\u56f4\u73af\u5883\u56e0\u5b50)\u7684\u5f71\u54cd\uff0c\u79bb\u4e2d\u5fc3\u70b9\u8d8a\u8fdc\u6c34\u6ce2\u8d8a\u8d8b\u5411\u4e8e\u5e73\u9759\uff0c\u8d85\u8fc7\u4e86\u4e00\u5b9a\u503c\uff0c\u6c34\u6ce2\u4f1a\u6d88\u5931\u4e8e\u65e0\u5f62\uff0c\u4e5f\u5c31\u662f\u8bf4iBeacon\u5411\u5916\u5e7f\u64ad\u7684\u8ddd\u79bb\u662f\u6709\u8303\u56f4\u7684\uff0c\u8d85\u8fc7\u4e86\u8fd9\u4e2a\u8303\u56f4\uff0c\u5c06\u63a5\u53d7\u4e0d\u5230iBeacon\u7684\u4fe1\u53f7\u3002<\/p>\n\n\n\n<p>\u603b\u4f53\u6765\u770b,iBeacon\u4e2d\u6709\u4e24\u4e2a\u89d2\u8272:\u53d1\u5c04\u8005 :\u5404\u4e2a\u786c\u4ef6\u8bbe\u5907;\u63a5\u53d7\u8005:\u667a\u80fd\u7ec8\u7aef(\u624b\u673a),\u53d1\u5c04\u8005\u901a\u8fc7BLE \u7684\u5e7f\u544a\u901a\u4fe1\u901a\u9053,\u4ee5\u4e00\u5b9a\u65f6\u95f4\u95f4\u9694\u5411\u5916\u5e7f\u64ad\u6570\u636e\u5305(\u4e00\u822c\u662f\u6bcf\u79d2\u4e24\u4e09\u6b21),\u63a5\u6536\u8005\u53ef\u4ee5\u901a\u8fc7\u7ec8\u7aef\u63d0\u4f9b\u7684\u529f\u80fd\u6765\u63a5\u6536,\u8fbe\u5230\u4fe1\u606f\u7684\u4ea4\u4e92.<br>\u6bcf\u4e2a\u4fe1\u53f7\u4e2d\u81f3\u5c11\u643a\u5e26\u4e86\u4e09\u4e2a\u4e3b\u8981\u4fe1\u606f:UUID, Major, Minor,\u8fd9\u4e09\u4e2a\u4fe1\u53f7\u7ec4\u6210\u4e86\u4e00\u4e2a iBeacon \u7684\u552f\u4e00\u6807\u8bc6\u7b26.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u5982\u4f55\u63a5\u6536iBeacon?<\/h5>\n\n\n\n<p>\u4f5c\u4e3aiOS\u5f00\u53d1\u8005,\u8fd9\u91cc\u6709\u4e00\u4e2a\u5148\u5929\u4f18\u52bf,\u6bcf\u4e00\u53f0iPhone\u8bbe\u5907\u90fd\u53ef\u4ee5\u4f5c\u4e3a&#8220;\u53d1\u5c04\u8005,\u6240\u4ee5\u6211\u4eec\u9700\u8981\u51c6\u5907\u4e24\u53f0iPhone\u624b\u673a,\u5176\u4e2d\u4e00\u53f0\u624b\u673a\u53bbAppStore\u4e0b\u8f7dAirBeacon\u5e94\u7528,\u7528\u4e8e\u53d1\u5c04iBeacon\u5e7f\u64ad\u4fe1\u53f7(\u53d1\u5c04\u8005),\u53e6\u5916\u4e00\u53f0\u7528\u4e8e\u63a5\u6536\u8c03\u8bd5<\/p>\n\n\n\n<p>\u5b9e\u9645\u573a\u666f\u80af\u5b9a\u4e0d\u662f\u7528iPhone\u8bbe\u5907\u4f5c\u4e3a\u53d1\u5c04,\u4e00\u822c\u90fd\u6709\u5f88\u591a\u7b2c\u4e09\u65b9\u7684\u786c\u4ef6\u5382\u5546\u505a\u8fd9\u4e2a,\u6bd4\u8f83\u4e3b\u6d41\u7684\u751f\u4ea7\u5546\u5305\u62ec Estimote \u3001 Aruba \u548c Radius Networks .\u5982\u679c\u4f60\u6b63\u5728\u6253\u7b97\u4f7f\u7528 iBeacons \uff0c\u4e0a\u8fb9\u7684\u4efb\u4f55\u4e00\u5bb6\u90fd\u4f1a\u662f\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u4e00\u4e2a\u57fa\u7ad9\u4e3b\u8981\u6709\u4e09\u90e8\u5206\u6807\u8bc6\uff1a<\/h5>\n\n\n\n<ul><li>1. UUID\uff0c\u5f62\u5982\uff1a206A2476-D4DB-42F0-BF73-030236F2C756\u3002\u7528\u6765\u6807\u8bc6\u67d0\u4e00\u4e2a\u516c\u53f8\u3002\u6bd4\u5982\uff0c\u67d0\u4e2a\u623f\u5730\u4ea7\u516c\u53f8\u7684\u5168\u90e8\u7684\u57fa\u7ad9\u90fd\u7528\u540c\u4e00\u4e2aUUID\u3002<\/li><li>2. major\uff0c\u7528\u6765\u6807\u8bc6\u67d0\u4e00\u7c7b\u7684beacon\u3002\u6bd4\u5982\u8fd9\u4e2a\u623f\u5730\u4ea7\u516c\u53f8\u7684\u5317\u4eac\u7684\u623f\u5b50\u90fd\u8bbe\u5b9a\u4e3a1\uff0c\u4e0a\u6d77\u7684\u90fd\u8bbe\u5b9a\u4e3a2\u3002<\/li><li>3. minor\uff0c\u7528\u6765\u6807\u8bc6\u67d0\u4e00\u4e2a\u7279\u5b9a\u7684beacon\u3002\u6bd4\u5982\u67d0\u680b\u697c\u7684\u67d0\u4e2a\u57fa\u7ad9\u3002<\/li><\/ul>\n\n\n\n<p>\u7528\u8fd9\u4e2a\u623f\u5730\u4ea7\u5546\u5f00\u53d1\u7684app\u5c31\u53ef\u4ee5\u83b7\u53d6\u5230UUID\uff0c\u548c\u540e\u9762\u7684major\u503c\u548cminor\u503c\u3002\u5f53app\u8fdb\u5165beacon\u7684\u533a\u57df\uff0c\u63a2\u6d4b\u5230UUID\uff1a\u201d206A2476-D4DB-42F0-BF73-030236F2C756\u201c\uff0cmajor\u4e3a1\uff0cminor\u4e3a20\u3002\u90a3\u4e48\u5c31\u8868\u660e\u8fd9\u4e2a\u7528\u6237\u5728\u8fd9\u4e2a\u623f\u5730\u4ea7\u5546\u7684\u5317\u4eac\u697c\u76d8\u7684\u7f16\u53f720\u7684\u697c\u76d8\u3002\u8fd9\u680b\u697c\u7684\u8bf4\u660e\u6587\u5b57\u3001\u56fe\u7247\u6216\u8005\u89c6\u9891\u7b49\u5c31\u53ef\u4ee5\u5c55\u73b0\u5728\u7528\u6237\u9762\u524d\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">iOS\u4e2d\u76f8\u5173API\u548c\u4f7f\u7528\u65b9\u6cd5,\u5927\u81f4\u4ee3\u7801<\/h5>\n\n\n\n<ul><li>\u9700\u8981\u6253\u5f00GPS\u5b9a\u4f4d\u548c\u84dd\u7259,\u5728Info.plist \u589e\u52a0 Privacy &#8211; Location Always Usage Description<\/li><li>iBeacon \u7684 API \u662f\u5728 CoreLocation, \u4f46iBeacon \u5fc5\u987b\u8981\u6253\u5f00\u84dd\u7259,\u5982\u679c\u9700\u8981\u5224\u65ad\u84dd\u7259,\u9700\u8981\u7528\u5230 CoreBluetooth \u6846\u67b6.<\/li><li>Monitoring\u548c Ranging \u662f\u4e24\u79cd\u76d1\u6d4b\u65b9\u5f0f,\u53ef\u4ee5\u4e00\u8d77\u7528,\u4f46\u662f\u9700\u8981\u533a\u5206\u4e1a\u52a1\u9700\u6c42,\u4e24\u79cd\u4e00\u8d77\u7528\u4f1a\u6709\u5c0f\u5751.<\/li><\/ul>\n\n\n\n<ol><li>CLLocationManager<\/li><\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">locationManager = CLLocationManager.init() \n\/\/ \u9075\u5faa\u4ee3\u7406\nlocationManager?.delegate = self \n\/\/ \u8bf7\u6c42\u7528\u6237\u6388\u6743\u5b9a\u4f4d\nlocationManager?.requestAlwaysAuthorization()\n<\/pre>\n\n\n\n<p>2.CLBeaconRegion \u7684\u521b\u5efa<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/\u552f\u4e00\u6807\u793a,\u5176\u5b9e\u6709\u4e09\u4e2a,\u5305\u62ecmajor,minor\nlet ZWUUID:UUID = UUID(uuidString: \"7E66DA30-0A96-4DB5-A15B-066CE9032E70\")!\n\nbeaconRegin = CLBeaconRegion(proximityUUID: ZWUUID, major: 6, minor: 3, identifier: \"ZWIBeacon\")\n\/\/\u901a\u77e5\u9000\u51fa\u548c\u8fdb\u5165\nbeaconRegin?.notifyOnExit = true\nbeaconRegin?.notifyOnEntry = true\n\n\/\/\u8865\u5145\u8bf4\u660e:\n\/\/\u4ec5\u4f7f\u7528proximityUUID\u6765\u521d\u59cb\u5316\u533a\u57df\uff0cmajor,minor\u503c\u5c06\u4f5c\u4e3a\u901a\u914d\u7b26\u3002\u53ea\u8981\u662f\u533a\u57df\u5185\u7684iBeacon\u7684proximityUUID\u4e0e\u6b64proximityUUID\u76f8\u540c\uff0c\u4e0d\u7ba1major, minor\u662f\u4ec0\u4e48\u503c\uff0c\u90fd\u80fd\u88ab\u68c0\u6d4b\u5230\u3002\n        CLBeaconRegion(proximityUUID:identifier:)\n\/\/\u4f7f\u7528proximityUUID\u548cmajor\u6765\u521d\u59cb\u5316\u533a\u57df\uff0cminor\u503c\u5c06\u4f5c\u4e3a\u901a\u914d\u7b26\u3002\u533a\u57df\u5185\u7684iBeacon\u7684proximityUUID\u548cmajor\u4e0e\u6b64proximityUUID\u548cmajor\u76f8\u540c\u65f6\uff0c\u4e0d\u8bbaminor\u4e3a\u4f55\u503c\uff0c\u90fd\u80fd\u88ab\u68c0\u6d4b\u5230\u3002\n        CLBeaconRegion(proximityUUID:major:identifier:)\n\/\/\u4f7f\u7528proximityUUID, major, minor\u6765\u521d\u59cb\u5316\uff0c\u53ea\u80fd\u68c0\u6d4b\u5230\u533a\u57df\u5185\u76f8\u540cproximityUUID, major, minor\u7684iBeacon\u8bbe\u5907\u3002\n        CLBeaconRegion(proximityUUID:major: minor:identifier:)\n<\/pre>\n\n\n\n<p>3.\u53ef\u7528\u4e24\u79cd\u65b9\u5f0f\u68c0\u6d4b\u533a\u57df Monitoring\u6216Ranging\u65b9\u5f0f\u3002<\/p>\n\n\n\n<p>Monitoring\u65b9\u5f0f: \u53ef\u4ee5\u7528\u6765\u5728\u8bbe\u5907\u8fdb\u5165\/\u9000\u51fa\u67d0\u4e2a\u5730\u7406\u533a\u57df\u65f6\u83b7\u5f97\u901a\u77e5, \u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5728\u5e94\u7528\u7a0b\u5e8f\u7684\u540e\u53f0\u8fd0\u884c\u65f6\u68c0\u6d4biBeacon\uff0c\u4f46\u662f\u53ea\u80fd\u540c\u65f6\u68c0\u6d4b20\u4e2aregion\u533a\u57df\uff0c\u5e76\u4e14\u4e0d\u80fd\u591f\u63a8\u6d4b\u8bbe\u5907\u4e0eiBeacon\u7684\u8ddd\u79bb\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">locationManager?.startMonitoring(for: beaconRegin!)\n    locationManager?.stopMonitoring(for: beaconRegin!)\n    \/\/ \u8bbe\u5907\u8fdb\u5165\u8be5\u533a\u57df\u65f6\u7684\u56de\u8c03\n    func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {\n\n    }\n    \/\/ \u8bbe\u5907\u9000\u51fa\u8be5\u533a\u57df\u65f6\u7684\u56de\u8c03\n    func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {\n\n    }\n    \/\/ \u6709\u9519\u8bef\u4ea7\u751f\u65f6\u7684\u56de\u8c03\n    func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) {\n\n    }\n<\/pre>\n\n\n\n<p>Ranging\u65b9\u5f0f: \u53ef\u4ee5\u7528\u6765\u68c0\u6d4b\u67d0\u533a\u57df\u5185\u7684\u6240\u6709iBeacons\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">locationManager?.startRangingBeacons(in: beaconRegin!)\n    locationManager?.stopRangingBeacons(in: beaconRegin!)\n     \/\/ \u68c0\u6d4b\u5230\u533a\u57df\u5185\u7684iBeacons\u65f6\u56de\u8c03\u6b64\u51fd\u6570\uff0c\u5dee\u4e0d\u591a1s\u5237\u65b0\u4e00\u6b21\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u8fd4\u56de\u4e00\u4e2a CLBeacon \u7684\u6570\u7ec4\uff0c\u6839\u636e CLBeacon \u7684 proximity \u5c5e\u6027\u5c31\u53ef\u4ee5\u5224\u65ad\u8bbe\u5907\u548c beacon \u4e4b\u95f4\u7684\u8ddd\u79bb,\n     \/\/ proximity \u5c5e\u6027\u6709\u56db\u4e2a\u53ef\u80fd\u7684\u503c\uff0cunknown\u3001immediate\u3001near \u548c far, \u53e6\u5916 CLBeacon \u8fd8\u6709 accuracy \u548c rssi \u4e24\u4e2a\u5c5e\u6027\u80fd\u63d0\u4f9b\u66f4\u8be6\u7ec6\u7684\u8ddd\u79bb\u6570\u636e\n    func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {\n\n    }\n    \/\/ \u6709\u9519\u8bef\u65f6\u5019\u7684\u56de\u8c03\n    func locationManager(_ manager: CLLocationManager, rangingBeaconsDidFailFor region: CLBeaconRegion, withError error: Error) {\n\n    }\n<\/pre>\n\n\n\n<p>\u6ce8\u610f:\u5982\u679c\u641c\u7d22\u4e0d\u5230\u8bbe\u5907\u5e7f\u64ad,\u770b\u770b\u662f\u4e0d\u662f\u56e0\u4e3aUUID\u7b49\u552f\u4e00\u6807\u793a\u662f\u4e0d\u662f\u4e0d\u4e00\u6837<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u66f4\u591a\u60f3\u6cd5\u8ba8\u8bba<\/h5>\n\n\n\n<p>1.\u8ddd\u79bb\u662f\u5426\u51c6\u786e<\/p>\n\n\n\n<p>\u7f51\u4e0a\u6709\u8bb8\u591a\u56f4\u7ed5\u6bcf\u6b21 ranging event\uff08\u6307\u7684\u5c31\u662f\u8303\u56f4\u6a21\u5f0f\u7684 didRangeBeacons \u56de\u8c03\uff09 \u6240\u8fd4\u56de\u7684 CLLocationAccuracy \u503c\u7684\u8ba8\u8bba\u3002\u4eba\u4eec\u5e38\u628a\u8fd9\u4e2a\u503c\u5f53\u4f5c\u624b\u673a\u4e0e\u53d1\u5c04\u5668\u7684\u5b9e\u9645\u8ddd\u79bb\u503c\u3002\u4ee5\u6211\u7684\u7ecf\u9a8c\u6765\u770b\uff0c\u4f60\u5f53\u7136\u53ef\u4ee5\u628a\u8fd9\u4e2a\u503c\u5f53\u4f5c\u5b9e\u9645\u7684\u8ddd\u79bb\uff0c\u4f46\u5b83\u5e76\u4e0d\u603b\u662f\u90a3\u4e48\u51c6\u786e\u3002\u82f9\u679c\u6587\u6863\u5efa\u8bae\u4f60\u9996\u5148\u5229\u7528 CLProxity \u679a\u4e3e\u503c\u6765\u521d\u6b65\u5224\u5b9a\u8bbe\u5907\u8ddd\u79bb\u7684\u8fdc\u8fd1\uff0c\u7136\u540e\u518d\u7528 CLLocationAccuracy \u7684\u503c\u6765\u533a\u5206\u5176\u4e2d\u63a5\u8fd1\u5ea6\u76f8\u8fd1\u7684\u503c\u3002<\/p>\n\n\n\n<p>2.\u76d1\u542c\u8d85\u8fc7 20 \u4e2a\u8bbe\u5907<\/p>\n\n\n\n<p>\u6b63\u50cf\u6211\u524d\u8fb9\u4ecb\u7ecd\u7684\u90a3\u6837\uff0c\u4f60\u73b0\u5728\u53ea\u80fd\u76d1\u542c 20 \u4e2a iBeacons \uff0c\u5982\u679c\u4f60\u9700\u8981\u76d1\u542c\u8d85\u8fc7 20 \u4e2a\u8bbe\u5907\uff0c\u4f60\u5c06\u9700\u8981\u5728\u5e94\u7528\u8fd0\u884c\u7684\u8fc7\u7a0b\u4e2d\u66f4\u6539\u76d1\u542c\u7684\u8bbe\u7f6e\uff0c\u4e00\u79cd\u5b9e\u73b0\u65b9\u6848\u662f\u7528\u56fe\u8868\u6765\u5c55\u793a\u4f60\u7684 iBeacons \u7f51\u7edc\uff0c\u5728\u7f51\u7edc\u4e2d\u5b9a\u4e49\u6700\u9876\u5c42\u7684 iBeacons \u4ee5\u53ca\u5982\u679c\u8fd9\u4e9b\u5f7c\u6b64\u63a5\u8fd1\u7684\u60c5\u51b5\u4e0b\u80fd\u591f\u8fde\u63a5\u5230\u7684\u8fb9\u754c\u3002\u8fd9\u6837\u4f60\u5c31\u53ef\u4ee5\u5feb\u901f\u7684\u67e5\u627e\u5230\u6700\u63a5\u8fd1\u7684 20 \u4e2a iBeacons \u5e76\u76d1\u542c\u4ed6\u4eec\u3002\u8fd9\u9700\u8981\u5f88\u591a\u7684\u5de5\u4f5c\uff0c\u4f46\u662f\u5b9a\u4e49\u4e00\u4e2a\u8fd9\u6837\u7684\u62d3\u6251\u662f\u4e00\u79cd\u5b9e\u73b0 20 \u4e2a iBeacons \u9650\u5236\u7684\u65b9\u5f0f\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u4e00\u4e2a\u5b8c\u6574\u7684Beacon\u4f8b\u5b50\uff1a<\/h5>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import UIKit\nimport CoreLocation\nimport CoreBluetooth\n\n\/\/\u84dd\u7259\u5f00\u542f\u901a\u77e5\n\/\/ did centralManager enable notification\nlet BluetoothNotificationManagerEnable = \"BabyNotificationAtCentralManagerEnable\"\n\n\/\/ \u84dd\u7259\u5f39\u6846\u662f\u9700\u8981\u5f39\u51fa\nlet BluetoothAlertIsShow = \"BluetoothAlertIsShow\"\n\n\/\/ \u641c\u7d22\u5230\u8bbe\u5907\u6570\u7ec4\ntypealias IbeaconSearchResults = (([CLBeacon]) -> ())\n\nclass IbeaconManager: NSObject {\n\n    static let `default` = IbeaconManager()\n    \n    var searchResultsCallback: IbeaconSearchResults?\n    \n    fileprivate var beaconSendRegion: CLBeaconRegion! \/\/ \u53d1\u9001\n    fileprivate var beaconReceiveRegion: CLBeaconRegion! \/\/ \u63a5\u53d7\n    fileprivate var locationManager: CLLocationManager!\n    fileprivate var beaconPeripheralData: NSDictionary!\n    fileprivate var peripheraManager: CBPeripheralManager!\n    var location: Float = 0.0 \/\/\u8ddd\u79bb\n    let beaconIdentifier = \"ibeaconTest\"\n    let defaultUUIDString = \"XXXX-XXXXXX-XXXXXX-XXXXXXXXXX\"\n    \n    override init() {\n        super.init()\n        \n        \/\/ \u53d1\u5c04\u4fe1\u53f7\n        \/\/ \u6b64\u5904\u4ee3\u7801\u7528\u53e6\u4e00\u90e8\u624b\u673a\u8fd0\u884c \u6a21\u62dfibeacon\u8bbe\u5907\u53d1\u9001\u4fe1\u53f7\n        beaconSendRegion = CLBeaconRegion(proximityUUID: UUID(uuidString: defaultUUIDString)!, major: 1234, minor: 5678, identifier: beaconIdentifier)\n        beaconPeripheralData = beaconSendRegion.peripheralData(withMeasuredPower: nil)\n        peripheraManager = CBPeripheralManager(delegate: self, queue: nil)\n        \n\n        \/\/ \u63a5\u53d7\u4fe1\u53f7\n        locationManager = CLLocationManager()\n        locationManager.delegate = self\n        locationManager.requestAlwaysAuthorization()\n        beaconReceiveRegion = CLBeaconRegion(proximityUUID: UUID(uuidString: defaultUUIDString)!, identifier: beaconIdentifier)\n        beaconReceiveRegion.notifyEntryStateOnDisplay = true\n        \n        \/\/\u8bf7\u6c42\u4e00\u76f4\u5141\u8bb8\u5b9a\u4f4d\n        locationManager.requestAlwaysAuthorization()\n        beaconReceiveRegion.notifyEntryStateOnDisplay = true\n    }\n    \n    \/\/\/ \u5f00\u59cb\u626b\u63cf\n    func startRunningBeacons() {\n        \/\/\u5f00\u59cb\u626b\u63cf\n        locationManager.startMonitoring(for: beaconReceiveRegion)\n        locationManager.startRangingBeacons(in: beaconReceiveRegion)\n    }\n    \n}\n\nextension IbeaconManager: CLLocationManagerDelegate {\n    \n    \/\/\u8fdb\u5165beacon\u533a\u57df\n    func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {\n        locationManager.startRangingBeacons(in: beaconReceiveRegion)\n        print( \"\u8fdb\u5165beacon\u533a\u57df\")\n    }\n    \n    \/\/\u79bb\u5f00beacon\u533a\u57df\n    func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {\n        locationManager.stopRangingBeacons(in: beaconReceiveRegion)\n        print(\"\u79bb\u5f00beacon\u533a\u57df\")\n    }\n    \n    func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {\n        \/\/\u8fd4\u56de\u662f\u626b\u63cf\u5230\u7684beacon\u8bbe\u5907\u6570\u7ec4\uff0c\u8fd9\u91cc\u53d6\u7b2c\u4e00\u4e2a\u8bbe\u5907\n        guard beacons.count > 0 else { return }\n        \n        self.searchResultsCallback?(beacons)\n        \n        \/\/ \u4e0b\u9762\u4e3a\u8c03\u8bd5\u4fe1\u606f\n        let beacon = beacons.first!\n        print(\"major====\",beacon.major)\n        print(\"minor====\",beacon.minor)\n        \/\/accuracy\u53ef\u4ee5\u83b7\u53d6\u5230\u5f53\u524d\u8ddd\u79bbbeacon\u8bbe\u5907\u8ddd\u79bb\n        let location = String(format: \"%.3f\", beacon.accuracy)\n        print( \"\u8ddd\u79bb\u7b2c\u4e00\u4e2abeacon\\(location)m\")\n    }\n    \n    func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) {\n        print(\"Failed monitoring region: \\(error.localizedDescription)\")\n    }\n    \n    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {\n        print(\"Location manager failed: \\(error.localizedDescription)\")\n    }\n}\n\nextension IbeaconManager: CBPeripheralManagerDelegate {\n    \n    func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {\n        \n        switch peripheral.state {\n        case .poweredOn:\n            peripheraManager.startAdvertising(beaconPeripheralData as? [String : Any])\n            print(\"\u84dd\u7259\u6253\u5f00\u4e86\uff01=============================\")\n            print(beaconReceiveRegion.proximityUUID)\n            print(beaconReceiveRegion.major)\n            print(beaconReceiveRegion.minor)\n            print(beaconReceiveRegion.identifier)\n            print(\"\u84dd\u7259\u6253\u5f00\u4e86\uff01=============================\")\n            UserDefaults.standard.set(true, forKey: BluetoothNotificationManagerEnable)\n            UserDefaults.standard.synchronize()\n        case .poweredOff:\n            print(\"\u84dd\u7259\u672a\u6253\u5f00\")\n            UserDefaults.standard.set(false, forKey: BluetoothNotificationManagerEnable)\n            UserDefaults.standard.synchronize()\n        default: peripheraManager.stopAdvertising()\n            \n        }\n    }\n}<\/pre>\n\n\n\n<p>\u9700\u8981\u4f7f\u7528\u7684\u5730\u65b9<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/\/ \u5f00\u59cb\u641c\u7d22\u84dd\u7259\u5217\u8868 | \u53ea\u8981\u641c\u7d22\u5230\u4e86\u5c31\u8bb0\u5f55\u503c \u7528\u4e8e\u5bf9\u6bd4\u6559\u5e08\u8bfe\u7a0b\u91cc\u7684UUID\u662f\u5426\u8ddf\u8fd9\u4e2a\u4e00\u81f4 | \u5982\u679c\u4e00\u81f4\u8bf4\u660e\u5728\u84dd\u7259\u641c\u7d22\u7684\u8303\u56f4\u5185 \u53ef\u4ee5\u6267\u884c\u7b7e\u5230 \u5982\u679c\u6ca1\u6709\u5219\u4e0d\u5728\u641c\u7d22\u8303\u56f4\u5185\n    func startBlueToothSearch(blueToothNotOpen: (()->())?) {\n        \n        IbeaconManager.default.startRunningBeacons()\n        IbeaconManager.default.searchResultsCallback = { (ibeacons) in\n            var location = Double(1000)\n            for ibeacon in ibeacons {\n                let majorMinor = \"\\(ibeacon.major)\\(ibeacon.minor)\"\n                \/\/ \u503c\u8d8a\u5c0f\u4ee3\u8868\u8ddd\u79bb\u6700\u8fd1 ibeacon.accuracy \u4e3a\u8ddd\u79bb\n                if ibeacon.accuracy &lt;= location {\n                    location = ibeacon.accuracy\n                }\n                print(\"major====\",ibeacon.major)\n                print(\"minor====\",ibeacon.minor)\n                let location = String(format: \"%.3f\", ibeacon.accuracy)\n                print( \"\u8ddd\u79bbbeacon====\\(location)m\")\n            }\n        }\n        \n        \/\/ \u68c0\u6d4b\u84dd\u7259\u662f\u5426\u6253\u5f00\n        if let enable = UserDefaults.standard.value(forKey: BluetoothNotificationManagerEnable) as? Bool {\n            if enable == false {\n                blueToothNotOpen?()\n            }\n        }else {\n            blueToothNotOpen?()\n        }\n    }<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>iBeacon\u662f\u4ec0\u4e48? \u7ef4\u57fa\u767e\u79d1\uff1aiBeacon\u662f\u82f9\u679c\u516c\u53f8\u63d0\u51fa\u7684\u201d\u4e00\u79cd\u53ef\u4ee5\u8ba9\u9644\u8fd1\u624b\u6301\u7535\u5b50\u8bbe\u5907\u68c0\u6d4b\u5230\u7684\u4e00\u79cd\u65b0\u7684\u4f4e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,8],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3344"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3344"}],"version-history":[{"count":11,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3344\/revisions"}],"predecessor-version":[{"id":3355,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3344\/revisions\/3355"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}