CoreBlueTooth:即使将数据写入可写特征也会出错

时间:2013-07-24 23:01:36

标签: ios objective-c cocoa-touch core-bluetooth cbcentralmanager

我正在使用CoreBlueTooth框架写入Peripheral的一个可写特性。我在中心实现“didWriteValueForCharacteristic:error:”委托,它总是让我失误。虽然我收到了我的外设数据。

Error Domain=CBErrorDomain Code=0 "Unknown error." UserInfo=0x166762e0 {NSLocalizedDescription=Unknown error.}

在我的代码中,我的self.data是一个带有3个键和值的NSDictionary。

// Central

- (void)centralManagerDidUpdateState:(CBCentralManager *)iCentral {
    if (iCentral.state != CBCentralManagerStatePoweredOn) {
        return;
    }

    [self.centralManager scanForPeripheralsWithServices:self.peripheralServices options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES}];
}


- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)iPeripheral advertisementData:(NSDictionary *)iAdvertisementData RSSI:(NSNumber *)iRSSI {
    if (self.discoveredPeripheral != iPeripheral) {
        // Save a local copy of the peripheral, so CoreBluetooth doesn't get rid of it
        self.discoveredPeripheral = iPeripheral;

        // Connect to the discovered peripheral
        [self.centralManager connectPeripheral:iPeripheral options:nil];
    }
}

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)iPeripheral advertisementData:(NSDictionary *)iAdvertisementData RSSI:(NSNumber *)iRSSI {
    if (self.discoveredPeripheral != iPeripheral) {
        // Save a local copy of the peripheral, so CoreBluetooth doesn't get rid of it
        self.discoveredPeripheral = iPeripheral;

        // Connect to the discovered peripheral
        [self.centralManager connectPeripheral:iPeripheral options:nil];
    }
}


// We've connected to the peripheral, now we need to discover the services and characteristics to find the 'writeable' characteristic.
- (void)centralManager:(CBCentralManager *)iCentral didConnectPeripheral:(CBPeripheral *)iPeripheral {
    // Stop scanning
    [self.centralManager stopScan];

    // Make sure we get the discovery callbacks
    iPeripheral.delegate = self;

    // Search only for services that match our UUID
    [iPeripheral discoverServices:self.peripheralServices];
}


- (void)peripheral:(CBPeripheral *)iPeripheral didDiscoverServices:(NSError *)iError {
    if (iError) {
        [self cleanup];
        return;
    }

    // Loop through the newly filled peripheral.services array, just in case there's more than one.
    for (CBService *service in iPeripheral.services) {
        [iPeripheral discoverCharacteristics:@[self.writeableCharactersticsUUID] forService:service];
    }
}


// Write the data into peripheral's characterstics
- (void)peripheral:(CBPeripheral *)iPeripheral didDiscoverCharacteristicsForService:(CBService *)iService error:(NSError *)iError {
    if (iError) {
        [self cleanup];

        return;
    }

    // Find out the writable characterstics
    for (CBCharacteristic *characteristic in iService.characteristics) {
        if ([characteristic.UUID isEqual:self.writeableCharactersticsUUID]) {
            NSData *dataToWrite = [NSJSONSerialization dataWithJSONObject:self.data options:0 error:nil];
            NSInteger dataSize = [[NSByteCountFormatter stringFromByteCount:dataToWrite.length countStyle:NSByteCountFormatterCountStyleFile] integerValue];
            if (dataSize > 130) {
                NSLog(@"Cannot send more than 130 bytes");
                return;
            }

            [self.discoveredPeripheral writeValue:dataToWrite forCharacteristic:self.centralWriteableCharacteristic type:CBCharacteristicWriteWithResponse];

            break;
        }
    }
}


- (void)peripheral:(CBPeripheral *)iPeripheral didWriteValueForCharacteristic:(CBCharacteristic *)iCharacteristic error:(NSError *)iError {
    NSLog(@"Error = %@", iError);
}


- (void)cleanup {
    // Don't do anything if we're not connected
    if (self.discoveredPeripheral.state != CBPeripheralStateConnected) {
        return;
    }

    // If we've got this far, we're connected, but we're not subscribed, so we just disconnect
    [self.centralManager cancelPeripheralConnection:self.discoveredPeripheral];
}


// Peripheral

- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)iPeripheral {
    if (iPeripheral.state != CBPeripheralManagerStatePoweredOn) {
        return;
    }

    CBMutableCharacteristic *characteristic = [[CBMutableCharacteristic alloc] initWithType:iCID properties:CBCharacteristicPropertyWrite value:nil permissions:CBAttributePermissionsWriteable];

    CBMutableService *writableService = [[CBMutableService alloc] initWithType:iServiceId primary:YES];
    writableService.characteristics = @[characteristic];

    //[self.peripheralManager removeAllServices];
    [self.peripheralManager addService:writableService];
    [self.peripheralManager startAdvertising:@{ CBAdvertisementDataServiceUUIDsKey : @[iServiceId]}];
}

- (void)peripheralManager:(CBPeripheralManager *)iPeripheral didReceiveWriteRequests:(NSArray *)iRequests {
    CBATTRequest *aRequest = iRequests[0];
    NSData *aData = aRequest.value;
    NSDictionary *aResponse = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:aData options:NSJSONReadingMutableContainers error:nil];

    NSLog(@"Received Data = %@", aResponse);
}

3 个答案:

答案 0 :(得分:9)

我已经弄清楚了。问题在于特征类型。我使用了“CBCharacteristicWriteWithoutResponse”而不是“CBCharacteristicWriteWithResponse”而且它有效。

我读完后就这样做了:

writeValue forCharacteristic writeType,此函数是写入设备特性的主要功能。 writeType属性设置为write而没有响应或写入响应。当使用带有响应的写入时,在iOS设备等待接收ok响应和回调时,将缓存对外围设备的所有写入。写入时不使用响应,不会缓存数据。这对于使用需要低延迟的东西很重要,比如RC车或直升机等。当使用带响应的写入时,iOS设备可能有时会落后,这不会产生很大的响应......对于每次写入,都会调用didWriteCharacteristic回调。 / p>

答案 1 :(得分:4)

为后代录制此内容:您必须做出响应以防止出现错误:

- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray *)requests
{
    // respond!
    [peripheral respondToRequest:[requests objectAtIndex:0] withResult:CBATTErrorSuccess];

答案 2 :(得分:2)

将此留给其他人,但OP的答案是不正确的。

这里犯的错误是他没有在这个功能中更新他的特征:

- (void)peripheralManager:(CBPeripheralManager *)iPeripheral didReceiveWriteRequests:(NSArray *)iRequests {
    CBATTRequest *aRequest = iRequests[0];
    NSData *aData = aRequest.value;
    NSDictionary *aResponse = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:aData options:NSJSONReadingMutableContainers error:nil];


    NSLog(@"Received Data = %@", aResponse);
}

由于未发现任何特性更新,操作系统会假设出现问题并产生错误。

CBMutableCharacteristic *characteristic = [[CBMutableCharacteristic alloc] initWithType:iCID properties:CBCharacteristicPropertyWrite value:nil permissions:CBAttributePermissionsWriteable];

此代码实际上设置为具有响应的特征可写,指定无响应的枚举是:

CBCharacteristicPropertyWriteWithoutResponse

希望这有助于其他偶然发现此事的人。