Rachel__^.^__ 2019-12-06 16:01 采纳率: 0%
浏览 414

iOS app 开发,怎么对ble特征描述,写入长数据。

  1. 问题描述: ios version 12 版本的手机,写 ble 特征描述,有长度限制(maxium: 182 byte)
  2. 尝试过的方法: 分包发数据,但是写入后再读取,发现数据是不完整的,是最后一包的内容
if (@available(iOS 13.0, *)) {
            [self.delegate bleDataWithData:textData];
        } else {
            NSInteger datacount = 160;
            NSInteger number = textData.length / datacount + ((textData.length % datacount)>0? 1:0);
            for(NSInteger i = 0; i < number; i++){
                if (i == number-1){
                    [self.delegate bleDataWithData:textData];
                } else {
                    NSData *data = [textData subdataWithRange:NSMakeRange(0, datacount)];
                    [self.delegate bleDataWithData:data];
                    textData = [textData subdataWithRange:NSMakeRange(datacount, textData.length-datacount)];
                }
            }
        }

求助大神们怎么实现写入超过长度的数据?

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-29 02:33
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    在Objective-C中,你可以使用BLECharacteristicWriteResponse来发送和接收BLE特征描述。以下是一个示例:

    // 创建一个 BLECharacteristicWriteRequest对象
    BLECharacteristicWriteRequest* request = [[BLECharacteristicWriteRequest alloc] init];
    
    // 设置写入的数据
    [request setCharacteristic:[BLECharacteristic characteristicForUUID:bleUUID]];
    [request setValue:[NSString stringWithFormat:@"%d", length] forKey:@"length"];
    
    // 发送请求
    BLEConnectionManager* manager = [BLEConnectionManager sharedConnectionManager];
    [manager writeCharacteristic:request completionHandler:^(NSError* error) {
        // 处理响应
    }];
    

    在这个例子中,我们首先创建了一个BLECharacteristicWriteRequest对象,并设置了我们要写的特性和长度。然后,我们将这个请求发送给蓝牙连接管理器。

    请注意,你需要根据你的设备和应用程序的具体需求来调整这些参数。例如,你可能需要将setValue:中的值更改为实际的长度,或者在completionHandler中处理响应。

    评论

报告相同问题?