0

我参考了Apple为CoreBluetooth提供的示例应用程序,我成功地将数据从外围设备发送到中心,现在我需要将数据从中心写入外围设备。谷歌搜索后我发现它可以使用[_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>

如果有人给出获取外设对象的最佳方法以及如何启动从中心到外设的数据发送,那就太好了。

编辑 我已经尝试过如何从 Objective-C 中的 UUID 中获取特征?

但是,在这种情况下,每当我尝试这样做时,我都无法循环服务它返回 services=nil。

4

0 回答 0