我提到Apple为CoreBluetooth提供的Sample app,我成功地将数据从外设发送到Central,现在我需要将数据从Central写入外设。谷歌搜索后,我发现可以使用[_discoveredPeripheral writeValue:aData forCharacteristic:charc type:CBCharacteristicWriteWithResponse];
以下是我的Central实现,向外设发送消息:
-(void)sendMessage:(NSString *)strMessage{
NSData *aData = [strMessage dataUsingEncoding:NSUTF8StringEncoding];
CBMutableCharacteristic *charc =[[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID] properties:CBCharacteristicPropertyWrite value:nil permissions: CBAttributePermissionsWriteable];
[_discoveredPeripheral writeValue:aData forCharacteristic:charc type:CBCharacteristicWriteWithResponse];
}
当我调用此方法时,它无法写入数据,而不是我可以在控制台上看到CoreBluetooth警告,如下所示,
CoreBluetooth[WARNING] <CBMutableCharacteristic: 0x15e8e420 UUID = 08590F7E-DB05-467E-8757-72F6FAEB13D4, Value = (null), Properties = 0x8, Permissions = 0x2, Descriptors = (null), SubscribedCentrals = (
)> is not a valid characteristic for peripheral <CBPeripheral: 0x15d91250 identifier = 37C314AF-FDB3-1F24-6937-8780B97AAB45, Name = "iPad", state = disconnected>
如果有人提供了获取外围设备的最佳方法以及如何启动从中心到外设的数据发送,那将是很好的。
修改 我尝试过How to get the characteristic from UUID in objective-C?
中显示的方式但是,在这种情况下,每当我尝试返回服务= nil时,我都无法循环服务。