zoukankan      html  css  js  c++  java
  • 蓝牙- CoreBluetooth

    /**

     *  CoreBluetooth  ----  BLE

     *

     *  @ 蓝牙4.0 + iBeacon  --- 室内导航(超市)

     */

    #import "ViewController.h"

    #import <CoreBluetooth/CoreBluetooth.h>

    @interface ViewController ()<CBCentralManagerDelegate, CBPeripheralDelegate>

     /**  中心管理者 ***/

    @property (nonatomic, strong) CBCentralManager *mgr;

     /**  保存外围设备 ***/

    @property (nonatomic, strong) NSMutableArray *peripherals;

    @end

    @implementation ViewController

    - (NSMutableArray *)peripherals{

        if (!_peripherals) {

            _peripherals = [[NSMutableArray alloc] init];

        }

        return _peripherals;

    }

    - (void)viewDidLoad {

        [super viewDidLoad];

       

        // 1. 创建中心设备

        self.mgr = [[CBCentralManager alloc] init];

        

        // 设置代理

        self.mgr.delegate = self;

        

        // 2. 扫描外围设备

        /**

         *  如果制定数组 代表只扫描指定的设备

         */

        [self.mgr scanForPeripheralsWithServices:nil options:nil];

        

        

        

        

    }

    #pragma mark - 3. CBCentralManagerDelegate

    - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI

    {

        // 保存扫描到的外部设备

        // 如果数组中不包含扫描到的设备, 添加设备到数组中

        if (![self.peripherals containsObject:peripheral]) {

            // 设置外设代理

            peripheral.delegate = self;

            

            [self.peripherals addObject:peripheral];

        }

    }

    /**

     *  模拟点击, 然后连接所有外围设备

     */

    - (void)start

    {

        for (CBPeripheral *peripheral in self.peripherals) {

            // 连接到外围设备

            [self.mgr connectPeripheral:peripheral options:nil];

        }

    }

    /**

     *  4. 连接外设成功

     */

    - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral

    {

        // 扫描外设中的服务(service) nil --- 扫描所有服务

        [peripheral discoverServices:nil];

    }

    /**

     *  连接外设失败

     */

    - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error

    {

        LogGreen(@"连接外设失败");

    }

    - (void)centralManagerDidUpdateState:(CBCentralManager *)central

    {

        

    }

    #pragma mark - CBPeripheralDelegate

    /**

     *  只要扫描到外设的服务就会调用

     *

     *  @param peripheral 服务所属的外设

     *  @param error      错误

     */

    - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error

    {

        // 获取外设中所有扫描的服务

        NSArray *services = peripheral.services;

        

        for (CBService *service in services) {

            // 过滤不需要的服务  --- 拿到需要的服务

            if([service.UUID.UUIDString isEqualToString:@""]){

                // 从需要的服务中查找需要的特征

                // 从peripheral中得到的service中, 扫描需要的特征

                [peripheral discoverCharacteristics:nil forService:service];

            }

            

        }

    }

    /**

     *  只要扫描到特征就会调用

     *

     *  @param peripheral 特征所属的外设

     *  @param service    特征所属的服务

     */

    - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error

    {

        // 拿到服务中 所有的特征

        NSArray *characters = service.characteristics;

        // 遍历特征, 拿到需要的特征处理

        for (CBCharacteristic *character in characters) {

            if ([character.UUID.UUIDString isEqualToString:@"wakeup"]) {

                LogRed(@"设置闹钟");

                

                // 停止

    //            [self.mgr stopScan];

            }

        }

    }

  • 相关阅读:
    Java集合源码分析(一)
    EffectiveJava——请不要在代码中使用原生态类型
    Dubbo初探
    EffectiveJava——用函数对象表示策略
    EffectiveJava——类层次优于标签类
    notebook1.md
    NoteBook学习(二)-------- Zeppelin简介与安装
    Spark2.0学习(三)--------核心API
    Spark2.0学习(二)--------RDD详解
    Spark2.0学习(一)--------Spark简介
  • 原文地址:https://www.cnblogs.com/guangleijia/p/4835859.html
Copyright © 2011-2022 走看看