Peripheral and Central at the Same Time on iOS

Peripheral and central at the same time on iOS

Yes, it can be in both roles at the same time. You just have to initialize a CBPeripheralManager and a CBCentralManager. As soon as the peripheral manager is initialized and you receive the POWER ON state the device starts acting as a peripheral. You can add your services at this point and receive connections from other devices. At the same time you can use the central manager to scan and initiate connections to other peripherals.

Note that you cannot connect to your own device even if it acts as a peripheral.

For your errors, I suggest:

  1. Turn off scanning before initiating a connection. That is, scan, find peripheral, stop scan, connect. Connection and scanning do not like each other.
  2. Use a dedicated queue for handling bluetooth events, not the main queue. [[CBCentralManager alloc] initWithDelegate:self queue:my_dedicated_bluetooth_q]
  3. Unfortunately, the stack sometimes become unstable. Even restarts are possible. But this usually happens only under heavy loads or several simultaneous connections. Hopefully, this will be improved in iOS7.
  4. The unfamous Unknown error started to appear for several developers recently. Judging from your description there are probably a number of reasons why your setup may fail and it would require much more info that what fits well into a SO question.

For more info I suggest you search the bluetooth-dev mailing list archives https://lists.apple.com/archives/Bluetooth-dev or send a mail Bluetooth-dev@lists.apple.com. The community provides great help if you approach with reasonable questions like this.

Can iOS do central and peripheral work on same app at same time?

I got it working. I just started with the Apple "BTLE central peripheral transfer", then first delted the -35 db bug that it has (search for "-35" then delete the if(){ return }), then I combined both the central.m and the peripheral.m into a single UIViewController .m file, added a UISwitch to select one of two service UUID's, and modified the peripheral sender to automatically increment the text field (after init'ing it to ASCII '0').

I had two iPad mini's continuously each sending the incrementing number to the other side. It got up to over 900 transfers and then hung. But I've seen the Apple "BTLE c p transfer" always hang after a few minutes, requiring iPad restart to continue. I ended app at both iPad's and cycled power, re-started app, and they got up to 1600 increments, and then hung.

To solve the hang, I'll add resource control to prevent central and peripheral managers from connecting at same time, as per Abo's recommendation.

Peripheral and Central at the same time on the same app iOS11

I'm getting back with the correct way of achieving the required functionality. After initialising the peripheralManager, create a CBMutableService and hold a reference to it(declared at the top of the class).

var globalService:CBMutableService? = nil

Next step is to check for the peripheralManager state, and do all the required work after you receive the poweredOn state:

func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
switch(peripheral.state)

case.poweredOn:
print("Peripheral service is powered on")

createServiceWithCharacteristics()
}

func createServiceWithCharacteristics(){

let serviceUUID:CBUUID = CBUUID(string: self.service_uuid_string)
let featureCharacteristicUUID:CBUUID = CBUUID(string: self.feature_characteristic_uuid_string)

// Start with the CBMutableCharacteristic
let permissions: CBAttributePermissions = [.readable, .writeable]
let properties: CBCharacteristicProperties = [.notify, .read, .write]

self.featureCharacteristic = CBMutableCharacteristic(type: featureCharacteristicUUID, properties: properties , value: nil, permissions: permissions)

// Then the service
let localService = CBMutableService(type: serviceUUID, primary: true)

// Add the characteristic to the service
localService.characteristics = [featureCharacteristic!]
globalService = localService

// And add it to the peripheral manager
self.peripheralManager?.add(globalService!)
print("Start advertising.")
peripheralManager?.startAdvertising([CBAdvertisementDataLocalNameKey:"Name"])
}

Can BLE iOS device be peripheral and central at the same time?

Yes - You can
Althpough the same device being central and peribheral at the same time is not standard as pe Bluetooth current specification, iOS currently supports it

Can CoreBluetooth act as both a central and peripheral at the same time (Unknown error 0)?

I can confirm CoreBluetooth on iOS doesn't have this issue, at least not when I last tested it on iOS 6 on an iPhone 5. The iPhone can advertise as a BLE peripheral while talking to another BLE peripheral as the BLE Central at the same time. I don't have results on OS X. But I will be really suprised if OS X, on a much more powerful platform, has such limit.



Related Topics



Leave a reply



Submit