现将创建蓝牙工程的要点总结一下,由于工程主要涉及中心模式,所以只总结中心模式的用法

1,引入CoreBluetooth.framework

2,实现蓝牙协议,如:

.h文件如下

@protocol CBCentralManagerDelegate;

@protocol CBPeripheralDelegate;

@interface ViewController : UIViewController <CBCentralManagerDelegate,CBPeripheralDelegate>

.m文件如下

#import "CoreBluetooth/CoreBluetooth.h"

另外还有代理部分请自行添加

3,下面是使蓝牙动起来的过程

3.1创建CBCentralManager实例

self.cbCentralMgr = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

设置代理,比如:

self.cbCentralMgr.delegate = self;

创建数组管理外设

self.peripheralArray = [NSMutableArray array];

3.2扫描周围的蓝牙

实际上周围的蓝牙如果可被发现,则会一直往外发送广告消息,中心设备就是通过接收这些消息来发现周围的蓝牙的

NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumbernumberWithBool:false],CBCentralManagerScanOptionAllowDuplicatesKey, nil];

[self.cbCentralMgr scanForPeripheralsWithServices:nil options:dic];

3.3发现一个蓝牙设备

也就是收到了一个周围的蓝牙发来的广告信息,这是CBCentralManager会通知代理来处理

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI

{

}

如果周围的蓝牙有多个,则这个方法会被调用多次,你可以通过tableView或其他的控件把这些周围的蓝牙的信息打印出来

3.4连接一个蓝牙

[self.cbCentralMgr connectPeripheral:peripheral options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]];

一个中心设备可以同时连接多个周围的蓝牙设备

当连接上某个蓝牙之后,CBCentralManager会通知代理处理

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

{

}

因为在后面我们要从外设蓝牙那边再获取一些信息,并与之通讯,这些过程会有一些事件可能要处理,所以要给这个外设设置代理,比如:

peripheral.delegate = self;

3.5查询蓝牙服务

[peripheral discoverServices:nil];

返回的蓝牙服务通知通过代理实现

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error

{

for (CBService* service in peripheral.services){

}

}

3.6查询服务所带的特征值

[peripheral discoverCharacteristics:nil forService:service];

返回的蓝牙特征值通知通过代理实现

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

{

for (CBCharacteristic * characteristic in service.characteristics) {

}

}

3.7给蓝牙发数据

[peripheral writeValue:data forCharacteristic:characteristictype:CBCharacteristicWriteWithResponse];

这时还会触发一个代理事件

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

{

}

3.8处理蓝牙发过来的数据

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

{

}

3.9 retrievePeripheralsWithIdentifiers 使用例子

-(IBAction) Retrieve:(id)Sender

{

[self.tvLog setText:@""];

NSMutableArray * Identifiers = [NSMutableArray array];

for (CBPeripheral * peripheral in self.peripheralArray) {

[Identifiers addObject:peripheral.identifier];

}

[self addLog:@"[self.cbCentralMgr retrievePeripheralsWithIdentifiers:self.PeripheralIdentifiers]"];

self.retrievePeripherals = [self.cbCentralMgr retrievePeripheralsWithIdentifiers:Identifiers];

for (CBPeripheral* peripheral in self.retrievePeripherals) {

[self addLog:[NSString stringWithFormat: @"%@ name:%@",peripheral,peripheral.name]];

}

[self.tableViewPeripheral reloadData];

}

3.10 retrieveConnectedPeripheralsWithServices 使用例子

-(IBAction) Retrieve:(id)Sender

{

[self.tvLog setText:@""];

NSMutableArray * services = [NSMutableArray array];

for (CBPeripheral * peripheral in self.peripheralArray) {

if (peripheral.isConnected) {

for (CBService *service in peripheral.services) {

[services addObject:service.UUID];

}

}

}

[self addLog:@"[self.cbCentralMgr retrieveConnectedPeripheralsWithServices:peripheral.services]"];

self.retrievePeripherals = [self.cbCentralMgrretrieveConnectedPeripheralsWithServices:services];

for (CBPeripheral* peripheral in self.retrievePeripherals) {

[self addLog:[NSString stringWithFormat: @"%@ name:%@",peripheral,peripheral.name]];

}

[self.tableViewPeripheral reloadData];

}

学习:http://blog.csdn.net/u010944926/article/details/20551879

