• IOS蓝牙项目总结


    常见的蓝牙标准有2.0和4.0。

     特点
    2.0 1.适用于数据量比较大得传输,比如音乐、语音
    2.IOS开发中,要求设备是经过MFI认证
    4.0 1.适用于实时性比较高的数据传输,比如遥控类的鼠标、键盘,传感设备的心跳计、血压计
    2.功耗低,距离短,轻量级

    注意:
    一般我们说的蓝牙4.0都支持2.0和4.0,只是我们开发过程中只负责4.0标准的部分。

    相关
    地址1 地址2

    一、基本知识

    1.CoreBluetooth框架是ble4.0开发使用的框架
    2.CoreBluetooth主要内容有两个:peripheral(外设模式)和central(中心模式)。现在我写的是central内容。

    二、外设模式关键操作

    1.创建中心角色(centralManger)
    2.扫描外设(peripheral)
    注意:
    该步骤需确认centralManager.state==CBCentralManagerStatePoweredOn
    3.持有并连接外设(connect)
    4.扫描外设的服务(service)
    注意:该步骤需确认didConnectPeripheral方法调用成功
    5.扫描服务的特征(characteristic)
    注意:该步骤需要确认didDiscoverServices方法回调成功
    6.操作特征
    注意:该操作需确认didDiscoverCharacteristicsForService成功回调
    6.1.重新读取特征值(read)
    6.2.往特征里写入值(write)
    6.3.订阅特征(notifying)
    6.4.扫描描述(descriptor)
    7.断开连接,停止扫描

    三、代码

    #import "BLETool.h"
    #import <CoreBluetooth/CoreBluetooth.h>
    @interface BLETool()<CBCentralManagerDelegate,CBPeripheralDelegate>
    
    @property (strong, nonatomic) CBCentralManager *centralManager;
    @property (strong, nonatomic) CBPeripheral *peripheral;
    @end
    @implementation BLETool
    
    - (instancetype)init{
        if (self = [super init]) {
            //1.创建中心管理角色。
            /**
             queue为nil表示默认主线程
             */
            self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
        }
        return self;
    }
    
    /**
     *  --  必须实现的代理,用来返回创建的centralManager的状态。
     *  --  注意:必须确认当前是CBCentralManagerStatePoweredOn状态才可以调用扫描外设的方法:
     scanForPeripheralsWithServices
     */
    - (void)centralManagerDidUpdateState:(CBCentralManager *)central{
        switch (central.state) {
            case CBCentralManagerStateUnknown:
                NSLog(@">>>CBCentralManagerStateUnknown");
                break;
            case CBCentralManagerStateResetting:
                NSLog(@">>>CBCentralManagerStateResetting");
                break;
            case CBCentralManagerStateUnsupported:
                NSLog(@">>>CBCentralManagerStateUnsupported");
                break;
            case CBCentralManagerStateUnauthorized:
                NSLog(@">>>CBCentralManagerStateUnauthorized");
                break;
            case CBCentralManagerStatePoweredOff:
                NSLog(@">>>CBCentralManagerStatePoweredOff");
                break;
            case CBCentralManagerStatePoweredOn:
            {
                NSLog(@">>>CBCentralManagerStatePoweredOn");
                //2.开始扫描周围的外设。
                /*
                 -- 两个参数为Nil表示默认扫描所有可见蓝牙设备。
                 -- 注意:第一个参数我一开始以为是扫描指定外设的,那么使用外设的identifier就可以了。后来发现不是,这个参数是用来扫描有指定服务的外设。然后有些外设的服务是相同的,比如都有FFF5服务,那么都会发现;而有些外设的服务是不可见的,就会扫描不到设备。
                 -- 成功扫描到外设后调用didDiscoverPeripheral
                 */
                [self.centralManager scanForPeripheralsWithServices:nil options:nil];
        }
                break;
            default:
                break;
        }
    }
    
    #pragma mark 发现外设
    - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI{
    
        if ([peripheral.name isEqualToString:@"TimesBasy-BLE"]||[peripheral.name isEqualToString:@"TimesBaby-BLE"]) {
            //注意:想要对指定peripheral进行后续操作,一定要保存这个外设对象。否则centralManager会认为你对指定peripheral不感兴趣,这样你即不能再扫描到这个指定peripheral,也不能对他进行后续操作(比如回调didConnectPeripheral)
            self.peripheral = peripheral;
    
            //3.连接外设
            /**
             --     一个中心管理角色可以连接多个外设,但是一个外设只能被一个角色连接,外设被连接后就不能能再被扫描到
             --     连接成功回调didConnectPeripheral,失败回调didFailToConnectPeripheral,取消连接回调didDisconnectPeripheral
             */
            [self.centralManager connectPeripheral:peripheral options:nil];
        }
    }
    
    
    #pragma mark 连接外设--成功
    - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
        //连接成功后停止扫描,节省内存
        [central stopScan];
    
        peripheral.delegate = self;
        //4.扫描外设的服务
        /**
         --     外设的服务、特征、描述等方法是CBPeripheralDelegate的内容,所以要先设置代理peripheral.delegate = self
         --     参数表示你关心的服务的UUID,比如我关心的是"FFF0",参数就可以为@[[CBUUID UUIDWithString:@"FFF0"]].那么didDiscoverServices方法回调内容就只有这两个UUID的服务,不会有其他多余的内容,提高效率。nil表示扫描所有服务
         --     成功发现服务,回调didDiscoverServices
         */
        [peripheral discoverServices:nil];
    }
    
    #pragma mark 连接外设——失败
    - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
    
    }
    
    #pragma mark 取消与外设的连接回调
    - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
    }
    
    #pragma mark 发现服务回调
    - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
    
        for (CBService *service in peripheral.services) {
    
            //5.扫描指定服务的特征
            /**
             --     第一个参数是数组,表示扫描指定服务下那些我关心的特征的UUID。
             --     成功扫描到特征后回调didDiscoverCharacteristicsForService
             */
            [peripheral discoverCharacteristics:nil forService:service];
        }
    }
    
    #pragma mark 发现特征回调
    /**
    --  发现特征后,可以根据特征的properties进行:读readValueForCharacteristic、写writeValue、订阅通知setNotifyValue、扫描特征的描述discoverDescriptorsForCharacteristic。
    --  注意:实际开发中根据文档来确定对指定characteristic的操作。因为可能你得蓝牙设备的特征的properties不在下面之列。比如properties = 0x18
    
     说明:我注视的那些不叫重要,其他的我也不知道
     CBCharacteristicPropertyBroadcast                                                = 0x01,
     //允许读
     CBCharacteristicPropertyRead                                                    = 0x02,
     //允许写,但不会回应(不会调用didWriteValueForCharacteristic)
     CBCharacteristicPropertyWriteWithoutResponse                                    = 0x04,
     //允许写,但会回应
     CBCharacteristicPropertyWrite                                                    = 0x08,
     //允许通知
     CBCharacteristicPropertyNotify                                                    = 0x10,
     //允许通知。和上面分属两种不同的通知类型
     CBCharacteristicPropertyIndicate                                                = 0x20,
    
    
     CBCharacteristicPropertyAuthenticatedSignedWrites                                = 0x40,
     CBCharacteristicPropertyExtendedProperties                                        = 0x80,
     CBCharacteristicPropertyNotifyEncryptionRequired NS_ENUM_AVAILABLE(NA, 6_0)        = 0x100,
     CBCharacteristicPropertyIndicateEncryptionRequired
     */
    - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
        //一般开发时我们都应该有蓝牙文档的,需要读、写、订阅的特征都说说ing,所以直接根据对应的特征的UUID操作就可以,不用根据properties。
        for (CBCharacteristic *characteristic in service.characteristics) {
            if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"FFF0"]]) {
                //读。重新获取characteristic的值
                /**
                 -- 阅读文档查看需要对该特征的操作
                 -- 读取成功回调didUpdateValueForCharacteristic
                 */
                [peripheral readValueForCharacteristic:characteristic];
            }
    
            if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"FFF1"]]) {
                //写。
                /**
                 -- 阅读文档查看需要对该特征的操作
                 -- type:CBCharacteristicWriteWithResponse表示写入成功会回调didWriteValueForCharacteristic
                 */
                        NSData *data = [NSData new];
                [peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
            }
    
            if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"FFF5"]]) {
                //订阅通知
                /**
                 -- 通知一般在蓝牙设备状态变化时会发出,比如蓝牙音箱按了上一首或者调整了音量,如果这时候对应的特征被订阅,那么app就可能收到通知
                 -- 阅读文档查看需要对该特征的操作
                 -- 订阅成功后回调didUpdateNotificationStateForCharacteristic
                 -- 订阅后characteristic的值发生变化会发送通知到didUpdateValueForCharacteristic
                 -- 取消订阅:设置setNotifyValue为NO
                 */
                [peripheral setNotifyValue:YES forCharacteristic:characteristic];
            }
    
            //扫描描述
            /**
             -- 进一步提供characteristic的值的相关信息。(因为我项目里没有的特征没有进一步描述,所以我也不怎么理解)
             -- 当发现characteristic有descriptor,回调didDiscoverDescriptorsForCharacteristic
             */
            [peripheral discoverDescriptorsForCharacteristic:characteristic];
    
        }
    }
    
    #pragma mark 数据写入特征回调
    - (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
    }
    
    #pragma mark 获取特征的值
    /**
     -- peripheral调用readValueForCharacteristic成功后会回调该方法
     -- peripheral调用setNotifyValue后,特征发出通知也会调用该方法
     */
    
    - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
    }
    
    #pragma mark 订阅通知回调
    - (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
    
    }
    
    #pragma mark 发现descriptors回调
    - (void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
    }
    
    #pragma mark 断开连接
    - (void)disConnectPeripheral{
        /**
         -- 断开连接后回调didDisconnectPeripheral
         -- 注意断开后如果要重新扫描这个外设,需要重新调用[self.centralManager scanForPeripheralsWithServices:nil options:nil];
         */
        [self.centralManager cancelPeripheralConnection:self.peripheral];
    }
    
    #pragma mark 停止扫描外设
    - (void)stopScanPeripheral{
        [self.centralManager stopScan];
    }
    @end
  • 相关阅读:
    gcc编译代码报错及编译方式
    YUV到RGB的转换
    YUV和RGB格式分析
    v4l2的学习建议和流程解析
    在Ubuntu下安装imx6linux系统的交叉编译环境遇到的问题总结
    《赢在测试2》--读书笔记
    关于javascript的slice方法
    数字的千分位格式化方法
    javascript开发中的封装模式(转)
    防止表单重复提交的几种方法
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/6513557.html
Copyright © 2020-2023  润新知