I have gone and set up my CBCentralManager
to search for devices and have the basic structure ready to receive and check on updated info.
I just cant seem to grasp on how to make a CBPeripheralManager
ViewController and how to send my CBCentral
data from the separate app on the press of a button. Simplest way being to send some string.
Here is my CBCentralManager
ViewController.
class ViewController: NSViewController, CBCentralManagerDelegate,CBPeripheralDelegate {
let TRANSFER_SERVICE_UUID = "FB694B90-F49E-4597-8306-171BBA78F846"
let TRANSFER_CHARACTERISTIC_UUID = "EB6727C4-F184-497A-A656-76B0CDAC633A"
var centralManager: CBCentralManager?
var discoveredPeripheral: CBPeripheral?
override func viewDidLoad() {
super.viewDidLoad()
centralManager = CBCentralManager(delegate: self, queue: nil)
// Do any additional setup after loading the view.
}
func centralManagerDidUpdateState(_ central: CBCentralManager) {
if (central.state != .poweredOn) {
return
}
else{
let serviceUUID:[CBUUID] = [CBUUID(string: self.TRANSFER_SERVICE_UUID)]
centralManager!.scanForPeripherals(withServices: serviceUUID, options: nil)
}
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
print("Discovered a peripheral")
print(peripheral.identifier)
print(peripheral.name!)
print(RSSI)
if(discoveredPeripheral != peripheral){
discoveredPeripheral = peripheral
centralManager?.stopScan()
print("Connection to peripheral")
centralManager?.connect(peripheral, options: nil)
}
}
func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
print(error!.localizedDescription)
centralManager?.cancelPeripheralConnection(peripheral)
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
print("Connected")
peripheral.delegate = self
let serviceUUIDS:[CBUUID] = [CBUUID(string: self.TRANSFER_SERVICE_UUID)]
peripheral.discoverServices(nil)
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
if error != nil{
centralManager?.cancelPeripheralConnection(peripheral)
}
for service:CBService in peripheral.services as [CBService]!{
peripheral.discoverCharacteristics(nil, for: service)
}
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
if error != nil{
centralManager?.cancelPeripheralConnection(peripheral)
}
for characteristic:CBCharacteristic in service.characteristics as [CBCharacteristic]!{
if characteristic.uuid.isEqual(CBUUID(string:self.TRANSFER_CHARACTERISTIC_UUID)){
peripheral.setNotifyValue(true, for: characteristic)
}
}
}
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
let stringFromData:String = String(data: characteristic.value!, encoding: String.Encoding.utf8)!
//if
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
Is this done by doing the exact opposite? I want to make sure I am looking at this in the right direction.
UPDATE
Inside the other application I am trying to begin this process like so:
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
if peripheralManager?.state != .poweredOn {
return
} else {
let serviceUUId:CBUUID = CBUUID(string:self.TRANSFER_SERVICE_UUID)
let mutable:CBMutableService = CBMutableService(type: serviceUUId, primary: true)
peripheralManager?.add(mutable)
}
}
Is the next step to start advertising?