iOS 蓝牙功能 bluetooth的更多相关文章

  1. iOS蓝牙BLE4.0通信功能

    概述 iOS蓝牙BLE4.0通信功能,最近刚学的苹果,为了实现蓝牙门锁的项目,找了一天学习了下蓝牙的原理,亲手测试了一次蓝牙的通信功能,结果成功了,那么就把我学习的东西分享一下. 详细 代码下载:ht ...

  2. https://github.com/coolnameismy/BabyBluetooth github上的一个ios 蓝牙4.0的库并带文档和教程

    The easiest way to use Bluetooth (BLE )in ios,even bady can use. 简单易用的蓝牙库,基于CoreBluetooth的封装,并兼容ios和 ...

  3. iOS蓝牙BLE开发

    蓝牙是一个标准的无线通讯协议,具有设备成本低.传输距离近和功耗低等特点,被广泛的应用在多种场合.蓝牙一般分为传统蓝牙和BLE两种模式:传统蓝牙可以传输音频等较大数据量,距离近.功耗相对大:而BLE则用 ...

  4. iOS蓝牙原生封装,助力智能硬件开发

    代码地址如下:http://www.demodashi.com/demo/12010.html 人工智能自1956年提出以来,一直默默无闻,近年来人工智能的发展得到重视逐渐发展起步,智能硬件.智能手环 ...

  5. iOS蓝牙开发(二)蓝牙相关基础知识

    原文链接: http://liuyanwei.jumppo.com/2015/07/17/ios-BLE-1.html iOS蓝牙开发(一)蓝牙相关基础知识: 蓝牙常见名称和缩写 MFI ====== ...

  6. Android使用BLE(低功耗蓝牙,Bluetooth Low Energy)

    背景 在学习BLE的过程中,积累了一些心得的DEMO,放到Github,形成本文.感兴趣的同学可以下载到源代码. github: https://github.com/vir56k/bluetooth ...

  7. 记录使用微信小程序的NFC和蓝牙功能读取15693芯片的开发历程

    开发目标: (1) 对于Android手机,直接通过微信小程序调用手机的NFC功能,对15693协议的芯片进行读写操作: (2)对于苹果手机(及没有NFC模块的手机),通过微信小程序的蓝牙功能连接到蓝 ...

  8. 经测试稳定可用的蓝牙链接通信Demo,记录过程中遇到的问题的思考和解决办法,并整理后给出一个Utils类可以简单调用来实现蓝牙功能

    说明:这是本人在蓝牙开发过程中遇到过的问题记录和分析,以及解决办法. 在研究过程中,许多的前人给出的解决方案和思路指导对我相当有帮助,但并非都是可采取的解决方法, 经过本人对这些方法的测试和使用过后, ...

  9. iOS蓝牙APP常驻后台

    iOS蓝牙类APP常驻后台的实现方法,经过在苹果开发者论坛询问,以及查看苹果开发者文档,最后得出正确的方法为: 1.设置plist,蓝牙权限 2.到target-capabilities-backgr ...

随机推荐

  1. dxFlowChart运行时调出编辑器

    dxFlowChart运行时调出编辑器 uses dxFcEdit; procedure TForm1.Button1Click(Sender: TObject);var f: TFChartEdit ...

  2. telnet 查看端口是否可访问

      1. 首先为什么要写这篇文章 说到为什么还得从DNS服务器说起.我在我的电脑上安装了DNS服务器,但是用网络去访问还怎么都访问都不上去.于是我就打开dos窗口,用ping命令查看是否可以ping( ...

  3. ora01940 无法删除当前连接的用户

    我用system这个用户登录oracle,想删除掉一个自己创建的用户user,在网上找到的方法都是说先查找到该用户连接的会话select username,sid,serial# from v$ses ...

  4. git reset,git checkout区别

    https://www.cnblogs.com/houpeiyong/p/5890748.html git reset  HEAD~1  将HEAD指向倒数第二次提交,最后一次提交删除,文件还在 gi ...

  5. 如何把自己的代码发布到npmjs(npm publish)

    来源: https://www.cnblogs.com/calamus/p/8384318.html

  6. [分享]在ubuntu9.10下实现开机自动登录并运行自己的图形程序

    在ubuntu9.10下实现开机自动登录并运行自己的图形界面程序(本人接触ubuntu时日不长,文中一些说法难免有错误和疏漏之处,还请大家不吝批评指正.)实现步骤分以下三大步:1. 实现ubuntu文 ...

  7. 编译和安装在Windows上橡胶树 (Compiling and Installing Yate on Windows)

    编译和安装在Windows上橡胶树     有两种方法来安装橡胶树下窗口: 下载并安装橡胶树 下载后页面设置. 另一个选择是下载橡胶树从SVN并编译它. 内容 ( 隐藏] 1 安装使用设置橡胶树 2  ...

  8. Android学习(二十三)SubMenu 子菜单

    一.SubMenu子菜单 和功能菜单相似,但是可以添加子菜单. 二.实现步骤: 1.通过onCreateOptionsMenu方法创建子菜单,可以通过代码动态创建,也可以通过xml进行创建. 2.通过 ...

  9. KeePass 不能复制账号密码

    最近一段时间发现 KeePass 不能复制账号密码了, 程序也没有任何错误提示, 没有办法定位原因, 这个就难办了. 网上搜索后发现这个帖子 Can't paste in gnome-terminal ...

  10. iOS应用程序开发之内购

    内购简介 配置iTunes Connect iOS客户端开发工作 一.内购简介 1⃣️通过苹果应用程序商店有三种主要赚钱的方式: –直接收费(与国内大部分用户的消费习惯相悖,如果直接收费,不要设置为6 ...