/*
建立中心设备
扫描外设(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. Batch Norm、Layer Norm、Weight Norm与SELU

    加速网络收敛——BN.LN.WN与selu 自Batch Norm出现之后,Layer Norm和Weight Norm作为Batch Norm的变体相继出现.最近又出来一个很”简单”的激活函数Sel ...

  2. UserInfoActivity用户图像修改和退出登录

    @OnClick(R.id.btn_user_logout) public void logout(View view){//"退出登录"button的回调方法 //1.将保存在s ...

  3. 大O表示法

    概念 大O表示法是和数据项的个数相关联的粗略度量算法时间复杂度的快捷方法. 常数一个无序可重复数组插入一个数据项的时间T是常数K,常数K表示一次插入所花费的时间,包含cpu.编译器等工作时间.可表示为 ...

  4. 读写JSON作配置文件

    个人不太喜欢XML,于是找了JSON来做配置,JSON虽然有很多引号,但这种key-value的形式,非常符合我的思维,就像是一个萝卜一个坑.最近在读写JSON文件,需要注意两个问题. 中文乱码: 直 ...

  5. 【Java】读写文本文件

    package rw; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileOutput ...

  6. 选择 Java 编写 iOS 与 安卓 App的八大理由

    [编者按]本文作者为 RoboVM 的 CEO 兼联合创始人 Henric Müller,主要介绍选用 Java 编写移动应用的八大理由.文章系国内 ITOM 管理平台 OneAPM 编译呈现. 在过 ...

  7. Maven安装及MyEclipse中使用Maven

    Step1:下载mavenhttp://maven.apache.org/download.cgi,下载好后解压到一个路径中 Step2:配置环境变量, 新建变量名:MAVEN_HOME 变量值:D: ...

  8. SQL Server 索引知识-结构,实现

    索引的作用毋庸置疑,但他是如何组织,并实现提高语句访问效率的呢?本篇文章为大家做个详细的介绍. 聚集索引架构 B-tree 如图1-1 a.B-tree的结构,叶子节点为数据.数据按照聚集索引键有序排 ...

  9. 12.2Data Guard新特性--使用DBMS_DBCOMP.DBCOMP数据比较

          Oracle Data Guard会主动对Hot数据(数据正被读取或修改)执行验证, 无论是primary还是standby,但对于那些Cold数据不会做任何检查和校验.所以在12.2版本 ...

  10. Windows与Linux 互相访问,挂载过程

    开始使用Linux时浏览器无法访问,多次尝试以失败告终,果断放弃自我动手, 找了大神帮助,弄了半天终于可以访问.但是之前在Windows下的文件也不能放弃,从大神那里那里文档,然后进行尝试 1.在Wi ...