Last active
January 15, 2017 09:51
-
-
Save river24/e7e4df806da9e53b4d93 to your computer and use it in GitHub Desktop.
MR04LNをリモート起動させるSwiftスクリプト
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
| #!/usr/bin/swift | |
| import Cocoa | |
| import IOBluetooth | |
| var TARGET_NAME = "xxxxxx-bt" | |
| var TARGET_ADDR = "ab-cd-ef-12-34-56" | |
| var SCAN_DURATION = 10 | |
| class BluetoothManager: NSObject, IOBluetoothDeviceInquiryDelegate { | |
| var inquiry: IOBluetoothDeviceInquiry! | |
| var found: Bool = false | |
| override init() { | |
| super.init() | |
| self.inquiry = IOBluetoothDeviceInquiry(delegate: self) | |
| self.inquiry.updateNewDeviceNames = true | |
| } | |
| func start() -> IOReturn { | |
| return self.inquiry.start() | |
| } | |
| func stop() { | |
| self.inquiry.stop() | |
| } | |
| func isFound() -> Bool { | |
| return self.found | |
| } | |
| @objc func deviceInquiryDeviceFound(sender: IOBluetoothDeviceInquiry!, device: IOBluetoothDevice!) { | |
| if (device.addressString == TARGET_ADDR && device.name == TARGET_NAME) { | |
| found = true | |
| device.openConnection() | |
| } | |
| } | |
| } | |
| func wakeup() { | |
| let btManager = BluetoothManager() | |
| if (btManager.start() == kIOReturnSuccess) { | |
| sleep(UInt32(SCAN_DURATION)) | |
| btManager.stop() | |
| } | |
| } | |
| func usage(called_name: String) { | |
| print("Usage:") | |
| print(" \(called_name) DEVICE_NAME MAC_ADDR") | |
| print(" \(called_name) DEVICE_NAME MAC_ADDR SCAN_DURATION") | |
| print("") | |
| print("Options:") | |
| print(" DEVICE_NAME : device name for remote wakeup (like 'xxxxxx-bt')") | |
| print(" MAC_ADDR : macc address for remote wakeup") | |
| print(" format: 'ab-cd-ef-12-34-56' (lower-case characters)") | |
| print(" SCAN_DURATION : duration for scanning/waiting (default: 10)") | |
| print("") | |
| } | |
| var args = Process.arguments | |
| var called_name = args[0].componentsSeparatedByString("/").last! | |
| if args.count >= 3 { | |
| TARGET_NAME = args[1] | |
| TARGET_ADDR = args[2] | |
| if args.count >= 4 { | |
| SCAN_DURATION = Int(args[3])! | |
| } | |
| wakeup() | |
| } else { | |
| usage(called_name) | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment