11//
22// BluetoothCentralManager.m
3- // zhipuzi
4- //
5- // Created by 智铺子 on 2017/1/3.
6- // Copyright © 2017年 迅享科技. All rights reserved.
73//
84#import " BluetoothCentralManager.h"
95// 服务UUID
@@ -54,16 +50,23 @@ - (instancetype)init {
5450/* *
5551 扫描周周的设备
5652 */
57- - (void )sacnNearPerpherals {
53+ - (void )scanNearPerpherals {
5854 WCLog (@" 开始扫描四周的设备" );
55+ [self .deviceList removeAllObjects ];
5956 /* *
6057 1.第一个参数为Services的UUID(外设端的UUID) 不能为nil
6158 2.第二参数的CBCentralManagerScanOptionAllowDuplicatesKey为已发现的设备是否重复扫描,如果是同一设备会多次回调
6259 */
6360// [self.centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:kServiceUUID]] options:@{CBCentralManagerScanOptionAllowDuplicatesKey : @NO}];
6461
65- // 这里已确认蓝牙已打开才开始扫描周围的外设。第一个参数nil就是扫描周围所有的外设。
66- [self .centralManager scanForPeripheralsWithServices: nil options: @{CBCentralManagerScanOptionAllowDuplicatesKey : @NO }];
62+
63+ if (self.centralManager .state == CBManagerStatePoweredOn) {
64+ // 扫描10秒停止扫描
65+ [self performSelector: @selector (stopScan ) withObject: nil afterDelay: 5.0 ];
66+
67+ // 这里已确认蓝牙已打开才开始扫描周围的外设。第一个参数nil就是扫描周围所有的外设。
68+ [self .centralManager scanForPeripheralsWithServices: nil options: @{CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];
69+ }
6770}
6871
6972/* *
@@ -74,6 +77,14 @@ - (void)stopScan{
7477 [self .centralManager stopScan ];
7578}
7679
80+ // / 连接指定的设备
81+ - (void )connectPeripheral : (CBPeripheral *)peripheral {
82+ self.peripheral = peripheral;
83+ WCLog (@" ----尝试连接设备----\n %@ " , peripheral);
84+ [self .centralManager connectPeripheral: peripheral
85+ options: [NSDictionary dictionaryWithObject: [NSNumber numberWithBool: YES ] forKey: CBConnectPeripheralOptionNotifyOnDisconnectionKey]];
86+ }
87+
7788/* *
7889 断开设备连接
7990 */
@@ -90,16 +101,14 @@ - (void)disconnectPeripheral{
90101- (void )writeDataToBLE : (NSString *)context
91102{
92103 // 发送下行指令(发送一条)
93- NSData *data = [context dataUsingEncoding: NSUTF8StringEncoding];
94- [self .peripheral writeValue: data forCharacteristic: self .characteristic type: CBCharacteristicWriteWithResponse];
104+ if (self.characteristic ) {
105+ NSData *data = [context dataUsingEncoding: NSUTF8StringEncoding];
106+ [self .peripheral writeValue: data forCharacteristic: self .characteristic type: CBCharacteristicWriteWithResponse];
107+ }
95108}
96109
97- -(void )delayMethod
98- {
99- [self .centralManager stopScan ];
100- }
101110
102- #pragma mark - CBCentralManagerDelegate
111+ #pragma mark ------------------------扫描发现蓝牙设备 CBCentralManagerDelegate
103112/* *
104113 检查App设备蓝牙是否可用
105114
@@ -109,7 +118,7 @@ - (void)centralManagerDidUpdateState:(CBCentralManager *)central {
109118 switch (central.state ){
110119 case CBManagerStatePoweredOn:{
111120 // 蓝牙已打开,开始扫描外设
112- [self sacnNearPerpherals ];
121+ [self scanNearPerpherals ];
113122 }
114123 break ;
115124
@@ -156,8 +165,11 @@ - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeri
156165 }
157166 }
158167
159- if ([self .delegate respondsToSelector: @selector (scanPerpheralsUpdatePerpherals: )]) {
160- [self .delegate scanPerpheralsUpdatePerpherals: self .deviceList.copy];
168+ if (self.deviceList .count > 0 ) {
169+
170+ if ([self .delegate respondsToSelector: @selector (scanPerpheralsUpdatePerpherals: )]) {
171+ [self .delegate scanPerpheralsUpdatePerpherals: self .deviceList.copy];
172+ }
161173 }
162174
163175// NSArray *serviceUUIDArr = advertisementData[@"kCBAdvDataServiceUUIDs"];
@@ -170,29 +182,21 @@ - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeri
170182// [self connectPeripheral:peripheral];
171183// }
172184// }
173-
174- // 扫描10秒停止扫描
175- [self performSelector: @selector (delayMethod ) withObject: nil /* 可传任意类型参数*/ afterDelay: 10.0 ];
176185}
177186
178- // / 连接指定的设备
179- - (void )connectPeripheral : (CBPeripheral *)peripheral {
180- self.peripheral = peripheral;
181- WCLog (@" ----尝试连接设备----\n %@ " , peripheral);
182- [self .centralManager connectPeripheral: peripheral
183- options: [NSDictionary dictionaryWithObject: [NSNumber numberWithBool: YES ] forKey: CBConnectPeripheralOptionNotifyOnDisconnectionKey]];
184- }
185187
186188// / 连接外设成功的代理方法
189+ #pragma mark ------------------------连接成功、失败、终端
187190-(void )centralManager : (CBCentralManager *)central didConnectPeripheral : (CBPeripheral *)peripheral {
191+ [self stopScan ];
192+
188193 [MBProgressHUD showSuccess: [NSString stringWithFormat: @" %@ %@ %@ " ,NSLocalizedString(@" bluetoothDevice" , @" 蓝牙设备" ),NSLocalizedString(@" connected" , @" 已连接" ),peripheral.name]];
189194
190195 // 设置设备代理
191196 [peripheral setDelegate: self ];
192197 // 查找外围设备中的所有服务
193198 [peripheral discoverServices: nil ];
194199
195- [self .centralManager stopScan ];
196200
197201 if ([self .delegate respondsToSelector: @selector (connectPerpheralSucess )]) {
198202 [self .delegate connectPerpheralSucess ];
@@ -210,7 +214,7 @@ - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPe
210214}
211215
212216
213- #pragma mark - CBPeripheralDelegate
217+ #pragma mark -------------外部蓝牙设备的服务,有了服务才能写数据 CBPeripheralDelegate
214218// / 获取外设服务的代理
215219- (void )peripheral : (CBPeripheral *)peripheral didDiscoverServices : (NSError *)error {
216220 if (error) {
@@ -263,12 +267,13 @@ - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForServi
263267 // 订阅, 实时接收
264268 [peripheral setNotifyValue: YES forCharacteristic: characteristic];
265269
270+ /*
266271 // 获取特征值发送数据(用于测试,正式可以注释下面两行代码)
267272 // 发送下行指令(发送一条)
268273 NSData *data = [@"蓝牙初始化数据" dataUsingEncoding:NSUTF8StringEncoding];
269274 // 将指令写入蓝牙
270275 [self.peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
271-
276+ */
272277 }
273278
274279 [peripheral discoverDescriptorsForCharacteristic: characteristic];
0 commit comments