Last active
August 29, 2015 14:04
-
-
Save dorentus/62a5a58625ced3d9bc53 to your computer and use it in GitHub Desktop.
iBeacon Demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import CoreLocation | |
@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate { | |
var window: UIWindow? | |
var locationManager: CLLocationManager! | |
lazy var beaconRegion: CLBeaconRegion = { | |
let uuid = NSUUID(UUIDString: "3c5ce91c-d0f5-4998-b193-41af3787f499") | |
let identifier = "rubyist.today.lighthouse" | |
let region = CLBeaconRegion(proximityUUID: uuid, identifier: identifier) | |
region.notifyEntryStateOnDisplay = true | |
return region | |
}() | |
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool { | |
if CLLocationManager.isMonitoringAvailableForClass(CLCircularRegion) { | |
self.locationManager = CLLocationManager() | |
self.locationManager.delegate = self | |
self.locationManager.startMonitoringForRegion(self.beaconRegion) | |
} | |
else { | |
let alertView = UIAlertView(title: "確認", message: "お使いの端末ではiBeaconを利用できません。", delegate: nil, cancelButtonTitle: "OK") | |
alertView.show() | |
} | |
return true | |
} | |
func locationManager(manager: CLLocationManager!, didEnterRegion region: CLRegion!) { | |
self.sendLocalNotificationForMessage("おかえりなさい、ご主人様", soundName: "enter.aiff") | |
} | |
func locationManager(manager: CLLocationManager!, didExitRegion region: CLRegion!) { | |
self.sendLocalNotificationForMessage("いってらっしゃい、ご主人様", soundName: "leave.aiff") | |
} | |
private func sendLocalNotificationForMessage(message: String?, soundName: String?) { | |
let notification = UILocalNotification() | |
notification.alertBody = message | |
notification.fireDate = NSDate() | |
if let soundName = soundName { | |
notification.soundName = soundName | |
} | |
else { | |
notification.soundName = UILocalNotificationDefaultSoundName | |
} | |
UIApplication.sharedApplication().scheduleLocalNotification(notification) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
[ $EUID -ne 0 ] && echo "Must be run as root" && exit | |
export BLUETOOTH_DEVICE=hci0 | |
export UUID="3c 5c e9 1c d0 f5 49 98 b1 93 41 af 37 87 f4 99" | |
export MAJOR="00 00" | |
export MINOR="00 00" | |
export POWER="C5" | |
export PATH=/usr/local/bin:$PATH | |
case $1 in | |
start) | |
echo "Launching virtual iBeacon..." | |
hciconfig $BLUETOOTH_DEVICE up | |
hciconfig $BLUETOOTH_DEVICE noleadv | |
hciconfig $BLUETOOTH_DEVICE leadv 0 | |
hcitool -i hci0 cmd 0x08 0x0008 1e 02 01 1a 1a ff 4c 00 02 15 $UUID $MAJOR $MINOR $POWER 00 | |
echo "Complete" | |
;; | |
stop) | |
echo "Disabling virtual iBeacon..." | |
hciconfig $BLUETOOTH_DEVICE noleadv | |
echo "Complete" | |
;; | |
*) | |
echo "Usage: $(basename $0) {start|stop}" | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment