IOS蓝牙项目总结
常见的蓝牙标准有2.0和4.0。
特点 2.0 1.适用于数据量比较大得传输,比如音乐、语音
2.IOS开发中,要求设备是经过MFI认证4.0 1.适用于实时性比较高的数据传输,比如遥控类的鼠标、键盘,传感设备的心跳计、血压计
2.功耗低,距离短,轻量级注意:
一般我们说的蓝牙4.0都支持2.0和4.0,只是我们开发过程中只负责4.0标准的部分。
一、基本知识
1.CoreBluetooth框架是ble4.0开发使用的框架
2.CoreBluetooth主要内容有两个:peripheral(外设模式)和central(中心模式)。现在我写的是central内容。
二、外设模式关键操作
1.创建中心角色(centralManger)
2.扫描外设(peripheral)
注意:
该步骤需确认centralManager.state==CBCentralManagerStatePoweredOn
3.持有并连接外设(connect)
4.扫描外设的服务(service)
注意:该步骤需确认didConnectPeripheral
方法调用成功
5.扫描服务的特征(characteristic)
注意:该步骤需要确认didDiscoverServices
方法回调成功
6.操作特征
注意:该操作需确认didDiscoverCharacteristicsForService
成功回调
6.1.重新读取特征值(read)
6.2.往特征里写入值(write)
6.3.订阅特征(notifying)
6.4.扫描描述(descriptor)
7.断开连接,停止扫描
三、代码
#import "BLETool.h"
#import <CoreBluetooth/CoreBluetooth.h>
@interface BLETool()<CBCentralManagerDelegate,CBPeripheralDelegate>
@property (strong, nonatomic) CBCentralManager *centralManager;
@property (strong, nonatomic) CBPeripheral *peripheral;
@end
@implementation BLETool
- (instancetype)init{
if (self = [super init]) {
//1.创建中心管理角色。
/**
queue为nil表示默认主线程
*/
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
return self;
}
/**
* -- 必须实现的代理,用来返回创建的centralManager的状态。
* -- 注意:必须确认当前是CBCentralManagerStatePoweredOn状态才可以调用扫描外设的方法:
scanForPeripheralsWithServices
*/
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
switch (central.state) {
case CBCentralManagerStateUnknown:
NSLog(@">>>CBCentralManagerStateUnknown");
break;
case CBCentralManagerStateResetting:
NSLog(@">>>CBCentralManagerStateResetting");
break;
case CBCentralManagerStateUnsupported:
NSLog(@">>>CBCentralManagerStateUnsupported");
break;
case CBCentralManagerStateUnauthorized:
NSLog(@">>>CBCentralManagerStateUnauthorized");
break;
case CBCentralManagerStatePoweredOff:
NSLog(@">>>CBCentralManagerStatePoweredOff");
break;
case CBCentralManagerStatePoweredOn:
{
NSLog(@">>>CBCentralManagerStatePoweredOn");
//2.开始扫描周围的外设。
/*
-- 两个参数为Nil表示默认扫描所有可见蓝牙设备。
-- 注意:第一个参数我一开始以为是扫描指定外设的,那么使用外设的identifier就可以了。后来发现不是,这个参数是用来扫描有指定服务的外设。然后有些外设的服务是相同的,比如都有FFF5服务,那么都会发现;而有些外设的服务是不可见的,就会扫描不到设备。
-- 成功扫描到外设后调用didDiscoverPeripheral
*/
[self.centralManager scanForPeripheralsWithServices:nil options:nil];
}
break;
default:
break;
}
}
#pragma mark 发现外设
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI{
if ([peripheral.name isEqualToString:@"TimesBasy-BLE"]||[peripheral.name isEqualToString:@"TimesBaby-BLE"]) {
//注意:想要对指定peripheral进行后续操作,一定要保存这个外设对象。否则centralManager会认为你对指定peripheral不感兴趣,这样你即不能再扫描到这个指定peripheral,也不能对他进行后续操作(比如回调didConnectPeripheral)
self.peripheral = peripheral;
//3.连接外设
/**
-- 一个中心管理角色可以连接多个外设,但是一个外设只能被一个角色连接,外设被连接后就不能能再被扫描到
-- 连接成功回调didConnectPeripheral,失败回调didFailToConnectPeripheral,取消连接回调didDisconnectPeripheral
*/
[self.centralManager connectPeripheral:peripheral options:nil];
}
}
#pragma mark 连接外设--成功
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
//连接成功后停止扫描,节省内存
[central stopScan];
peripheral.delegate = self;
//4.扫描外设的服务
/**
-- 外设的服务、特征、描述等方法是CBPeripheralDelegate的内容,所以要先设置代理peripheral.delegate = self
-- 参数表示你关心的服务的UUID,比如我关心的是"FFF0",参数就可以为@[[CBUUID UUIDWithString:@"FFF0"]].那么didDiscoverServices方法回调内容就只有这两个UUID的服务,不会有其他多余的内容,提高效率。nil表示扫描所有服务
-- 成功发现服务,回调didDiscoverServices
*/
[peripheral discoverServices:nil];
}
#pragma mark 连接外设——失败
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
}
#pragma mark 取消与外设的连接回调
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
}
#pragma mark 发现服务回调
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
for (CBService *service in peripheral.services) {
//5.扫描指定服务的特征
/**
-- 第一个参数是数组,表示扫描指定服务下那些我关心的特征的UUID。
-- 成功扫描到特征后回调didDiscoverCharacteristicsForService
*/
[peripheral discoverCharacteristics:nil forService:service];
}
}
#pragma mark 发现特征回调
/**
-- 发现特征后,可以根据特征的properties进行:读readValueForCharacteristic、写writeValue、订阅通知setNotifyValue、扫描特征的描述discoverDescriptorsForCharacteristic。
-- 注意:实际开发中根据文档来确定对指定characteristic的操作。因为可能你得蓝牙设备的特征的properties不在下面之列。比如properties = 0x18
说明:我注视的那些不叫重要,其他的我也不知道
CBCharacteristicPropertyBroadcast = 0x01,
//允许读
CBCharacteristicPropertyRead = 0x02,
//允许写,但不会回应(不会调用didWriteValueForCharacteristic)
CBCharacteristicPropertyWriteWithoutResponse = 0x04,
//允许写,但会回应
CBCharacteristicPropertyWrite = 0x08,
//允许通知
CBCharacteristicPropertyNotify = 0x10,
//允许通知。和上面分属两种不同的通知类型
CBCharacteristicPropertyIndicate = 0x20,
CBCharacteristicPropertyAuthenticatedSignedWrites = 0x40,
CBCharacteristicPropertyExtendedProperties = 0x80,
CBCharacteristicPropertyNotifyEncryptionRequired NS_ENUM_AVAILABLE(NA, 6_0) = 0x100,
CBCharacteristicPropertyIndicateEncryptionRequired
*/
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
//一般开发时我们都应该有蓝牙文档的,需要读、写、订阅的特征都说说ing,所以直接根据对应的特征的UUID操作就可以,不用根据properties。
for (CBCharacteristic *characteristic in service.characteristics) {
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"FFF0"]]) {
//读。重新获取characteristic的值
/**
-- 阅读文档查看需要对该特征的操作
-- 读取成功回调didUpdateValueForCharacteristic
*/
[peripheral readValueForCharacteristic:characteristic];
}
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"FFF1"]]) {
//写。
/**
-- 阅读文档查看需要对该特征的操作
-- type:CBCharacteristicWriteWithResponse表示写入成功会回调didWriteValueForCharacteristic
*/
NSData *data = [NSData new];
[peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
}
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"FFF5"]]) {
//订阅通知
/**
-- 通知一般在蓝牙设备状态变化时会发出,比如蓝牙音箱按了上一首或者调整了音量,如果这时候对应的特征被订阅,那么app就可能收到通知
-- 阅读文档查看需要对该特征的操作
-- 订阅成功后回调didUpdateNotificationStateForCharacteristic
-- 订阅后characteristic的值发生变化会发送通知到didUpdateValueForCharacteristic
-- 取消订阅:设置setNotifyValue为NO
*/
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
}
//扫描描述
/**
-- 进一步提供characteristic的值的相关信息。(因为我项目里没有的特征没有进一步描述,所以我也不怎么理解)
-- 当发现characteristic有descriptor,回调didDiscoverDescriptorsForCharacteristic
*/
[peripheral discoverDescriptorsForCharacteristic:characteristic];
}
}
#pragma mark 数据写入特征回调
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
}
#pragma mark 获取特征的值
/**
-- peripheral调用readValueForCharacteristic成功后会回调该方法
-- peripheral调用setNotifyValue后,特征发出通知也会调用该方法
*/
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
}
#pragma mark 订阅通知回调
- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
}
#pragma mark 发现descriptors回调
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
}
#pragma mark 断开连接
- (void)disConnectPeripheral{
/**
-- 断开连接后回调didDisconnectPeripheral
-- 注意断开后如果要重新扫描这个外设,需要重新调用[self.centralManager scanForPeripheralsWithServices:nil options:nil];
*/
[self.centralManager cancelPeripheralConnection:self.peripheral];
}
#pragma mark 停止扫描外设
- (void)stopScanPeripheral{
[self.centralManager stopScan];
}
@end
IOS蓝牙项目总结的更多相关文章
- iOS 蓝牙开发(四)BabyBluetooth蓝牙库介绍(转)
转载自:http://www.cocoachina.com/ios/20151106/14072.html 原文作者:刘彦玮 BabyBluetooth 是一个最简单易用的蓝牙库,基于CoreBlue ...
- IOS 蓝牙相关-BabyBluetooth蓝牙库介绍(4)
BabyBluetooth 是一个最简单易用的蓝牙库,基于CoreBluetooth的封装,并兼容ios和mac osx. 特色: 基于原生CoreBluetooth框架封装的轻量级的开源库,可以帮你 ...
- iOS蓝牙中的进制转换
Bluetooth4.0.jpg 最近在忙一个蓝牙项目,在处理蓝牙数据的时候,经常遇到进制之间的转换,蓝牙处理的是16进制(NSData),而我们习惯的计数方式是10进制,为了节省空间,蓝牙也会把16 ...
- ios蓝牙开发(四)BabyBluetooth蓝牙库
BabyBluetooth 是一个最简单易用的蓝牙库,基于CoreBluetooth的封装,并兼容ios和mac osx. 特色: 基于原生CoreBluetooth框架封装的轻量级的开源库,可以帮你 ...
- https://github.com/coolnameismy/BabyBluetooth github上的一个ios 蓝牙4.0的库并带文档和教程
The easiest way to use Bluetooth (BLE )in ios,even bady can use. 简单易用的蓝牙库,基于CoreBluetooth的封装,并兼容ios和 ...
- iOS蓝牙开发
蓝牙常见名称和缩写 MFI ======= make for ipad ,iphone, itouch 专们为苹果设备制作的设备 BLE ==== buletouch low energy,蓝牙4.0 ...
- OpenDigg iOS开源项目月报201704
由OpenDigg 出品的iOS开源项目月报第一期来啦.我们的iOS开源月报集合了OpenDigg一个月来新收录的优质的iOS开源项目,方便iOS开发人员便捷的找到自己需要的项目工具等. Transi ...
- iOS蓝牙BLE4.0通信功能
概述 iOS蓝牙BLE4.0通信功能,最近刚学的苹果,为了实现蓝牙门锁的项目,找了一天学习了下蓝牙的原理,亲手测试了一次蓝牙的通信功能,结果成功了,那么就把我学习的东西分享一下. 详细 代码下载:ht ...
- iOS开源项目周报0323
由OpenDigg 出品的iOS开源项目周报第十三期来啦.我们的iOS开源周报集合了OpenDigg一周来新收录的优质的iOS开源项目,方便iOS开发人员便捷的找到自己需要的项目工具等. CHIPag ...
随机推荐
- jquery19 ajax()
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- Java开发常用的代码片段
1. 字符串有整型的相互转换 String a = String.valueOf(2); //integer to numeric string int i = Integer.parseInt ...
- [ Java ] [ Spring ] [ Spring MVC ] Tutorial
中文 Spring 教學 http://hatemegalaxy.blogspot.tw/2014/09/spring-framework-useful-tutorials-for.html 英文 S ...
- CSS 文本样式
1. 文本样式 text <!--文本颜色color --> .text {color:red;} 2. 文本对齐方式 text-align <style> .text ...
- PHP生成二维码方法
<?php //先下载一份phpqrcode类,下载地址http://down.51cto.com/data/780947require_once("phpqrcode/phpqrco ...
- java 参数
-Xmx:size java最大堆内存 -Xms:size 初始化内存 -Xmn:size 年轻带堆大小 -XX:NewSize=size 年轻带的大小 -XX:NewRatio=ratio 年轻带和 ...
- 微信小程序从零开始开发步骤(二)创建小程序页面
上一章注册完小程序,添加新建的项目,大致的准备开发已经完成,本章要分享的是 要创建一个简单的页面了,创建小程序页面的具体几个步骤: 1. 在pages 中添加一个目录 选中page,右击鼠标,从硬盘打 ...
- 7lession-基础数据使用介绍
1.数值 这个使用比较简单 a = 1 b = 3.2 c = 12.5+4j d = 20L 2.字符串 代码 s = "hello world,i am comming" pr ...
- 19,tuple多元数组
#include <iostream> #include <tuple> using namespace std; void main() { char ch = 'a'; ; ...
- 将二级目录下的文件合并成一个文件的Python小脚本
这个小程序的目的是将二级目录下的文件全部合并成一个文件(其实几级目录都可以,只要做少许改动) #coding:utf8 import sys, os def process(path): new_fi ...