CoreBluetooth Central --> Peripheral

倾然丶 夕夏残阳落幕 提交于 2020-01-01 16:49:53

问题


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!


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/20034433/corebluetooth-central-peripheral

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!