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. Fragment禁止预加载

    项目中经常会用到ViewPager+Fragment组合,然而,有一个很让人头疼的问题就是,去加载数据的时候由于ViewPager的内部机制所限制,所以它会默认至少预加载一个. 1.既然说是ViewP ...

  2. 原生 JS 实现一个瀑布流插件

    更好的阅读体验,点击 原文地址 瀑布流布局中的图片有一个核心特点 -- 等宽不定等高,瀑布流布局在国内网网站都有一定规模的使用,比如pinterest.花瓣网等等.那么接下来就基于这个特点开始瀑布流探 ...

  3. 重温MFC

    1. Button控件 2. 旋转和高级编辑控件 3. 标签控件和属性页 4. 列表控件 5. 树控件 6. 进度条控件和滑动条控件 7. 滚动条 8.工具栏和状态栏

  4. common-logging--源码之SimpleLog

    common-logging源码Log接口 在common-logging的源码中,将log核心类抽象成了一个Log接口. 这里贴出Log接口的源码: /* * Licensed to the Apa ...

  5. Oracle多行记录合并的几种方法

    今天正好遇到需要做这个功能,顺手搜了一下网络,把几种方法都列出来,方便以后参考. 1 什么是合并多行字符串(连接字符串)呢,例如: SQL> desc test; Name Type Nulla ...

  6. Python简单爬虫Requests

    首先添加库 附配环境变量:安装环境变量 cmd==> 输入指令: path=%path%;C:\Python(Python安装路径) 回车 python2.7版本可能没有pip的话可以先到www ...

  7. js 生成 UUID

    在项目中遇到要生成 UUID 的需求,第一反应就是有没有原生的生成方法,找了找,发现没有,只能自己建立算法 function. 下面是我用的算法 function uuid(len, radix) { ...

  8. jQuery的Nicescroll滚动条插件使用方法

    Nicescroll滚动条插件是一个非常强大的基于jQuery的滚动条插件,不需要增加额外的css,几乎全浏览器兼容.ie6+,实现只需要一段代码,侵入性非常小,样式可完全自定义,支持触摸事件,可在触 ...

  9. C# String StringBuilder 区别

    这篇博客,纯参考.主要为了自己也复习一遍,过一下其中的原理. string aTest = "abc";//分配固定的内存大小 aTest += "ddd"; ...

  10. POJ [P2631] Roads in the North

    树的直径 树的直径求法: 任取一点u,找到树上距u最远的点s 找到树上距s点最远的点t,s->t的距离即为所求 #include <iostream> #include <cs ...