iOS知识点~miOS学习开发

iOS蓝牙开发笔记(一)

2017-02-01  本文已影响1265人  lynn_lin010

最近做的是有关智能硬件相关的项目,项目完成之后想着整理一下相关的知识点,以便以后查阅.
一.蓝牙相关概念

  1. 蓝牙开发中主要设备概念有中心设备(iPhone手机),连接设备这两个概念
  2. 搜索过程中有服务和特征值这两个概念,可以通过筛选服务的UUID从而达到只展示特定某类产品的蓝牙设备.
  3. 需要注意一点的是iOS不提供MAC地址,所以设备的UUID是由手机的ID和MAC地址算出来的.因此,只有同一手机连接同一蓝牙设备的时候,UUID作为唯一标识才有用处.

二.蓝牙相关方法

  1. 连接蓝牙步骤
// 创建中心设备管理
_bluetoothManager= [[CBCentralManageralloc]initWithDelegate:selfqueue:nil];
// 开始扫描外设
[self.bluetoothManager scanForPeripheralsWithServices:nil options:nil];
  1. 中心设备代理方法
// 中心设备的蓝牙状态发生变化之后会调用此方法 [必须实现的方法]
 - (void)centralManagerDidUpdateState:(CBCentralManager *)central;
 // 中心设备状态枚举
typedef NS_ENUM(NSInteger, CBCentralManagerState) {
    CBCentralManagerStateUnknown = CBManagerStateUnknown,// 蓝牙状态未知
    CBCentralManagerStateResetting = CBManagerStateResetting,
    CBCentralManagerStateUnsupported = CBManagerStateUnsupported, // 不支持蓝牙
    CBCentralManagerStateUnauthorized = CBManagerStateUnauthorized, // 蓝牙未授权
    CBCentralManagerStatePoweredOff = CBManagerStatePoweredOff, // 蓝牙关闭状态
    CBCentralManagerStatePoweredOn = CBManagerStatePoweredOn, // 蓝牙开启状态
} NS_DEPRECATED(NA, NA, 5_0, 10_0, "Use CBManagerState instead");
// 应用从后台恢复到前台的时候,会和系统蓝牙进行同步,调用此方法
 - (void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary<NSString *, id> *)dict;
// 代理方法的dict中相关的key值
CBCentralManagerRestoredStatePeripheralsKey // 返回一个中心设备正在连接的所有外设数组
CBCentralManagerRestoredStateScanServicesKey // 返回一个中心设备正在扫描的所有服务UUID的数组
CBCentralManagerRestoredStateScanOptionsKey // 返回一个字典包含正在被使用的外设的扫描选项
// 中心设备发现外设的时候调用的方法
 - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI;
// 中心设备连接上外设时候调用的方法
 - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral;
// 中心设备连接外设失败时调用的方法
 - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error;
// 中心设备与已连接的外设断开连接时调用的方法
 - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error;
  1. 外设代理方法
// 连接外设之后发现服务时调用的方法 调用discoverServices:之后发现服务后会调用此代理方法
 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(nullable NSError *)error;
// 连接外设之后发现特征时调用的方法 调用discoverCharacteristics:forService:之后发现特征后会调用此代理方法
 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(nullable NSError *)error;
// 读取外设数据 调用readValueForCharacteristic:之后调用此代理方法
 - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
// 将数据写入外设 调用writeValue:forCharacteristic:type:之后调用此代理方法
 - (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
// 外设更改名字时调用的方法
 - (void)peripheralDidUpdateName:(CBPeripheral *)peripheral NS_AVAILABLE(NA, 6_0);
// 外设服务发生改变的时候调用此方法
 - (void)peripheral:(CBPeripheral *)peripheral didModifyServices:(NSArray<CBService *> *)invalidatedServices NS_AVAILABLE(NA, 7_0);
// 外设读取RSSI的方法
 - (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(nullable NSError *)error NS_AVAILABLE(NA, 8_0);
// 订阅之后订阅是否成功调用此方法 调用setNotifyValue:forCharacteristic:之后调用此代理方法;订阅特征的特征值有变化的时候 调用peripheral:didUpdateValueForCharacteristic:error:这个方法
 - (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
// 调用discoverDescriptorsForCharacteristic:之后调用代理方法
 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
// 读取特征描述后调用方法 调用readValueForDescriptor:之后调用此代理方法
 - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForDescriptor:(CBDescriptor *)descriptor error:(nullable NSError *)error;
// 写入特征描述后调用的方法 调用writeValue:forDescriptor:之后调用此代理方法
 - (void)peripheral:(CBPeripheral *)peripheral didWriteValueForDescriptor:(CBDescriptor *)descriptor error:(nullable NSError *)error;
// 调用discoverIncludedServices:forService:之后发现includedServices中特定的服务后调用此代理方法
 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverIncludedServicesForService:(CBService *)service error:(nullable NSError *)error;

三.info.plist

  1. 如果需要在后台使用蓝牙的话,需要在info.plist中Required background modes数组中添加两个元素App shares data using CoreBluetooth;App communicates using CoreBluetooth.需要注意的是不要手写,直接选择比较好.有次上传app的时候,因为这个上传不成功.直接手写的话,看Info.plist的源文件显示的不正确,所以还是直接选择比较好.
  2. iOS10之后要再Info.plist文件中加入key为Privacy - Bluetooth Peripheral Usage Description的使用蓝牙设备描述
上一篇下一篇

猜你喜欢

热点阅读