iOS BLE 分包发送

单次发送的数据过大,蓝牙模块内部接收缓冲区只有20个字节

BLE_SEND_MAX_LEN是蓝牙单次可处理最大字节长度

//分包发送蓝牙数据
-(void)sendMsgWithSubPackage:(NSData*)msgData 
                  Peripheral:(CBPeripheral*)peripheral
              Characteristic:(CBCharacteristic*)character
{
    for (int i = 0; i < [msgData length]; i += BLE_SEND_MAX_LEN) {
        // 预加 最大包长度,如果依然小于总数据长度,可以取最大包数据大小
        if ((i + BLE_SEND_MAX_LEN) < [msgData length]) {
            NSString *rangeStr = [NSString stringWithFormat:@"%i,%i", i, BLE_SEND_MAX_LEN];
            NSData *subData = [msgData subdataWithRange:NSRangeFromString(rangeStr)];
            NSLog(@"%@",subData);
            [self writeCharacteristic:peripheral
                       characteristic:character
                                value:subData];
            //根据接收模块的处理能力做相应延时
            usleep(20 * 1000);
        }
        else {
            NSString *rangeStr = [NSString stringWithFormat:@"%i,%i", i, (int)([msgData length] - i)];
            NSData *subData = [msgData subdataWithRange:NSRangeFromString(rangeStr)];
            [self writeCharacteristic:peripheral
                       characteristic:character
                                value:subData];
            usleep(20 * 1000);
        }
    }
}

你可能感兴趣的:(iOS BLE 分包发送)