2

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?

A. Petrizza
  • 3,202
  • 2
  • 15
  • 24
  • 2
    This code looks ok. You will need to create an instance of CBMutablePeripheral, add your service to it and add the characteristic to the service. You can then advertise that peripheral using CBPeripheralManager. – Paulw11 Jan 07 '17 at 21:00
  • So i just need to create a new viewcontroller on my separate app? Make a new instance of the CBMutablePeripheral, and then advertise to the CBCentralManager app? – A. Petrizza Jan 07 '17 at 21:07
  • And now i am getting lost on how to attach my characteristics to my service. Could you help me, by showing me a little snippet here? – A. Petrizza Jan 07 '17 at 21:17
  • 1
    Simply assign them to your service's `characteristics` property. – Paulw11 Jan 07 '17 at 21:20
  • It seems CBMutablePeripheral might be depricated? I can see CBMutableCharacteristic, but not what is mentioned above! – A. Petrizza Jan 07 '17 at 21:26
  • 1
    Sorry, that was my mistake. You just create the CBMutableService and advertise it via CBPeripheralManager. The iOS device itself is the peripheral and you don't need to explicitly create it – Paulw11 Jan 07 '17 at 21:28
  • Okay so the take away from here is that I need to create this service, add my characteristics to this service, and then advertise. Does that sound correct? – A. Petrizza Jan 07 '17 at 21:30
  • 2
    Yes. You also need to implement the delegate methods to respond to read requests – Paulw11 Jan 07 '17 at 21:49
  • How to disconnect peripheral? @A. Petrizza – Parth Barot Jul 18 '19 at 08:26

0 Answers0