美文网首页
iOS蓝牙开发-2-按协议开发

iOS蓝牙开发-2-按协议开发

作者: ___吉 | 来源:发表于2018-09-06 20:36 被阅读0次

    一般来说和蓝牙外设硬件交互都会有简单的协议,一是规范通信,二是对数据加密。
    我公司的协议都是由算法定义的,算法会给到一份详细的协议文档,上面会详细的写清楚数据解析及交互。


    协议结构.png

    例如:读取电量功能


    读取电量.png
    因为涉及到公司产品,所以打码。

    按上面的协议结构来:
    假如帧头是0x01,功能码是0x02,校验和是0xxx(具体如何计算,公司内部确定)
    APP 发送给 蓝牙外设的数据(读取电量指令)应为:

    0x01 0x02 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0xxx(校验码)
    

    蓝牙外设收到读取电量指令后,返回电量数据应为:

    0x01 0x02 0x5A 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0xxx(校验码)
    

    注意,第三个字节为电量值,解析代码:

    const u_int8_t *bytes = [data bytes];
    NSInteger powerValue = bytes[2];
    //得到的 powerValue 即为电量值,接下来写个if判断就好了
    if (powerValue == 120) {
            //充电中
        }else{
            //显示电量即可
    }
    

    我自己做的项目中,CBCentralManager、CBPeripheral都是有单独写了一个类


    单独的类.png

    WSBLECenterManager:搜索外设、连接设备、自动重连等等逻辑,内含:

    /**
     蓝牙状态更新
     
     @param central central
     */
    - (void)centralManagerDidUpdateState:(CBCentralManager *)central{
    }
    
    /**
     发现设备
     
     @param central central
     @param peripheral 外设
     @param advertisementData 广播
     @param RSSI 信号值
     */
    - (void)centralManager:(CBCentralManager *)central
     didDiscoverPeripheral:(CBPeripheral *)peripheral
         advertisementData:(NSDictionary *)advertisementData
                      RSSI:(NSNumber *)RSSI{
    //找到自己需要的设备后,可在此处连接,调用以下方法
      [_myCentralManager connectPeripheral:peripheral options:nil];
    }
    
    /**
     连接成功
    
     @param central central
     @param peripheral 外设
     */
    - (void)centralManager:(CBCentralManager *)central
      didConnectPeripheral:(CBPeripheral *)peripheral{
    }
    
    /**
     连接失败
    
     @param central central
     @param peripheral 外设
     @param error error
     */
    - (void)centralManager:(CBCentralManager *)central
    didFailToConnectPeripheral:(CBPeripheral *)peripheral
                     error:(NSError *)error{
    }
    
    /**
     断开连接
    
     @param central central
     @param peripheral 外设
     @param error error
     */
    - (void)centralManager:(CBCentralManager *)central 
    didDisconnectPeripheral:(CBPeripheral *)peripheral 
                     error:(NSError *)error{
    }
    
    

    WSBLESensor:发送指令、收取数据、解析数据等等功能
    内含:

    /**
     发现服务
    
     @param peripheral peripheral
     @param error error
     */
    -(void)peripheral:(CBPeripheral *)peripheral
    didDiscoverServices:(NSError *)error{
    }
    
    /**
     发现特征
    
     @param peripheral peripheral
     @param service service
     @param error error
     */
    - (void)peripheral:(CBPeripheral *)peripheral
    didDiscoverCharacteristicsForService:(CBService *)service
                 error:(NSError *)error {
    }
    
    /**
     收到数据
    
     @param peripheral peripheral
     @param characteristic characteristic
     @param error error
     */
    -(void)peripheral:(CBPeripheral *)peripheral
    didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic
                error:(NSError *)error{
    }
    
    //发送数据到外设,调用
    [self.peripheral writeValue:data
              forCharacteristic:self.writeCharacteristic
                           type:CBCharacteristicWriteWithResponse];
    

    上面发送读取电量指令时,如下:

        Byte bytes[20];
        bytes[0] = (Byte)(1);//帧头
        bytes[1] = (Byte)(2);//读取电量的功能码
        for (NSInteger i = 2; i < 19; i++) {
            bytes[i] = (Byte)(0);
        }
        bytes[19] = (Byte)(/*校验和*/);
        NSData *data = [NSData dataWithBytes:bytes length:sizeof(bytes)];
        [self.peripheral writeValue:data
                  forCharacteristic:self.writeCharacteristic
                               type:CBCharacteristicWriteWithResponse];
    

    我的设计蓝牙设计思路:

    WSBLECenterManager:
      包含CBCentralManager单例对象:  _myCentralManager
      设置代理、实现CBCentralManagerDelegate协议及其方法。这样搜索到的设备都能在WSBLECenterManager获取到,同时连接成功、连接失败、断开连接等等也都可以通过 CBCentralManagerDelegate 协议中的代理方法知晓
    如果是需要连接设备,我的做法是在 WSBLECenterManager 中定义了一个可变数组,用于存储连接上的设备。
      查找到的设备、连接设备是否成功、断开设备的提示等都是通过block或者代理回调到viewController中
    
    WSBLESensor:
      这是外设模型,存储了广播数据,信号值等等
      CBPeripheralDelegate 协议中的代理方法都在这个类中实现。查找服务、查找特征、收取数据、解析数据、发送数据都在这个类中实现
      数据处理结果、数据发送结果等都是通过block或者代理回调到viewController中
    
    以上,即可将CBCentralManager、CBPeripheral拆分开来
    其他的就是一些枚举、block等定义了,可以单独写一个.h文件
    

    相关文章

      网友评论

          本文标题:iOS蓝牙开发-2-按协议开发

          本文链接:https://www.haomeiwen.com/subject/jqkihxtx.html