博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS开发笔记--Core Bluetooth开发
阅读量:6914 次
发布时间:2019-06-27

本文共 2858 字,大约阅读时间需要 9 分钟。

推荐阅读文章:

一、前言

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年的使用寿命,而且是在设备不断对外发射信号的情况下;

 

转载于:https://www.cnblogs.com/ios4kerwin/p/5514830.html

你可能感兴趣的文章
Tomcat性能调优方案
查看>>
Ubuntu12.04上编译PlateGatewayQt
查看>>
(转)UITableView使用详解 相当详细,不错的东东
查看>>
Java中JDK,JRE和JVM之间的关系
查看>>
Python-NLTK环境搭建
查看>>
linux下的usb抓包方法【转】
查看>>
二叉搜索树转换成有序的双向链表
查看>>
内核定时器的使用(好几个例子add_timer)【转】
查看>>
linux中高端内存和低端内存的概念【转】
查看>>
软件整合--硬件整合--平台整合
查看>>
Android 将Activiyt作为Dialog弹出
查看>>
Android 抽屉类SlidingDrawer的使用
查看>>
Android][Android Studio] *.jar 与 *.aar 的生成与*.aar导入项目方法
查看>>
node-webkit教程(14)禁用缓存
查看>>
maven中properties标签定义变量
查看>>
Linux下路由配置梳理
查看>>
9.2. frame
查看>>
sql获取每门课程成绩最好的学生信息
查看>>
VS输出窗口(output view)的小技巧--文件行号字符定位
查看>>
14.4. Example
查看>>