zoukankan      html  css  js  c++  java
  • 蓝牙4.0实现及原理

    /*

     建⽴立中⼼心设备

     扫描外设(Discover Peripheral)

     连接外设(Connect Peripheral)

     扫描外设中的服务和特征(Discover Services And Characteristics)

     利⽤用特征与外设做数据交互(Explore And Interact)

     断开连接(Disconnect)

     */

     

    #import "ViewController.h"

    #import <CoreBluetooth/CoreBluetooth.h>

     

    @interface ViewController ()<CBCentralManagerDelegate,CBPeripheralDelegate>

    //所有扫描外设的列表

    @property(nonatomic,strong)NSMutableArray*peripherals;

    //创建蓝牙管理工具 中心设备

    @property(nonatomic,strong)CBCentralManager*manager;

    //服务信息(心率跳动次数)

    @property(nonatomic,strong)CBCharacteristic*characteristicHeart;

     

     

    @end

     

    @implementation ViewController

     

    -(CBCentralManager*)manager

    {

        if (!_manager) {

            _manager=[[CBCentralManager alloc]init];

        }

        return _manager;

    }

    -(NSMutableArray*)peripherals

    {

        if (!_peripherals) {

            _peripherals=[[NSMutableArray alloc]init];

        }

        return _peripherals;

    }

     

    - (void)viewDidLoad {

        [super viewDidLoad];

        //2.扫描外设

        //  可以扫描指定的服务或者特性 [数组里传UUID唯一标识

        [self.manager scanForPeripheralsWithServices:@[@"778899"] options:nil];

        //  可以扫描所有的设备

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

        //设置代理,获取扫描产生的设备

        self.manager.delegate=self;

        

        //3.连接外设 可以设置btn点击连接外设

        for (CBPeripheral *per in self.peripherals) {

            [self.manager connectPeripheral:per options:nil];

            //设置外设代理,用于信息交互

            per.delegate=self;

        }

        

        

        

        

        

    }

     

    #pragma mark CBCentralManagerDelegate

    //扫描外部设备就会调用

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

    {

        //peripheral 外设 如果存在就不添加

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

        {

            [self.peripherals addObject:peripheral];

        }

        

    }

    //4 一旦连接到外设 扫描服务信息

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

    {

        //扫描服务

        [peripheral discoverServices:@[@"123456"]];

    }

     

     

     

    //中心设备状态更新调用

    -(void)centralManagerDidUpdateState:(CBCentralManager *)central

    {

        NSLog(@"扑捉当前中心设备的状态!");

    }

     

    #pragma mark CBPeripheralDelegate

    //外设扫描到服务调用

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

    {

        for (CBService *service in peripheral.services) {

            [peripheral discoverCharacteristics:@[@"222222"] forService:service];

        }

    }

    //扫描到服务中的特性的时候调用

     

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

    {

        for (CBCharacteristic *character in service.characteristics) {

            

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

                self.characteristicHeart=character;

                NSLog(@"记录查到的特性");

            }

        }

    }

     

     

     

     

     

    -(void)dealloc

    {

        

    }

     

     

     

     

     

     

    @end

  • 相关阅读:
    [转]MYSQL5.7版本sql_mode=only_full_group_by问题
    [坑]Linux MySQL环境表名默认区分大小写
    [转]CentOS 7.3 安装MySQL
    [转]Oracle截取字符串相关函数
    服务相关
    CSRF攻击
    sqlalchemy——多表操作
    sqlalchemy——基本操作
    高可用——网站运行监控
    高可用——软件质量保证
  • 原文地址:https://www.cnblogs.com/tangranyang/p/4659531.html
Copyright © 2011-2022 走看看