推荐阅读文章:http://blog.csdn.net/pony_maggie/article/details/26740237框架
1、前言性能
CoreBluetooth框架的核心实际上是两个东西,peripheral和central, 能够理解成外设和中心。对应他们分别有一组相关的API和类,以下图所示:spa
2、Core Bluetooth 的基本常识:
一、 每一个蓝牙设备都是经过服务和特征来展现本身
一个设备必然包含一个或多给服务,每一个服务下面又包含多个特征
二、特征是与外界交互的最小单位
好比说,一台蓝牙设备,用特征A来描述本身的出场信息,用特征B来描述本身的收发数据
三、服务和特征都是用UUID来惟一标识的,经过UUID就能区别不一样的服务和特征
四、设备里面的服务和特征的功能,都是由蓝牙设备硬件厂商提供,好比哪些用来交互(读写),哪些用来可获取模块信息(只读)等。.net
3、Core Bluetooth的开发步骤:代理
一、创建中心设备
二、扫描外设(Discover Peripheral)
三、链接外设(connect Peripheral)
四、扫描外设中的服务于特征(Discover Services and Characteristics)
五、利用特征与外设作数据交互(Explore and interact)
六、断开链接(Disconnect)blog
创建中心设备:ip
//1、建立中心设备开发
CBCentralManager *mgr = [[CBCentralManager alloc] init];get
//设置代理it
mgr.delegate = self;
//2、利用中心设备扫描外部设备
/*
若是指定数据NSArray 只能扫描指定的设备
*/
[mgr scanForPeripheralsWithServices:nil options:nil];
self.mgr = mgr;
#pragma mark - CBCentralManagerDelegate
//实现代理,扫描到外设,保存外设
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
//保留扫描到的外设
if (![self.peripherals containsObject:peripheral])
{
[self.peripherals addObject:peripheral];
}
}
/**
* 模拟点击链接全部的外设 链接全部外设
*/
- (void)start
{
for (CBPeripheral *peripheral in self.peripherals) {
peripheral.delegate = self;
[self.mgr connectPeripheral:peripheral options:nil];
}
}
/**
* 链接外设成功
*
* @param central
* @param peripheral
*/
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
//1、扫描外设中的服务(扫描全部服务)
[peripheral discoverServices:nil];
}
/**
* 链接外设失败
*
* @param central
* @param peripheral
* @param error
*/
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
}
#pragma mark - CBPeripheralDelegate
/**
* 只要扫描到服务就会调用
*
* @param peripheral peripheral
* @param error error
*/
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
//获取外设中全部扫描的获得的服务
NSArray *services = peripheral.services;
for (CBService *service in services)
{
//能够过滤不须要的服务
if ([service.UUID.UUIDString isEqualToString:@"989589595995"]) return;
//从须要的服务中查找须要的特征
//从peripheral中的service中扫描特征
[peripheral discoverCharacteristics:nil forService:service];
}
}
/**
* 只要扫描到特征就会调用
*
* @param peripheral 外设
* @param service 服务
* @param error 错误信息
*/
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
NSArray *characteristics = service.characteristics;
//遍历特征,拿到须要的特征处理
for (CBCharacteristic *characteristic in characteristics)
{
if ([characteristic.UUID.UUIDString isEqualToString:@"9996996969"])
{
}
}
}
4、蓝牙的现状
一、绝大多数只能手机支持蓝牙4.0(BLE)
二、蓝牙芯片发展迅速,在性能和效率方面都是有很大的提升,且不断变得更小更便宜
三、iBeacon + 蓝牙(BLE),前途一片光明
应用之一:室内导航
Estimote公司为iBeacon提供基站
3个Estimote公司为iBeacon预购价格为99美圆
Estimote公司推出的iBeacon基站的最远出书距离为50m,最佳距离是10m之内
一块纽扣电池就能为一个iBeacon基站提供长达2年的使用寿命,并且是在设备不断对外发射信号的状况下;