CoreBluetooth Central --> Peripheral
Asked Answered
H

2

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!

Hollington answered 17/11, 2013 at 18:20 Comment(0)
I
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).

Immutable answered 17/11, 2013 at 21:55 Comment(5)
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...Hollington
I now actually got the notification and was able to read the value of it! THANKS!Hollington
<CBCharacteristicPropertyRead, Worked for me.Submerse
@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? ThxCercus
@Cercus 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.Hollington
A
-1

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

Aneurysm answered 15/2, 2014 at 10:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.