Matthew Griffin
Matthew Griffin

Reputation: 391

CBPeripheralManager read/write

I'm using CoreBluetooth on the iPhone and I'm wondering if there's a way to do read/write requests from the peripheral side. I have set up a CBPeripheralManager and a delegate, but looking through the documentation I don't see any way to read from a peripheral's attributes or even its own attributes.

Is this only possible from Central?

Upvotes: 1

Views: 851

Answers (2)

Mark
Mark

Reputation: 7419

If you're trying to send data from a CBPeripheralManager to one or more connected CBCentrals, use a characteristic that has notify or indicate enabled. The CBPeripheralManager can send data like so:

let success = peripheralManager.updateValue(data, forCharacteristic: characteristic,
    onSubscribedCentrals: [central])
if !success {
    // cache the data until the delegate method peripheralManagerIsReadyToUpdateSubscribers is called.
}

Note: Make sure the CBCentrals subscribe to the characteristic you plan on using.

As for reading data from a CBCentral, you could send a "read request" using the method above, and have the CBCentral respond with the data you're looking for.

Upvotes: 0

cbowns
cbowns

Reputation: 6375

Yes. CoreBluetooth assumes that if you're setting up a peripheral, you wouldn't query the Bluetooth stack to retrieve attributes about yourself, and instead would simply query some internal data structure.

Upvotes: 1

Related Questions