/*
建立中心设备
扫描外设(Discover Peripheral)
连接外设(Connect Peripheral)
扫描外设中的服务和特征(Discover Services And Charateristics)
利用特征与外设做数据交互(Explore And Interact)
断开连接(Disconnect)
*/ #import "ViewController.h"
#import <coreBluetooth/CoreBluetooth.h> @interface ViewController ()<CBCentralManagerDelegate, CBPeripheralDelegate> @property (nonatomic, strong) CBCentralManager *mgr;
/** 发现的外设数组*/
@property (nonatomic, strong) NSMutableArray *peripheralArray; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; // 1.建立中心设备
// queque:如果说你传空,在主队列
self.mgr = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; // 2.扫描外设 } - (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated]; // 最后一步,断开连接
[self.mgr stopScan];
} #pragma CBCentralManagerDelegate
/*
state:
CBManagerStateUnknown = 0, 未知
CBManagerStateResetting, 重置
CBManagerStateUnsupported, 不支持
CBManagerStateUnauthorized, 未经授权
CBManagerStatePoweredOff, 没有启动
CBManagerStatePoweredOn, 开启
*/
- (void)centralManagerDidUpdateState:(CBCentralManager *)central { NSLog(@"state = %zd", central.state);
// 蓝牙开启
if (central.state == CBManagerStateUnknown) { NSLog(@"未知的蓝牙");
}
if (central.state == CBManagerStatePoweredOn) { // 2.扫描外设
[self.mgr scanForPeripheralsWithServices:nil options:nil];
}
} /**
3.连接外设(Connect Peripheral)
@param peripheral 外设
@param advertisementData 相关的二进制数据
@param RSSI 信号强度
*/
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI { // 1.记录外设,必须要有数组进来行存储
if ([self.peripheralArray containsObject:peripheral]) { [self.peripheralArray addObject:peripheral];
}
// 2.隐藏步骤,tableview表格列表(省略了)
// 3.连接外围设备
[self.mgr connectPeripheral:peripheral options:nil];
// 4.设置外设的代理
peripheral.delegate = self; } // 连接到外设之后,会调用这个代理方法
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral { // 扫描服务
// 传nil代表扫描所有服务
[peripheral discoverServices:nil]; } #pragma CBPeripheralDelegate
// 外设扫描里面的服务
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error { // 获取外设的服务
for (CBService *service in peripheral.services) { if ([service.UUID.UUIDString isEqualToString:@""]) {
// uuid一致,那就开始扫描特征
[peripheral discoverCharacteristics:nil forService:service]; }
}
} // 当发现到特征的时候会调用
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error { //
for (CBCharacteristic *characteristic in service.characteristics) { if ([characteristic.UUID.UUIDString isEqualToString:@""]) { // [peripheral readValueForDescriptor:<#(nonnull CBDescriptor *)#>]; // [peripheral writeValue:<#(nonnull NSData *)#> forDescriptor:<#(nonnull CBDescriptor *)#>];
}
}
} - (NSMutableArray *)peripheralArray { if (!_peripheralArray) { _peripheralArray = [NSMutableArray array];
}
return _peripheralArray;
}

