Core Bluetooth的使用

1,建立中心设备

2,扫描外设(Discover Peripheral)

3,连接外设(Connect Peripheral)

4,扫描外设中的服务和特征(Discover Services And Characteristics)

5,利用特征与外设做数据交互(Explore And Interact)

6,断开连接(Disconnect)

//  ViewController.m

//  01-蓝牙4.0

//  Created by apple on 16/1/4.

//  Copyright © 2016年 apple. All rights reserved.

#import "ViewController.h"

#import <CoreBluetooth/CoreBluetooth.h>

@interface ViewController ()<CBCentralManagerDelegate,CBPeripheralDelegate>

@property (nonatomic,strong)CBCentralManager *manager;

@property (nonatomic,strong)NSMutableArray *peripherals;//盛放外围设备的数组

@end

@implementation ViewController

- (NSMutableArray *)peripherals

{

if (_peripherals == nil) {

_peripherals = [NSMutableArray array];

}

return _peripherals;

}

- (void)viewDidLoad {

[super viewDidLoad];

//1.创建中心管理者

//传 nil 为主队列

CBCentralManager *manager = [[CBCentralManager alloc]initWithDelegate:self queue:nil];

self.manager = manager;

}

//状态发生改变的时候  回来到此方法

- (void)centralManagerDidUpdateState:(CBCentralManager *)central

{

//2.先判断蓝牙是打开

if (central.state == CBCentralManagerStatePoweredOn) {

//3.搜索外围设备

[central scanForPeripheralsWithServices:nil options:nil];

}

}

//CBPeripheral 外围设备

//advertisementData配置信息

//RSSI  信号强度

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI

{

[self.peripherals addObject:peripheral];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

//4.连接外围设备

//遍历数组

for (int i = 0; i < self.peripherals.count; i ++) {

CBPeripheral *peripheral = self.peripherals[i];

[self.manager connectPeripheral:peripheral options:nil];

}

}

//连接外围设备之后  会来到此方法

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral

{

//5.获取服务

[peripheral  discoverServices:nil];

peripheral.delegate = self;

}

//发现服务会来的此方法

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error

{

//6.通过服务去找特征

[peripheral  discoverCharacteristics:nil forService:peripheral.services.lastObject];

}

//发现特征会来的此方法

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error

{

}

@end

iOS中蓝牙的使用的更多相关文章

  1. iOS中 蓝牙2.0详解/ios蓝牙设备详解 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 整体布局如下:     程序结构如右图: 每日更新关注:http://weibo.com/hanjunqiang  ...

  2. ios中蓝牙自动连接出现硬件提示框的问题

    出现如图所示情况,这时候有两种方法可以处理,一种是让硬件部修改硬件配对,另一种是程序里面测试该提示框的对应特征值,不要调用该特征值就不会出现 //2.扫描到Characteristics,特征回调 - ...

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

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

  4. iOS中的蓝牙

    iOS中的蓝牙 概述 iOS中提供了4个框架用于实现蓝牙连接 1.GameKit.framework(用法简单) 只能用于iOS设备之间的同个应用内连接,多用于游戏(eg.拳皇,棋牌类),从iOS7开 ...

  5. IOS中调用系统的电话、短信、邮件、浏览功能

    iOS开发系列--通讯录.蓝牙.内购.GameCenter.iCloud.Passbook系统服务开发汇总 2015-01-13 09:16 by KenshinCui, 26990 阅读, 35 评 ...

  6. iOS 中有用的开源库

    youtube下载神器:https://github.com/rg3/youtube-dl vim插件:https://github.com/Valloric/YouCompleteMe vim插件配 ...

  7. iOS关于蓝牙连接的简单介绍与使用

    下面是两台iPhone6连接同一台蓝牙设备的结果: **成功连接**** peripheral: <CBPeripheral: 0x1700f4500, identifier = 50084F6 ...

  8. iOS 中 常用的第三方库

    现在对于我们 iOS 开发来说,基本上说不可能不使用第三方轮子啦,毕竟没那么多时间,而且自己造的轮子往往想着成为上图中的最后一个,结果却成了上图中第二个或第一个啦,当然大公司另当别论.下面我从之前用过 ...

  9. esp32使iOS 获取蓝牙外设的Mac地址

    最近在做一个需要上下位机的项目,我负责的任务下位机,使用的主控芯片是esp32.这个项目中有一项是需要手机扫描二维码然后连接作为esp32的蓝牙.二维码中包含了mac地址信息,在手机扫描周围设备的时候 ...

随机推荐

  1. python_如何让类支持比较运算?

    案例: 有时我们希望自定义的类,实例间可以使用比较运算符进行比较,我们自定义比较的行为. 需求: 有一个矩形的类,我们希望比较两个矩形的实例时,比较的是他们的面积 如何解决这个问题? 在类中重新定义比 ...

  2. vs Code 运行一个本地WEB服务器

    VS Code作为前端开发工具,值得推荐,用上之后,爱不释手 当我们建立一个全静态的前端项目时,如果需要将整个项目运行起来也非常简单,两步即可: 1.安装npm install -g live-ser ...

  3. linkinFrame--测试项目添加git管理

    OK,前面一篇博客,已经搭建好了项目的基础的结构.现在我们添加一个简单的servlet来测试下项目能不能跑,然后在讲项目添加到coding上用git管理. 1,我们编写一个servlet,编写一个js ...

  4. curl错误码说明

    1.得到错误码 $errno=curl_errno($ch); if($errno!=0){ -- } 2.错误码说明 <?php return [ '1'=>'CURLE_UNSUPPO ...

  5. 【转】Shell执行MySql操作

    mysql  -hhostname -Pport -uusername -ppassword  -e  相关mysql的sql语句,不用在mysql的提示符下运行mysql,即可以在shell中操作m ...

  6. LINUX下SYN攻防战 [转]

    LINUX下SYN攻防战        (一)SYN攻击原理SYN攻击属于DOS攻击的一种,它利用TCP协议缺陷,通过发送大量的半连接请求,耗费服务器CPU和内存资源.SYN攻击聊了能影响主机外,还可 ...

  7. bzoj 4562 [Haoi2016]食物链

    4562: [Haoi2016]食物链 Time Limit: 10 Sec  Memory Limit: 128 MB Description 如图所示为某生态系统的食物网示意图,据图回答第1小题 ...

  8. centos 下安装 Jre 及 selenium

    下载软件包 下载链接: jre-7u55-linux-i586.tar.gz : http://pan.baidu.com/s/14cjds selenium-server-standalone-2. ...

  9. Vue脚手架(vue-cli)安装总结

    单页Web应用(single page web application,SPA),就是只有一张Web页面的应用,是加载单个HTML 页面并在用户与应用程序交互时动态更新该页面的Web应用程序. 提供一 ...

  10. 软件开发:网站&视频&书籍推荐(不断更新)

    利用书籍进行系统学习,凭借博客/新闻等资料开阔眼界,辅之以代码及项目实战,并勤加以总结,方可进步. 常用网站: Leetcode刷题:https://leetcode.com/ ,练习数据结构和算法必 ...