4

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!

Julian F. Weinert
  • 7,474
  • 7
  • 59
  • 107

2 Answers2

6

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).

sandeepmistry
  • 2,058
  • 4
  • 20
  • 25
  • When I set then up like this, I do not get notified of connects. I now set them up with `CBCharacteristicPropertyRead | CBCharacteristicPropertyNotify` but I still get no write notifications... – Julian F. Weinert Nov 18 '13 at 09:18
  • I now actually got the notification and was able to read the value of it! THANKS! – Julian F. Weinert Nov 18 '13 at 09:26
  • @Julian: I get no response too, and if I set them up with both `Read|Notify` I also get no response, how did you get it working from BLTETransfer of Apple's sample? Thx – Zennichimaro Nov 04 '14 at 09:40
  • @Zennichimaro I succeeded with two characteristics, one set to `read` the other one to `write` as stated in the answer. See my code from the OP and the answer above... Should do the trick. Although I didn't use apples example. – Julian F. Weinert Nov 09 '14 at 20:19
-1

Just use https://github.com/LGBluetooth/LGBluetooth It will make life easier

l0gg3r
  • 8,864
  • 3
  • 26
  • 46