iOS 蓝牙技术

蓝牙常见名称和缩写

  • MFI ======= make for ipad ,iphone, itouch 专们为苹果设备制作的设备
  • BLE ==== buletouch low energy,蓝牙4.0设备因为低耗电,所以也叫做BLE
  • peripheral,central == 外设和中心,发起连接的时central,被连接的设备为perilheral
  • service and characteristic === 服务和特征 每个设备会提供服务和特征,类似于服务端的api,但是机构不同。每个外设会有很多服务,每个服务中包含很多字段,这些字段的权限一般分为 读read,写write,通知notiy几种,就是我们连接设备后具体需要操作的内容。
  • Description 每个characteristic可以对应一个或多个Description用户描述characteristic的信息或属性
    MFI === 开发使用ExternalAccessory 框架
    4.0 BLE === 开发使用CoreBluetooth 框架

蓝牙基础知识

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

  • 这两组api分别对应不同的业务场景,左侧叫做中心模式,就是以你的app作为中心,连接其他的外设的场景,而右侧称为外设模式,使用手机作为外设别其他中心设备操作的场景。
  • 服务和特征,特征的属性(service and characteristic):
    每个设备都会有一些服务,每个服务里面都会有一些特征,特征就是具体键值对,提供数据的地方。每个特征属性分为这么几种:读,写,通知这么几种方式。
//objcetive c特征的定义枚举
typedef NS_OPTIONS(NSUInteger, CBCharacteristicProperties) {
    CBCharacteristicPropertyBroadcast                                               = 0x01,
    CBCharacteristicPropertyRead                                                    = 0x02,
    CBCharacteristicPropertyWriteWithoutResponse                                    = 0x04,
    CBCharacteristicPropertyWrite                                                   = 0x08,
    CBCharacteristicPropertyNotify                                                  = 0x10,
    CBCharacteristicPropertyIndicate                                                = 0x20,
    CBCharacteristicPropertyAuthenticatedSignedWrites                               = 0x40,
    CBCharacteristicPropertyExtendedProperties                                      = 0x80,
    CBCharacteristicPropertyNotifyEncryptionRequired NS_ENUM_AVAILABLE(NA, 6_0)     = 0x100,
    CBCharacteristicPropertyIndicateEncryptionRequired NS_ENUM_AVAILABLE(NA, 6_0)   = 0x200
};

外设、服务、特征间的关系

1442301743869760.png

蓝牙中心模式流程

  1. 建立中心角色

  2. 扫描外设(discover)

  3. 连接外设(connect)

  4. 扫描外设中的服务和特征(discover)

    4.1 获取外设的services

    4.2 获取外设的Characteristics,获取Characteristics的值,获取Characteristics的Descriptor和Descriptor的值

  5. 与外设做数据交互(explore and interact)

  6. 订阅Characteristic的通知

  7. 断开连接(disconnect)

蓝牙外设模式流程

  1. 启动一个Peripheral管理对象

  2. 本地Peripheral设置服务,特性,描述,权限等等

  3. Peripheral发送广告

  4. 设置处理订阅、取消订阅、读characteristic、写characteristic的委托方法

蓝牙设备状态

  1. 待机状态(standby):设备没有传输和发送数据,并且没有连接到任何设

  2. 广播状态(Advertiser):周期性广播状态

  3. 扫描状态(Scanner):主动寻找正在广播的设备

  4. 发起链接状态(Initiator):主动向扫描设备发起连接。

  5. 主设备(Master):作为主设备连接到其他设备。

  6. 从设备(Slave):作为从设备连接到其他设备。

蓝牙设备的五种工作状态

准备(standby)
广播(advertising)
监听扫描(Scanning
发起连接(Initiating)
已连接(Connected)

蓝牙和版本的使用限制

蓝牙2.0 === 越狱设备
蓝牙4.0 === iOS 6 以上
MFI认证设备(Make For ipod/ipad/iphone) === 无限制

XMBlueToothManager

XMBlueToothManager是对BabyBluetooth的二次封装,由于BabyBluetooth中链式函数的使用,导致不能在swift工程中使用,XMBlueToothManager完全弥补了这样的问题,全部使用block回调。代码实现起来方便好用。
IMG_2652.PNG

#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
#import "BabyBluetooth.h"
#import "SVProgressHUD.h"

#define XMSERVICEBLOCK (CBPeripheral *peripheral, NSError *error)
#define XMCHARCTICBLOCK (CBPeripheral *peripheral, CBService *service, NSError *error)
#define XMVALUEFORCHARCTIC (CBPeripheral *peripheral, CBCharacteristic *characteristics, NSError *error)
#define XMDISCOVERDSCRIPFORCH (CBPeripheral *peripheral, CBCharacteristic *characteristic, NSError *error)
#define XMREADVALUEFORDES (CBPeripheral *peripheral, CBDescriptor *descriptor, NSError *error)
/**
 扫描到设备
 
 @param central 外设中心
 @param peripheral 外设
 @param advertisementData advertisementData
 @param RSSI RSSI
 */
typedef void(^XMPeripheralsBlock)(CBCentralManager *central, CBPeripheral *peripheral, NSDictionary *advertisementData, NSNumber *RSSI);

/**
 发现设备服务
 
 @param peripheral 外设
 @param error error
 */
typedef void(^XMDiscoverServices)XMSERVICEBLOCK;

/**
 发现设备的Characteristics
 
 @param peripheral peripheral
 @param service service
 @param error error
 */
typedef void(^XMDiscoverCharacteristics)XMCHARCTICBLOCK;

/**
 读取到的特征值
 
 @param peripheral peripheral
 @param characteristics characteristics
 @param error error
 */
typedef void(^XMReadValueForCharacteristic)XMVALUEFORCHARCTIC;

/**
 读取到特征描述
 
 @param peripheral peripheral
 @param characteristic characteristic
 @param error error
 */
typedef void(^XMDiscoverDescriptorsForCharacteristic)XMDISCOVERDSCRIPFORCH;

/**
 读取Descriptor
 
 @param peripheral peripheral
 @param descriptor descriptor
 @param error error
 */
typedef void(^XMReadValueForDescriptors)XMREADVALUEFORDES;

/**
 设置查找设备的过滤器
 
 @param peripheralName peripheralName
 @param advertisementData advertisementData
 @param RSSI RSSI
 */
typedef BOOL(^XMsetFilterOnDiscoverPeripherals)(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI);

/**
 链接成功与否
 
 @param state state
 */
typedef void(^XMConnectStateBlock)(BOOL state);

/**
 断开连接
 
 @param central central
 @param peripheral peripheral
 @param error error
 */
typedef void(^XMDisconnectAtChannel)(CBCentralManager *central, CBPeripheral *peripheral, NSError *error);

/**
 发现服务
 */
typedef void(^XMDiscoverServicesAtChannel)XMSERVICEBLOCK;

/**
 发现特征
 */
typedef void(^XMDiscoverCharacteristicsAtChannel)XMCHARCTICBLOCK;

/**
 读取到service的特征值
 */
typedef void(^XMReadValueForCharacteristicAtChannel)XMVALUEFORCHARCTIC;

/**
 发现特征描述
 */
typedef 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值