3

我有我的应用程序在两台 iOS 设备(都有 6.1.4)上运行,其中一台设备充当中央设备,一台设备充当外围设备。

我已经成功地从外围设备获取通知(设置为通知的特征)到中央就好了。

但是,我想将一个从 Central 到 Peripheral 的值写入特定的可写特征,但这总是失败。

委托的peripheral:didWriteValueForCharacteristic:error:方法立即被调用,错误描述如下:

“一个或多个参数无效”

我一直在网上搜索任何线索,但尚未提出问题所在。

这就是我在外围设备端设置特征的方式:

[[CBMutableCharacteristic alloc] initWithType:<My CBUUID> properties:CBCharacteristicPropertyWrite value:nil permissions:0];

我发现,当我发现中央一侧的特征时,我的可写特征就在那里。我确实在那时存储了一个参考,我稍后在编写我的值时尝试使用它。我正在编写的值只是一个文本字符串,我将其转换为 NSData,如下所示:

NSString *payloadMessage = @"Hello";
NSData *payload = [payloadMessage dataUsingEncoding:NSUTF8StringEncoding];
[peripheral writeValue:payload forCharacteristic:<myCharacteristic> type:CBCharacteristicWriteWithResponse];

我能看到的唯一奇怪的事情是我的特征属性设置为 138。看起来它既可写又具有扩展属性。不知道为什么 CoreBluetooth 框架会自动添加它。不知道有没有关系。

我试图发送的特征或数据似乎有问题,但对于我的生活,我无法弄清楚。如果有人有任何建议或想法,我将不胜感激

4

3 回答 3

4

我确实在 Bluetooh-dev 列表中收到了解决我问题的答案。

我没有使用正确的权限创建我的特征。

这是我最初拥有的:

[[CBMutableCharacteristic alloc] initWithType:<CBUUID> properties:CBCharacteristicPropertyWrite value:nil permissions:0];

这应该是:

[[CBMutableCharacteristic alloc] initWithType:<CBUUID> properties:CBCharacteristicPropertyWrite value:nil permissions: **CBAttributePermissionsWriteable**];  
于 2013-05-17T15:58:59.410 回答
3

当我不小心在 discoverCharacteristics:forService: 中提供了一个字符串数组而不是 CBUUID 时,我得到了同样的错误。

不正确:

[peripheral discoverCharacteristics:@[NOTIFY_CHARACTERISTIC_UUID, WRITE_WO_RESPONSE_CHARACTERISTIC_UUID]
                         forService:service];

正确的:

[peripheral discoverCharacteristics:@[[CBUUID UUIDWithString:NOTIFY_CHARACTERISTIC_UUID],
                                      [CBUUID UUIDWithString:WRITE_WO_RESPONSE_CHARACTERISTIC_UUID]]
                         forService:service]; 
于 2013-09-17T09:29:58.050 回答
0
  1. 创造特色

    self.writeableCharacteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:CBUUIDWriteableCharacteristicCodeString] properties:CBCharacteristicPropertyWrite value:nil permissions:CBAttributePermissionsWriteable];
    
  2. 将其添加到服务和 PeripheralManager

    CBMutableService *gameTransferService = [[CBMutableService alloc] initWithType:[CBUUID UUIDWithString:CBUUIDServiceCodeString] primary:YES];
    gameTransferService.characteristics = @[self.notifierCharacteristic, self.writeableCharacteristic];
    [self.peripheralRoleManager addService:gameTransferService];
    
  3. 确保您已连接到另一台设备

    [self.service connectToPerephiral:self.dataSource[indexPath.row]];
    
  4. 在连接的外围设备中找到 u 特性并写入数据

    for(CBService *service in self.discoveredPeripheral.services) {
            if([service.UUID isEqual:[CBUUID UUIDWithString:<CBUUIDServiceCodeString>]]) {
                for(CBCharacteristic *charac in service.characteristics) {
                    if([charac.UUID isEqual:[CBUUID UUIDWithString:<CBUUIDWriteableCharacteristicCodeString>]]) {
                        [self.discoveredPeripheral writeValue:data forCharacteristic:charac type:CBCharacteristicWriteWithResponse];
                }
            }
        }
    }
    
  5. 添加委托方法- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray<CBATTRequest *> *)requests,发现你的特征像

    for (CBATTRequest *request in requests) {
        if ([request.characteristic.UUID isEqual:[CBUUID UUIDWithString:CBUUIDNotifierCharacteristicCodeString]]) {
    #ifdef DEBUG
            NSString *dataString = [[NSString alloc] initWithData:request.value encoding:NSUTF8StringEncoding];
            NSLog(@"Received from manager - %@", dataString);
    #endif
            if (self.delegate && [self.delegate respondsToSelector:@selector(BLEServiceDidReceiveData:peripheral:service:)]) {
                [self.delegate BLEServiceDidReceiveData:request.value peripheral:nil service:self];
            }
        }
    }
    
于 2015-10-30T08:48:50.090 回答