/*
建立中心设备
扫描外设(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. Pwn with File结构体(四)

    前言 前面几篇文章说道,glibc 2.24 对 vtable 做了检测,导致我们不能通过伪造 vtable 来执行代码.今天逛 twitter 时看到了一篇通过绕过 对vtable 的检测 来执行代 ...

  2. android 每个块半径不同的扇形图,自定义view

    1.首先看效果图 2.自定义PieChartView,继承自View,下边为PieChartView代码 package com.yingjinbao.im.peach.customview; imp ...

  3. private 与 super

    public class Person { private String name; private int age; } public class Student extends Person { ...

  4. JQuery 选择器 筛选器

    什么是jQuery对象 参考:http://jquery.cuishifeng.cn/css.html jQuery 对象就是通过jQuery包装DOM对象后产生的对象.jQuery 对象是 jQue ...

  5. 使用cancelBubble竟然可以阻止所有浏览器的冒泡?

    以前一直以为cancelBubble是IE8及以下的专属,今天做一个测试的时候意外发现了所有浏览器都支持,便提出来希望有哪位解释下. 1.使用原生js在FF下和chrome下两种方法都可以阻止冒泡 d ...

  6. LeetCode 题解之Add Binary

    1.题目描述 2.题目分析 使用string 的逆向指针,做二进制加法,注意进位问题就可以. 3.代码 string addBinary(string a, string b) { string::r ...

  7. tshark----wireshark的命令行工具

    tshark - 转储和分析网络流 概要 tshark的 [  -2  ] [  -a  <捕捉自动停止条件>] ... [  -b  <捕捉环形缓冲区选项>] ... [   ...

  8. [翻译] BKZoomView

    BKZoomView https://github.com/freshking/BKZoomView A UIView that will zoom into its parent view. It ...

  9. jQuery Ajax url使用方式

    jQuery Ajax的使用场景: 页面需要通过后台逻辑,但只需要局部刷新以显示新的内容. jQuery Ajax url使用方式1.servlet方式: 需要在struts.xml中写一个actio ...

  10. playfair

    又是一道实验吧的题,哈哈,我比较弱. 因为题目写了play我首先想到的是playfair,好,下面先看下百科 好了,已知了密钥: 所以有: s n f m th b g o ui c j p vy d ...