参考:http://www.cocoachina.com/ios/20170824/20369.html
①蓝牙常见名称和缩写
-
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, 可以理解成外设和中心
②蓝牙开发流程
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)
③蓝牙和版本的使用限制
-
蓝牙2.0 === 越狱设备
-
蓝牙4.0 === iOS 6 以上
-
MFI认证设备(Make For ipod/ipad/iphone) === 无限制
④写数据时要注意CBCharacteristicWriteWithoutResponse(写无回复)和
CBCharacteristicWriteWithResponse(写有回复)的区别
characteristic 属性 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 }; */ NSLog(@"%@",characteristic); // 打印结果为 <CBCharacteristic: 0x1702a2a00, UUID = FFF6, properties = 0x16, value = (null), notifying = NO> // 我的结果 为 0x16 (0x08 & 0x16)结果不成立, (0x04 & 0x16)结果成立,那写入类型就是 CBCharacteristicPropertyWriteWithoutResponse }
⑤在使用蓝牙的过程中,如果是iOS10及以上机型需要在info.plist文件中添加NSBluetoothPeripheralUsageDescription描述字段,向用户声明使用蓝牙的意图demo地址 https://download.csdn.net/download/haoxuhong/10290004
-