问题描述
我对蓝牙通信很陌生。我的第一个项目打算将数据从iOS设备传输到BLEshield(小芯片)。
I'm quite new to bluetooth communication. My first project intends to transfer data from an iOS device to a BLEshield (small chip).
为了测试我的中心代码,我决定将iPhone设置为外围设备(一旦我拿到它,芯片将拥有的角色)和iPad作为中心。
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!
推荐答案
以下属性:
-
_readChar
应为CBCharacteristicPropertyRead
-
_writeChar
应为CBCharacteristicPropertyWriteWithoutResponse
_readChar
should beCBCharacteristicPropertyRead
_writeChar
should beCBCharacteristicPropertyWriteWithoutResponse
有关详细信息,请参阅。 CBCharacteristicPropertyNotify
不允许该特征可写或可读,只能通知(订阅/取消订阅)。
See here for more details. CBCharacteristicPropertyNotify
does not allow the characteristic to be writable or readable, it is only notifiable (subscribe/unsubscribe).
这篇关于CoreBluetooth Central - >外围设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!