Search code examples
iosswiftibeaconapple-tv

Can I turn my new 4th gen AppleTV into an iBeacon?


I read you can write apps for the new AppleTV. I read it runs a version of iOS. I read too that it has bluetooth capabilities.

Question, very simply, could I turn my AppleTV into an iBeacon, albeit a very expensive one? :)


Solution

  • Ok, I got around to testing this at last, with the result ok, some promise. Here is the details, tvOS doesn't have CLRegion, will not compile, so the official route for iBeacon don't work.

    Did however get it to see the iBeacon with this code; so there is hope... maybe not with iBeacons, but Bluetooth...

    This is the BLE code I used...

    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")
    }
    
    }
    

    With these lines in the 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()
    

    Need to say a special thank you to rhythmic-fistman who helped me with the code!