Julian F. Weinert Julian F. Weinert - 3 years ago 146
iOS Question

CoreBluetooth Central --> Peripheral

I'm quite new to bluetooth communication. My first project intends to transfer data from an iOS device to a BLEshield (small chip).

To test my central code, I decided to setup an iPhone as peripheral (the role the chip will have, once I got it) and an iPad as Central.

I can connect the devices and also send data from the peripheral to the central. It's quite easy though:

- (void)startService {
_readChar = [[CBMutableCharacteristic alloc] initWithType:[CBUUID ...] properties:CBCharacteristicPropertyNotify value:nil permissions:CBAttributePermissionsReadable];
_writeChar = [[CBMutableCharacteristics alloc] initWithType:[CBUUID ...] properties:CBCharacteristicPropertyNotify value:nil permissions:CBAttributePermissionsWriteable];

_service = [[CBMutableService alloc] initWithType:[CBUUID ...] primary:YES];
[_service setCharacteristics:@[_readChar, _writeChar]];

_peripheral = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];
[_peripheral addService:_service];

[_peripheral startAdvertising:@{CBAdvertisementDataServiceUUIDKey: @[[CBUUID ...]], CBAdvertisementDataLocalNameKey: @"ServiceName"}];

- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic {
[_peripheral updateValue:[@"HELLO WORLD" dataUsingEncoding:NSUTF8StringEncoding] forCharacteristic:_readChar onSubscribedCentrals:nil];

BUT I cannot get the other direction working. To send data from the central side, I have the following code:

[_activePeripheral writeValue:[@"PONG" dataUsingEncoding:NSUTF8StringEncoding] forCharacteristic:_writeChar type:CBCharacteristicWriteWithoutResponse];

I assume that either one of these methods should be called on the peripheral:

- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveReadRequest:(CBATTRequest *)request
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray *)requests
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error

But actually nothing happens. Unfortunately my hardware project will use a chip that can only work in peripheral mode and in the end I will almost exclusively write to the peripheral as it is an transmitter for control signals.

I hope someone can help me!

Answer Source

The properties for:

  • _readChar should be CBCharacteristicPropertyRead
  • _writeChar should be CBCharacteristicPropertyWriteWithoutResponse

See here for more details. CBCharacteristicPropertyNotify does not allow the characteristic to be writable or readable, it is only notifiable (subscribe/unsubscribe).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download