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. Linuxc - 标准输入流、标准输出流、标准错误流

    输入流stdin默认是键盘,输出流stdout默认是显示器,错误流stderr #include <stdio.h> int main() { printf("请输入选择的数字: ...

  2. Azure Powershell使用已有特殊化非托管磁盘创建ARM虚拟机

    生成已有特殊化非托管磁盘的方法主要有如下两种: 1.使用StorageExplorer存储管理工具,复制特殊化磁盘到一个新的容器下 2.New Portal中删除虚拟机,默认vhd文件会保留在存储账号 ...

  3. Python程序的执行方式

    Python代码有两种执行方式: 一.文件执行 二.交互器执行(推荐) 一.文件执行 1.用 notepad++ 或 Sublime Text,甚至 写字本创建一个文件. 2.比如:print('He ...

  4. 判断具有某个属性js、jQuery

    if(!rr.classList.contains('invalid')){ updateCount(i,-1);//更新tab数量 } /*if(!$(rr).hasClass('invalid') ...

  5. Linux指令--wget

    Linux系统中的wget是一个下载文件的工具,它用在命令行下.对于Linux用户是必不可少的工具,我们经常要下载一些软件或从远程服务器恢复备份到本地服务器.wget支持HTTP,HTTPS和FTP协 ...

  6. mysql主从配置主主配置

    一.     概述  MySQL从3.23.15版本以后提供数据库复制(replication)功能,利用该功能可以实现两个数据库同步.主从模式.互相备份模式的功能.本文档主要阐述了如何在linux系 ...

  7. android 通过getDimension,getDimensionPixelOffset和getDimensionPixelSize获取dimens.xml文件里面的变量值

    dimens.xml里写上三个变量: <dimen name="activity_vertical_margin1">16dp</dimen> <di ...

  8. Linq to SQL 中实现模糊查询

    list = list.Where(i => i.Name.Contains(empName)).ToList();

  9. Mysql 时间格式默认空串 '0000-00-00 00:00:00' select抛出异常的解决方法

    Mysql 时间格式默认插入值为空时,会以'0000-00-00 00:00:00'填充,这时如果select时会抛出SQLExecption如下: java.sql.SQLException: Va ...

  10. 获取sap登陆用户名的中文描述

    一.业务场景: 当通过MKPF-USNAM查找ADRP-NAME_LAST时,中间缺少一个表,即USR21.否则,MKPF-USNAM不能和ADRP-PERSNUMBER直接对等. 二.解决方法: D ...