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. 2017-07-05 (whereis which find)

    whereis whereis 命令名 作用 搜索命令所在的路径以及帮助文档所在的位置 选项 -b 搜索命令所在的位置 -m 搜索帮助文档所在的位置 例子 whereis ls  查看ls命令所在的位 ...

  2. spring bean中子元素lookup-method和replaced-method

    lookup-method 示例: 步骤一:定义一个Car类 package org.hope.spring.bean.lookup; public class Car { private Strin ...

  3. Linux使用top与free命令查看CPU与内存使用情况

    top命令: 显示内容解释: 第一行top分别为:当前时间:系统运行天数:使用者个数:系统负载的平均值,后面的三个值分别为1分钟前.5分钟前.15分钟前进程的平均数,这个数值超过 CPU 数目时,说明 ...

  4. JDBC (二)

    1 使用JDBC进行批处理 当需要向数据库发送一批SQL语句的时候,应该避免向数据库一条条的发送执行,而应该采用JDBC的批处理机制,以提高执行效率. 实现批处理的方式一: Statement.add ...

  5. func_get_args  获取一个函数的所有参数

    func_get_args  获取一个函数的所有参数 {     $numargs = func_num_args(); //参数数量     echo "参数个数是: $numargs&l ...

  6. ie下常见的css兼容问题

    1.border-radius 边框圆角 IE8及以下浏览器不支持border-radius webkit引擎支持-webkit-borderradius 私有属性 mozilla Gecko引擎支持 ...

  7. CSS——类和ID选择器的区别

    1.相同点,可以应用在任何元素. 2.不同点,ID选择器只能在元素里只能分别引用,不能同时引用. 如: <style type="text/css">.stress{( ...

  8. Python之算法

    一.什么算法 算法:一个计算过程,解决问题的方法 二.时间复杂度 看代码:                                                               ...

  9. 强大的API测试工具Hitchhiker v0.9 基于UI的断言测试,回顾2017

    v0.9是Hitchhiker在2017农历年的最后一个版本,而起点正是刚过完2016农历年,农历2018即将到来,一年轮回,今天写点东西稍微回顾下hitchhiker的2017. 先还是说v0.9, ...

  10. sigmoid_cross_entropy_with_logits

    sigmoid_cross_entropy_with_logits 原创文章,请勿转载!!! 函数定义 def sigmoid_cross_entropy_with_logits(_sentinel= ...