相关疑难解决方法(0)

使用CoreBluetooth读取长特征值

我有一个包含图像数据的特征值.在外围设备中我设置了如下值:

_photoUUID = [CBUUID UUIDWithString:bPhotoCharacteristicUUID];
_photoCharacteristic = [[CBMutableCharacteristic alloc] initWithType:_photoUUID
                                                          properties:CBCharacteristicPropertyRead
                                                               value:Nil
                                                         permissions:CBAttributePermissionsReadable];
Run Code Online (Sandbox Code Playgroud)

我的理解是,当请求此值时,将调用didRecieveRequest回调:

-(void) peripheralManager:(CBPeripheralManager *)peripheral didReceiveReadRequest:(CBATTRequest *)request {

    if ([request.characteristic.UUID isEqual:_photoUUID]) {
        if (request.offset > request.characteristic.value.length) {
            [_peripheralManager respondToRequest:request withResult:CBATTErrorInvalidOffset];
            return;
        }
        else {
            // Get the photos
            if (request.offset == 0) {
                _photoData = [NSKeyedArchiver archivedDataWithRootObject:_myProfile.photosImmutable];
            }

            request.value = [_photoData subdataWithRange:NSMakeRange(request.offset, request.characteristic.value.length - request.offset)];
            [_peripheralManager respondToRequest:request withResult:CBATTErrorSuccess];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这几乎来自Apple的文档.在didDiscoverCharacteristic回调的中央一侧,我有以下代码:

if ([characteristic.UUID isEqual:_photoUUID]) {
    _photoCharacteristic = characteristic;
    [peripheral readValueForCharacteristic:characteristic];
}
Run Code Online (Sandbox Code Playgroud)

而这反过来调用didUpdateValueCorCharacteristic回调:

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic …
Run Code Online (Sandbox Code Playgroud)

bluetooth objective-c ios core-bluetooth

14
推荐指数
1
解决办法
1万
查看次数

标签 统计

bluetooth ×1

core-bluetooth ×1

ios ×1

objective-c ×1