我可以将新的第 4 代 AppleTV 变成 iBeacon 吗?
Can I turn my new 4th gen AppleTV into an iBeacon?
我了解到您可以为新的 AppleTV 编写应用程序。我读到它运行 iOS 的版本。我也读到它具有蓝牙功能。
问题很简单,我可以将我的 AppleTV 变成 iBeacon,尽管它非常昂贵吗? :)
作为人们 "jailbreak"(或直接在 Linux 上安装)Apple TV,您几乎可以安装任何东西。由于 iBeacon 是 Apple 开放的标准(并且有开源代码),我会说答案是 "yes"。
你进行iOS设备广播的方法是,你用你想要广播的UUID、主要和次要创建一个CLBeaconRegion
对象,调用它的peripheralDataWithMeasuredPower
method, and pass the dictionary you obtain this way to CBPeripheralManager
's startAdvertising
方法。
现在,tvOS 缺少 CLBeaconRegion
class,但具有 CBPeripheralManager
和 startAdvertising
方法。这意味着您应该能够在 iOS 设备上生成要传递给 startAdvertising
的字典,获取其内容,并将其复制到您的 tvOS 应用程序中。
事实上,过去人们已经用 OS X Mavericks 这样做了(我认为 Apple 在 Yosemite 中阻止了这一点):http://www.blendedcocoa.com/blog/2013/11/02/mavericks-as-an-ibeacon/
注意:这个方法我自己没试过。 Apple 完全有可能在 tvOS 上屏蔽了这个技巧,就像他们在 OS X.
上所做的那样
好吧,我终于抽出时间来测试一下,结果还好,有些许诺。这是详细信息,tvOS 没有 CLRegion,无法编译,因此 iBeacon 的官方路由不起作用。
但是用这段代码看到了iBeacon;所以还有希望……也许不是 iBeacons,而是蓝牙……
这是我用的BLE码...
import Foundation
import CoreBluetooth
class BLEManager {
var centralManager : CBCentralManager!
var bleHandler : BLEHandler // delegate
required init() {
self.bleHandler = BLEHandler()
self.centralManager = CBCentralManager(delegate: self.bleHandler, queue: dispatch_get_main_queue())
}
}
class BLEHandler : NSObject, CBCentralManagerDelegate {
override init() {
super.init()
}
func centralManagerDidUpdateState(central: CBCentralManager) {
switch (central.state)
{
case .Unsupported:
print("BLE is unsupported")
case .Unauthorized:
print("BLE is unauthorized")
case .Unknown:
print("BLE is unknown")
case .Resetting:
print("BLE is reseting")
case .PoweredOff:
print("BLE is powered off")
case .PoweredOn:
print("BLE is powered on")
central.scanForPeripheralsWithServices(nil, options: nil)
default:
print("BLE default")
}
print("centralManagerDidUpdateState")
}
func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
print("didConnectPeripheral")
}
func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
print("\(peripheral.name) : \(RSSI) dBm")
}
func centralManager(central: CBCentralManager, didFailToConnectPeripheral peripheral: CBPeripheral, error: NSError?) {
}
func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {
print("didDiscoverServices")
}
func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?) {
print("didDiscoverCharacteristicsForService")
}
func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?) {
print("didUpdateValueForCharacteristic")
}
func peripheral(peripheral: CBPeripheral, didUpdateNotificationStateForCharacteristic characteristic: CBCharacteristic, error: NSError?) {
print("didUpdateNotificationStateForCharacteristic")
}
func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?) {
print("didDisconnectPeripheral")
}
}
在 viewontroller 中使用这些行
var bleManager: BLEManager!
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
bleManager = BLEManager()
特别感谢帮我写代码的rhythmic-fistman!
我了解到您可以为新的 AppleTV 编写应用程序。我读到它运行 iOS 的版本。我也读到它具有蓝牙功能。
问题很简单,我可以将我的 AppleTV 变成 iBeacon,尽管它非常昂贵吗? :)
作为人们 "jailbreak"(或直接在 Linux 上安装)Apple TV,您几乎可以安装任何东西。由于 iBeacon 是 Apple 开放的标准(并且有开源代码),我会说答案是 "yes"。
你进行iOS设备广播的方法是,你用你想要广播的UUID、主要和次要创建一个CLBeaconRegion
对象,调用它的peripheralDataWithMeasuredPower
method, and pass the dictionary you obtain this way to CBPeripheralManager
's startAdvertising
方法。
现在,tvOS 缺少 CLBeaconRegion
class,但具有 CBPeripheralManager
和 startAdvertising
方法。这意味着您应该能够在 iOS 设备上生成要传递给 startAdvertising
的字典,获取其内容,并将其复制到您的 tvOS 应用程序中。
事实上,过去人们已经用 OS X Mavericks 这样做了(我认为 Apple 在 Yosemite 中阻止了这一点):http://www.blendedcocoa.com/blog/2013/11/02/mavericks-as-an-ibeacon/
注意:这个方法我自己没试过。 Apple 完全有可能在 tvOS 上屏蔽了这个技巧,就像他们在 OS X.
上所做的那样好吧,我终于抽出时间来测试一下,结果还好,有些许诺。这是详细信息,tvOS 没有 CLRegion,无法编译,因此 iBeacon 的官方路由不起作用。
但是用这段代码看到了iBeacon;所以还有希望……也许不是 iBeacons,而是蓝牙……
这是我用的BLE码...
import Foundation
import CoreBluetooth
class BLEManager {
var centralManager : CBCentralManager!
var bleHandler : BLEHandler // delegate
required init() {
self.bleHandler = BLEHandler()
self.centralManager = CBCentralManager(delegate: self.bleHandler, queue: dispatch_get_main_queue())
}
}
class BLEHandler : NSObject, CBCentralManagerDelegate {
override init() {
super.init()
}
func centralManagerDidUpdateState(central: CBCentralManager) {
switch (central.state)
{
case .Unsupported:
print("BLE is unsupported")
case .Unauthorized:
print("BLE is unauthorized")
case .Unknown:
print("BLE is unknown")
case .Resetting:
print("BLE is reseting")
case .PoweredOff:
print("BLE is powered off")
case .PoweredOn:
print("BLE is powered on")
central.scanForPeripheralsWithServices(nil, options: nil)
default:
print("BLE default")
}
print("centralManagerDidUpdateState")
}
func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
print("didConnectPeripheral")
}
func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
print("\(peripheral.name) : \(RSSI) dBm")
}
func centralManager(central: CBCentralManager, didFailToConnectPeripheral peripheral: CBPeripheral, error: NSError?) {
}
func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {
print("didDiscoverServices")
}
func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?) {
print("didDiscoverCharacteristicsForService")
}
func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?) {
print("didUpdateValueForCharacteristic")
}
func peripheral(peripheral: CBPeripheral, didUpdateNotificationStateForCharacteristic characteristic: CBCharacteristic, error: NSError?) {
print("didUpdateNotificationStateForCharacteristic")
}
func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?) {
print("didDisconnectPeripheral")
}
}
在 viewontroller 中使用这些行
var bleManager: BLEManager!
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
bleManager = BLEManager()
特别感谢帮我写代码的rhythmic-fistman!