zoukankan      html  css  js  c++  java
  • iOS开发笔记--Core Bluetooth开发

    推荐阅读文章:http://blog.csdn.net/pony_maggie/article/details/26740237

    一、前言

    CoreBluetooth框架的核心其实是两个东西,peripheral和central, 可以理解成外设和中心。对应他们分别有一组相关的API和类,如下图所示:

    二、Core Bluetooth 的基本常识:
    1、 每个蓝牙设备都是通过服务和特征来展示自己
    一个设备必然包含一个或多给服务,每个服务下面又包含多个特征
    2、特征是与外界交互的最小单位
    比如说,一台蓝牙设备,用特征A来描述自己的出场信息,用特征B来描述自己的收发数据
    3、服务和特征都是用UUID来唯一标识的,通过UUID就能区别不同的服务和特征
    4、设备里面的服务和特征的功能,都是由蓝牙设备硬件厂商提供,比如哪些用来交互(读写),哪些用来可获取模块信息(只读)等。

    三、Core Bluetooth的开发步骤:

    1、建立中心设备
    2、扫描外设(Discover Peripheral)
    3、连接外设(connect Peripheral)
    4、扫描外设中的服务于特征(Discover Services and Characteristics)
    5、利用特征与外设做数据交互(Explore and interact)
    6、断开连接(Disconnect)

    建立中心设备:

    //1、创建中心设备

        CBCentralManager *mgr = [[CBCentralManager alloc] init];

        //设置代理

        mgr.delegate = self;

        //2、利用中心设备扫描外部设备

        /*

         如果指定数据NSArray 只能扫描指定的设备

         */

        [mgr scanForPeripheralsWithServices:nil options:nil];

        self.mgr = mgr;

    #pragma mark  - CBCentralManagerDelegate

    //实现代理,扫描到外设,保存外设

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

    {

       //保留扫描到的外设

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

        {

            [self.peripherals addObject:peripheral];

        }

    }

    /**

     *  模拟点击连接所有的外设 连接所有外设

     */

    - (void)start

    {

        for (CBPeripheral *peripheral in self.peripherals) {

            peripheral.delegate = self;

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

        }

    }

     

    /**

     *  连接外设成功

     *

     *  @param central

     *  @param peripheral

     */

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

    {

        //1、扫描外设中的服务(扫描所有服务)

        [peripheral discoverServices:nil];

        

    }

     

    /**

     *  连接外设失败

     *

     *  @param central

     *  @param peripheral

     *  @param error

     */

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

    {

    }

     

    #pragma mark - CBPeripheralDelegate

    /**

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

     *

     *  @param peripheral peripheral

     *  @param error      error

     */

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

    {

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

        NSArray *services = peripheral.services;

        for (CBService *service in services)

        {

            //可以过滤不需要的服务

            if ([service.UUID.UUIDString isEqualToString:@"989589595995"]) return;

            

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

            //peripheral中的service中扫描特征

            [peripheral discoverCharacteristics:nil forService:service];

        }

        

    }

     

    /**

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

     *

     *  @param peripheral 外设

     *  @param service    服务

     *  @param error      错误信息

     */

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

    {

        NSArray *characteristics = service.characteristics;

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

        

        for (CBCharacteristic *characteristic in characteristics)

        {

            if ([characteristic.UUID.UUIDString isEqualToString:@"9996996969"])

            {

                

            }

        }

    }

     

    四、蓝牙的现状

    1、绝大多数只能手机支持蓝牙4.0(BLE)

    2、蓝牙芯片发展迅速,在性能和效率方面都是有很大的提高,且不断变得更小更便宜

    3、iBeacon + 蓝牙(BLE),前途一片光明

         应用之一:室内导航

        Estimote公司为iBeacon提供基站

        3个Estimote公司为iBeacon预购价格为99美元

        Estimote公司推出的iBeacon基站的最远出书距离为50m,最佳距离是10m以内

        一块纽扣电池就能为一个iBeacon基站提供长达2年的使用寿命,而且是在设备不断对外发射信号的情况下;

  • 相关阅读:
    Spring IOC容器的初始化-(二)BeanDefinition的载入和解析
    Spring IOC容器的初始化—(一)Resource定位
    SpringMVC的启动过程
    Spring IOC容器在Web容器中是怎样启动的
    Cannot retrieve metalink for repository: epel/x86_64. Please verify its path and try again 问题分析
    网络丢包分析
    yum安装jdk如何配置JAVA_HOME
    Linux网卡绑定
    Linux Buffer/Cache 的区别
    理解load averages
  • 原文地址:https://www.cnblogs.com/ios4kerwin/p/5514830.html
Copyright © 2011-2022 走看看