蓝牙开发<coreBluetooth/CoreBluetooth.h>的更多相关文章

  1. iOS蓝牙开发CoreBluetooth快速入门

    在iOS开发中,实现蓝牙通信有两种方式,一种是使用传统的GameKit.framework,另一种就是使用在iOS 5中加入的CoreBluetooth.framework. 利用CoreBlueto ...

  2. iOS之蓝牙开发—CoreBluetooth详解

    CoreBluetooth的API是基于BLE4.0的标准的.这个框架涵盖了BLE标准的所有细节.仅仅只有新的iOS设备和Mac是和BLE标准兼容.在CoreBluetooth框架中,有两个主要的角色 ...

  3. iOS 蓝牙开发之(CoreBlueTooth)

    CoreBlueTooth 简介: 可用于第三方的蓝牙交互设备 设备必须支持蓝牙4.0 iPhone的设备必须是4S或者更新 iPad设备必须是iPad mini或者更新 iOS的系统必须是iOS 6 ...

  4. ios蓝牙开发(三)ios连接外设的代码实现:手机app去读写蓝牙设备。

    手机app去读写蓝牙设备....... 代码下载: 原文博客主提供Github代码连接,地址是:https://github.com/coolnameismy/demo ios连接外设的代码实现流程: ...

  5. iOS-BLE蓝牙开发持续更新

    文/煜寒了(简书作者)原文链接:http://www.jianshu.com/p/84b5b834b942著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 在写这个博客之前,空余时间抽看 ...

  6. iOS-BLE蓝牙开发

    Demo地址:WEBlueToothManager 在写这个博客之前,空余时间抽看了近一个月的文档和Demo,系统给的解释很详细,接口也比较实用,唯独有一点,对于设备 的唯一标示,网上众说纷纭,在这里 ...

  7. ios蓝牙开发(二)ios连接外设的代码实现

    上一篇文章介绍了蓝牙的技术知识,这里我们具体说明一下中心模式的应用场景.主设备(手机去扫描连接外设,发现外设服务和属性,操作服务和属性的应用.一般来说,外设(蓝牙设备,比如智能手环之类的东西), 会由 ...

  8. iOS蓝牙开发

    蓝牙常见名称和缩写 MFI ======= make for ipad ,iphone, itouch 专们为苹果设备制作的设备 BLE ==== buletouch low energy,蓝牙4.0 ...

  9. iOS蓝牙开发(4.0)详解

    最近由于项目需要, 一直在研究蓝牙4.0,在这儿分享给大家, 望共同进步. 一.关于蓝牙开发的一些重要的理论概念: 1.当前ios中开发蓝牙所运用的系统库是<CoreBluetooth/Core ...

随机推荐

  1. OG数据预处理

    1.影像处理        ogCalcExtent --srs EPSG:26711 --inputdir D:/software/preprocess/data/bugaboos/ --filet ...

  2. mysql 客户端

    MySQL是基于C/S模式的数据库管理系统.MySQL公司开发了众多的客户端软件来帮助用户管理MySQL软件,最著名的就是 MySQL Command Line Client 和 MySQL-Work ...

  3. 获取WebService的请求信息

    一个已经写好的项目中有多个WebService,由于之前没有记录请求信息的,有时候需要查错等需要找到当次的请求信息,所以需要加入记录请求信息的功能. 首先想到的是在每一个带有WebMethod特性的方 ...

  4. js过滤HTML标签以及&nbsp;

    function removeHTMLTag(str) { str = str.replace(/<\/?[^>]*>/g,''); //去除HTML tag str = str.r ...

  5. ORA-06512 问题解决

    SQL> select * from dba_role_privs where grantee='SUK';     GRANTEE GRANTED_ROLE ADMIN_OPTION DEFA ...

  6. 检查windows系统支持的密码套件

    Windows 10客户端及Windows server 2016 服务器可以使用powershell 命令获得系统支持的密码套件列表,禁用启用相应的密码套件. #命令链接:https://techn ...

  7. 摄像机内参相关(3ds max)

    一般的原理参考:http://ksimek.github.io/2012/08/13/introduction/ 对于3ds max建模时使用的target camera,如果想得到它的内外参,可以利 ...

  8. windows命令行大全

    命令简介 cmd是command的缩写.即命令行 . 虽然随着计算机产业的发展,Windows 操作系统的应用越来越广泛,DOS 面临着被淘汰的命运,但是因为它运行安全.稳定,有的用户还在使用,所以一 ...

  9. SharePoint 2007——内容管理之归档篇

    如果需要使用这个功能普通的站点上(没有使用Record Center站点模板的站点),必须激活'Office SharePoint Server Publishing’  featue. 在使用Rec ...

  10. java String,StringBuilder和StringBuffer

    String:1.java语言中的字符串值属于String类,虽然有其它方法表示字符串(如字符数组),但java一般使用Sting类作为字符串的标准格式,java编译器把字符串值作为String对象. ...