2.1设置对象 @property (strong, nonatomic) CBCentralManager *CBManager; @property (strong, nonatomic) CBPeripheral *peripheral; 遵照对应的代理:CBCentralManagerDelegate,CBPeripheralDelegate 2.2实例化中心设备 //options 后表明的是是否开启alert提示 self.CBManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:@{@"CBCentralManagerOptionShowPowerAlertKey":@NO}]; 代理方法实现监控蓝牙开启状态以及链接发送数据 3.1实时监控蓝牙打开状态 - (void)centralManagerDidUpdateState:(CBCentralManager *)central{ switch (central.state) { case 5: // 第一个参数填nil表明扫描全部蓝牙设备,第二个参数options也能够写nil,调用下方方法后会开始扫描周围蓝牙设备开始调用3.2中代理方法 [self.CBManager scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey : [NSNumber numberWithBool:YES]}]; break; } } 3.2 开始扫描数据,扫描到对应数据后中止扫描 -(void)centralManager:(CBCentralManager*)central didDiscoverPeripheral:(CBPeripheral*)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI { //获取广播数据中的名称 NSString*localName=[advertisementData valueForKey:@"kCBAdvDataLocalName"]; if ([localName hasPrefix:@"HC"]) { _peripheral = peripheral; [self.CBManager connectPeripheral:_peripheral options:nil]; // 扫描到设备以后中止扫描 [self.CBManager stopScan]; } } 3.3链接到当前设备 //链接外设成功,开始发现服务 - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral { NSLog(@"成功链接 peripheral: %@ with UUID: %@",peripheral,peripheral.identifier); // 链接设备以后设置蓝牙对象的代理,扫描服务 [self.peripheral setDelegate:self]; [self.peripheral discoverServices:nil]; NSLog(@"扫描服务"); } 3.3 发现服务,而后根据服务查找对应的特征 //已发现服务 -(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error { NSLog(@"发现服务."); for (CBService *s in peripheral.services) { if ([s.UUID.UUIDString isEqual:ServicesUUID]) { [peripheral discoverCharacteristics:nil forService:s]; } } } 3.4 根据服务UUID查询对应服务的特征 -(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error { for (CBCharacteristic *c in service.characteristics) { if ([c.UUID.UUIDString isEqual:CharacteristicsUUID]) { self.characteristic = c; //把数据进行转NSData编码,转数据成功后写数据给蓝牙外设 NSData *data = [self returnDataWithKey:self.BleId]; [_peripheral setNotifyValue:YES forCharacteristic:c]; [_peripheral writeValue:data forCharacteristic:self.characteristic type:CBCharacteristicWriteWithoutResponse]; } 3.5 读取蓝牙外设返回的数据 //获取外设发来的数据,不管是read和notify,获取数据都是从这个方法中读取。 - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { if ([characteristic.UUID.UUIDString isEqual:CharacteristicsUUID]) { NSData * data = characteristic.value; ///<d6c8d6c8 40010000 00000000 00000000 00000000> 成功返回数据 ///<d6c8d6c8 80010000 00000000 00000000 00000000> 校验和错误返回数据 // 比对data数据是不是返回成功的data数据或者失败的数据 ,成功返回,提示用户开门成功 }
4.1链接蓝牙设备发送数据,蓝牙设备收不到,具体缘由是不清楚须要链接的硬件具体的服务以及服务对应的特征;框架
4.2 发送数据是须要发送先把当前的16进制数据转byte,而后转NSData类型数据;ide
4.2.1 获取到门锁上15位ID号,15位ID字符转16进制数据:编码
- (NSString *)hexStringFromString:(NSString *)stringatom
4.2.2 转换好的15位ID号进行校验和处理,处理后拼接字符串,把拼接好的16进制字符串转byte而后再转NSData类型数据:spa
- (NSData *)returnDataWithKey:(NSString *)key代理
4.2.3 NSData数据转NSString类型:code
- (NSString *)convertDataToHexStr:(NSData *)data对象
4.2.4 获取蓝牙外设发送的数据时,每次接收最大为20字节,获取时须要屡次获取拼接data,而后再与对应的协议进行对比;blog