zoukankan      html  css  js  c++  java
  • 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);
            }
        }
    }

    转载自:http://www.jianshu.com/p/29bd630077b4

  • 相关阅读:
    流形学习(Mainfold Learning)
    陈皓的博客
    背包九讲
    阮一峰的个人网站
    PyTorch教程【六】Transforms的使用
    PyCharm设置代码提示忽略大小写
    PyTorch教程【五】TensoBoard的使用
    pip 换源
    PyCharm常用快捷键
    JAVA基础篇—HashMap
  • 原文地址:https://www.cnblogs.com/sunmair/p/7281517.html
Copyright © 2011-2022 走看看