我对蓝牙通讯很陌生。我的第一个项目打算将数据从iOS设备传输到BLEshield (小型芯片)。
为了测试我的中央代码,我决定将一个iPhone设置为外围设备(一旦我得到它,芯片将扮演这个角色),并将一个iPad设置为Central。
我可以连接设备,也可以将数据从外设发送到中央。不过,这很容易:
- (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];
}
但我不能让另一个方向起作用。要从中央端发送数据,我有以下代码:
[_activePeripheral writeValue:[@"PONG" dataUsingEncoding:NSUTF8StringEncoding] forCharacteristic:_writeChar type:CBCharacteristicWriteWithoutResponse];
我假设这两种方法中的任何一种都应该在外围调用:
- (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
但实际上什么都没发生。不幸的是,我的硬件项目将使用只能在外围模式工作的芯片,最后我将几乎完全写到外围设备上,因为它是控制信号的发送器。
我希望有人能帮我!
发布于 2013-11-17 21:55:40
以下各项的物业:
_readChar
应该是CBCharacteristicPropertyRead
_writeChar
应该是CBCharacteristicPropertyWriteWithoutResponse
有关更多详细信息,请参阅here。CBCharacteristicPropertyNotify
不允许特征是可写的或可读的,它只是应该通知的(订阅/取消订阅)。
发布于 2014-02-15 10:18:27
只需使用https://github.com/LGBluetooth/LGBluetooth,它会使生活更轻松
https://stackoverflow.com/questions/20034433
复制相似问题