Skip to content

Commit 3b1535d

Browse files
tonychanchenccharlesren
authored andcommitted
调试蓝牙扫描附近设备、连接设备、发送数据功能
Change-Id: I7f2d16a91f34d01a7b8e2be5e935842e468d04ec
1 parent 1420ca7 commit 3b1535d

File tree

5 files changed

+83
-46
lines changed

5 files changed

+83
-46
lines changed

LinkApp.xcodeproj/xcshareddata/xcschemes/LinkApp.xcscheme

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@
7575
<CommandLineArguments>
7676
<CommandLineArgument
7777
argument = "-FIRDebugEnabled"
78+
isEnabled = "NO">
79+
</CommandLineArgument>
80+
<CommandLineArgument
81+
argument = "-noFIRAnalyticsDebugEnabled"
82+
isEnabled = "YES">
83+
</CommandLineArgument>
84+
<CommandLineArgument
85+
argument = "-FIRDebugDisabled"
7886
isEnabled = "YES">
7987
</CommandLineArgument>
8088
</CommandLineArguments>

Source/LinkApp/Classes/Module/Debug/TIoTDebugtools.swift

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,10 @@ class TIoTDebugtools: NSObject, UITableViewDataSource, UITableViewDelegate {
7272
//配置title和执行的方法
7373
lazy var dataSource: Array = { () -> [Dictionary<String, String>] in
7474
let dataSource = [["title":"跳转H5", "SEL":"jumpControl"],
75-
["title":"修改全局uin", "SEL":"changeGlobalUin"],
76-
["title":"切换至测试环境", "SEL":"changeToDebug"],
77-
["title":"切换至现网环境", "SEL":"changeToRelease"]
75+
// ["title":"修改全局uin", "SEL":"changeGlobalUin"],
76+
// ["title":"切换至测试环境", "SEL":"changeToDebug"],
77+
// ["title":"切换至现网环境", "SEL":"changeToRelease"]
78+
["title":"发送数据到MyLamp", "SEL":"sendBLEData"]
7879
]
7980
return dataSource
8081
}()
@@ -163,14 +164,17 @@ class TIoTDebugtools: NSObject, UITableViewDataSource, UITableViewDelegate {
163164
return view
164165
}()
165166

167+
lazy var blue: BluetoothCentralManager = {
168+
169+
let blue = BluetoothCentralManager.shareBluetooth()!
170+
blue.delegate = self
171+
return blue
172+
}()
173+
166174
@objc func jumpControl() {
167175
// swipeAnimationView.play()
168176

169-
let wxManager = WxManager.shared()
170-
// wxManager?.shareWebPageToWXSceneSession(withTitle: "title",
171-
// description: "desccc",
172-
// thumbImage: UIImage(named: "equipmentSelectTabbar"),
173-
// webUrl: "http://www.baidu.com")
177+
/* let wxManager = WxManager.shared()
174178
wxManager?.shareMiniProgramToWXSceneSession(withTitle: "titlet",
175179
description: "desccc",
176180
path: "",
@@ -179,6 +183,10 @@ class TIoTDebugtools: NSObject, UITableViewDataSource, UITableViewDelegate {
179183
thumbImage: UIImage(named: "equipmentSelectTabbar"),
180184
thumbImageUrl: "",
181185
complete: nil)
186+
*/
187+
188+
189+
blue.scanNearPerpherals()
182190
/*
183191
let alert = UIAlertController(title: "输入URL", message: nil, preferredStyle: UIAlertController.Style.alert)
184192
alert.addTextField { (textField) in
@@ -199,4 +207,25 @@ class TIoTDebugtools: NSObject, UITableViewDataSource, UITableViewDelegate {
199207
TIoTAPPConfig.iot_topController.present(alert, animated: true, completion: nil)
200208
*/
201209
}
210+
211+
@objc func sendBLEData() {
212+
blue.writeData(toBLE: "color")
213+
}
214+
}
215+
216+
217+
extension TIoTDebugtools: BluetoothCentralManagerDelegate {
218+
func scanPerpheralsUpdatePerpherals(_ perphersArr: [CBPeripheral]!) {
219+
220+
for onePerpher in perphersArr {
221+
222+
if onePerpher.name == "MyLamp" {
223+
blue.connect(onePerpher)
224+
}
225+
}
226+
}
227+
228+
func connectPerpheralSucess() {
229+
print("sussss")
230+
}
202231
}

Source/LinkApp/Classes/Universal/蓝牙/BluetoothCentralManager.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
//
22
// BluetoothCentralManager.h
3-
// zhipuzi
4-
//
5-
// Created by 智铺子 on 2017/1/3.
6-
// Copyright © 2017年 迅享科技. All rights reserved.
7-
//
83

94
#import <Foundation/Foundation.h>
105
#import <CoreBluetooth/CoreBluetooth.h>
116

127
@protocol BluetoothCentralManagerDelegate <NSObject>
138
@optional
149
//实时扫描外设(目前扫描10s)
15-
- (void)scanPerpheralsUpdatePerpherals:(NSArray *)perphersArr;
10+
- (void)scanPerpheralsUpdatePerpherals:(NSArray<CBPeripheral *> *)perphersArr;
1611
//连接外设成功
1712
- (void)connectPerpheralSucess;
1813
//发送数据后,蓝牙回调
@@ -33,7 +28,7 @@
3328
/**
3429
* 开始扫描周边设备
3530
*/
36-
- (void)sacnNearPerpherals;
31+
- (void)scanNearPerpherals;
3732

3833
/** 连接设备 */
3934
- (void)connectPeripheral:(CBPeripheral *)peripheral;

Source/LinkApp/Classes/Universal/蓝牙/BluetoothCentralManager.m

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
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];

Source/LinkApp/Supporting Files/LinkApp-Bridging-Header.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
#import "TIoTNavigationController.h"
1212
#import "TIoTAppConfig.h"
1313
#import "WxManager.h"
14-
14+
#import "BluetoothCentralManager.h"

0 commit comments

Comments
 (0)