CoreBluetooth Central - >外围设备

Jul*_*ert 3 ios core-bluetooth bluetooth-lowenergy cbperipheral cbcentralmanager

我对蓝牙通信很陌生.我的第一个项目打算将数据从iOS设备传输到BLEshield(小芯片).

为了测试我的中央代码,我决定设置一个iPhone作为外围设备(芯片将拥有的角色,一旦我拿到它)和一个iPad作为中心.

我可以连接设备,也可以将数据从外设发送到中心.这很容易:

- (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];
}
Run Code Online (Sandbox Code Playgroud)

但我不能让另一个方向发挥作用.要从中央发送数据,我有以下代码:

[_activePeripheral writeValue:[@"PONG" dataUsingEncoding:NSUTF8StringEncoding] forCharacteristic:_writeChar type:CBCharacteristicWriteWithoutResponse];
Run Code Online (Sandbox Code Playgroud)

我假设应该在外设上调用这些方法中的任何一个:

- (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
Run Code Online (Sandbox Code Playgroud)

但事实上没有任何反应 不幸的是,我的硬件项目将使用只能在外设模式下工作的芯片,最后我几乎只能写入外设,因为它是控制信号的发送器.

我希望有一个人可以帮助我!

san*_*try 5

属性:

  • _readChar 应该 CBCharacteristicPropertyRead
  • _writeChar 应该 CBCharacteristicPropertyWriteWithoutResponse

有关详细信息,请参见此处 CBCharacteristicPropertyNotify不允许特征可写或可读,它只是通知(订阅/取消订阅).