使用CoreBluetooth(BLE)将数据从CentralManager发送到PeripheralManager

时间:2013-05-17 05:54:51

标签: ios objective-c bluetooth-lowenergy core-bluetooth

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

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

但是,我想将中心到外设的值写入特定的可写特性但总是失败。

立即调用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框架会自动添加它。不知道是否重要。

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

3 个答案:

答案 0 :(得分:4)

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

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

这就是我原来拥有的:

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

这应该是:

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

答案 1 :(得分:3)

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

不正确:

[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]; 

答案 2 :(得分: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];
            }
        }
    }