iOS开发之蓝牙4.0 BLE开发

限时加码!20+主流AI编程工具免费用 购周边加赠Coding Plan Lite,Claude Code、Cursor等即刻畅享,学习进阶更高效! 阅读详情

原文链接: http://zh.5long.me/2015/bluetooth-for-iOS-developer/

前言

物联网时代的到来,智能硬件层出不穷。蓝牙低功耗技术(BLE,Bluetooth Low Energy)使得蓝牙4.0的应用越来越广泛,比如小米手环就是使用蓝牙4.0来传输数据。

在蓝牙4.0之前,iOS的蓝牙功能作为私有,开发者只能使用蓝牙开发联机游戏而不能和第三方蓝牙设备通信,当时要开发一个用iPhone通过蓝牙来控制硬件的APP是件非常复杂的事情。好在蓝牙4.0之后,苹果开放了蓝牙4.0开发接口。而TI公司也推出了两款蓝牙芯片:CC2540和CC2541。之后就涌现了一大批基于蓝牙4.0的硬件设备和APP。如Stick-N-Find和耐克数字运动手环 NIKE+。随后中国也出现了一大批类似设备,小米、华为都出了手环。

本人差不多也是在那时接触蓝牙4.0,做一个蓝牙通信的APP。在当时资料还不是那么丰富的情况下,整个开发过程也是个探索过程。由于最近又需要做一个蓝牙通信的APP,于是整理了之前写的代码,重新写了一份蓝牙通信的代码(之前写的他太乱了 : D)。

本文介绍iOS下开发蓝牙4.0通信程序的流程以及使用到相关的库。

相关资料

关键类

蓝牙4.0相关的组件为CoreBluetooth,所以要使用之前先引用’CoreBluetooth/CoreBluetooth.h’,其中有两个重要的类:

  • ‘CBCentralManager’:蓝牙的控制中心,通过它来获知蓝牙状态、扫描周边蓝牙设备、发起连接等。
  • ‘CBPeripheral’:和周边蓝牙设备对应的一个对象,每一个蓝牙设备对应一个CBPeripheral对象。通过它乡对应的蓝牙设备发送数据、读取数据、获取RSSI值等。

‘CBCentralManager’和’CBPeripheral’通过协议来交互,对应的两个协议为:

  • ‘CBCentralManagerDelegate’:CBCentralManager’的协议。
  • ‘CBPeripheralDelegate’:CBPeripheral’的协议。

比如当’CBCentralManager’发起扫描周边设备时,当发现了一个设备后就会调用CBCentralManagerDelegate’的’- (void)centralManager:(CBCentralManager*)central didDiscoverPeripheral:(CBPeripheral*)peripheral advertisementData:(NSDictionary*)advertisementData RSSI:(NSNumber*)RSSI’,通过实现该方法就可做相应地处理。

关于协议,请参考iOS开发入门教程之Objective-C · 协议(Protocols)

以下列出这两个协议常用的方法:

CBCentralManagerDelegate

@required
/*
本机设备(iPhone/iPad)蓝牙状态改变
*/
- (void)centralManagerDidUpdateState:(CBCentralManager *)central;

@optional
/*
扫描到了一个蓝牙设备peripheral
*/
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI;

/*
成功连接蓝牙设备peripheral
*/
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral;

/*
连接蓝牙设备peripheral失败
*/
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error;

/*
蓝牙设备peripheral已断开
*/
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error;

CBPeripheralDelegate

@optional
/*
蓝牙设备peripheral的RSS值改变
*/
- (void)peripheralDidUpdateRSSI:(CBPeripheral *)peripheral error:(NSError *)error NS_DEPRECATED(NA, NA, 5_0, 8_0);

/*
从peripheral成功读取到services
*/
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error;

/*
从peripheral的service成功读取到characteristics
*/
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error;

/*
从蓝牙设备peripheral接收到数据
*/
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error;

/*
乡蓝牙设备peripheral成功写入数据,写数据时需要以CBCharacteristicWriteWithResponse才会调用
*/
 - (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error;

蓝牙通信方式

蓝牙通信方式为service+characteristic,这和网络通信的IP+端口类似,service可理解为IP,characteristic可理解为端口号。servicecharacteristic都是一个数字编号,一般用16进制表示,如0xFFE00xFFE1

需要注意的是发送方和接收方必须是在相同的service+characteristic下才能正常发送和接收数据,如发送方在service0xFFE0characteristic0xFFE1下发送信息,接收方也应该在service0xFFE0characteristic0xFFE1下接收信息,否则是接收不到数据的。

一个蓝牙设备可以有多个service,一个service也可以有多个characteristic。不同的servicecharacteristic组合可形成多个通信通道。

还有一点需要提醒的是,由于苹果的限制,iOS SDK并没有提供获取蓝牙设备MAC地址的方法,所以一般还是不要用MAC,使用CBPeripheralUUIDname代替

蓝牙通信开发流程

在此就已经了解了iOS开发蓝牙通信所需要的库了,下面进入开发流程。蓝牙通信流程一般是:

  1. 初始化
  2. 扫描周边的蓝牙设备。
  3. 连接某一设备。
  4. 发送/接收数据。
  5. 断开连接

当然,所有这些操作都默认iPhone/iPad已开启蓝牙功能:D

初始化

初始化一个CBCentralManager

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

扫描周边的蓝牙设备

调用CBCentralManagerscanForPeripheralsWithServices就能扫描周边的设备。

[self.centralManager scanForPeripheralsWithServices:nil options:nil];

当扫描到一个蓝牙设备时,会调用CBCentralManagerDelegatecentralManager:(CBCentralManager *) didDiscoverPeripheral:(CBPeripheral *) advertisementData:(NSDictionary *) RSSI:(NSNumber *)方法,通过实现该协议方法,就可做相应地处理。

- (void)centralManager:(CBCentralManager*)central didDiscoverPeripheral:(CBPeripheral*)peripheral advertisementData:(NSDictionary*)advertisementData RSSI:(NSNumber*)RSSI
{
    NSLog(@"find new device:%@", peripheral.name);
}

连接某一设备

通过调用CBCentralManagerconnectPeripheral: options:方法就可向蓝牙设备发起连接,连接成功或失败会分别调用CBCentralManagerDelegatecentralManager:(CBCentralManager *) didConnectPeripheral:(CBPeripheral *)centralManager:(CBCentralManager *) didFailToConnectPeripheral:(CBPeripheral *) error:(NSError *)方法。

如果连接成功,还应该读取peripheralservicecharacteristic

[self.centralManager connectPeripheral:peripheral options:nil];

- (void)centralManager:(CBCentralManager*)central didConnectPeripheral:(CBPeripheral*)peripheral
{
    peripheral.delegate = self;
    [peripheral discoverServices:nil];
}

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
    [peripheral discoverCharacteristics:nil forService:service];
}

发送数据

发送数据只需要在指定的servicecharacteristic组合下发送即可,如果是以CBCharacteristicWriteWithResponse模式发送,发送完后还会调用CBPeripheralDelegateperipheral:(CBPeripheral *) didWriteValueForCharacteristic:(CBCharacteristic *) error:(NSError *),实现该协议方法可判断发送是否成功。以CBCharacteristicWriteWithoutResponse模式则不会有回调。

[peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];

- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
    if (error)
    {
        NSLog(@"%@", error);
    }
}

接收数据

当接收到数据后,CBCentralManager会调用协议CBCentralManagerDelegateperipheral:(CBPeripheral *) didUpdateValueForCharacteristic:(CBCharacteristic *) error:(NSError *)error方法,实现该方法就可获取接收到得数据。

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
    NSString *msg=[[NSString alloc] initWithData:characteristic.value  encoding:NSUTF8StringEncoding];
    NSLog(@"message:%@", msg);
}

断开连接

通过调用CBCentralManagercancelPeripheralConnection:即可断开蓝牙连接,成功断开后,会调用CBCentralManagerDelegate- (void)centralManager:(CBCentralManager *) didDisconnectPeripheral:(CBPeripheral *) error:(NSError *)方法。

[self.centralManager cancelPeripheralConnection:peripheral];

- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
}

结语

本文介绍了开发iOS下蓝牙通信APP的必要知识及开发流程,给出了一个iOS蓝牙通信的框架。关于各个函数的详细说明,还需要查阅官方文档。结合官方文档,相信读者可以开发出一个蓝牙通信的模型来。

ios蓝牙扫描指定的设备scanForPeripheralsWithServices(过滤掉其他设备,只留下自家设备) 文章目录1.扫描广播的service UUIDs是2803或是CB00的设备2.扫描广播的service UUIDs是2803的设备2.1 苹果手机蓝牙工具显示2.2 安卓手机蓝牙工具显示 1.扫描广播的service UUIDs是2803或是CB00的设备 NSString *str = @"2803"; CBUUID *uuid = [CBUUID UUIDWithString:str]; NSString *str1 = @"CB00"; CBUUID *uuid1 = [C 阅读详情

相关推荐

unity bluetooth android,Unity 实现蓝牙通信Arduino Bluetooth Plugin

Unity 实现蓝牙通信1.话不多说上效果,目前开发的是Android版本的蓝牙和带蓝牙电子设备连接,下图展示的蓝牙控制机器小车运动,由于上传完整视频会比较麻烦,所以选择截屏展示,望大家见谅。2.首先导入一个插件,插件官方地址 https://assetstore.unity.com/packages/tools/input-management/arduino-bluetooth-plug...

weixin_35225144的博客 6564

Unity安卓蓝牙插件

Unity安卓蓝牙插件,用以调用安卓蓝牙连接蓝牙模块并进行通讯。

iOS开发蓝牙4.0 -- BLE开发

前言—— 常用概念用语: MFI ( make for ipad ,iphone, itouch)  -------- 专们为苹果设备制作的设备 BLE (buletouch low energy)                  --------  蓝牙4.0设备因为低耗电,所以也叫做BLE peripheral                             

xinzhaoyu的博客 3080

蓝牙通信插件最新版Bluetooth LE for iOS tvOS and Android.unitypackage

2019年最新版本的Bluetooth LE for iOS tvOS and Android.unitypackage2.25。现在市面上都有BUG,只有我这个可以直接用哦!!!

ios蓝牙扫描指定的设备scanForPeripheralsWithServices

文章目录 0.苹果官方文档 1.扫描广播的service UUIDs是2803或是CB00的设备 2.扫描广播的service UUIDs是2803的设备 2.1 苹果手机蓝牙工具显示 2.2 安卓手机蓝牙工具显示 3.生成CBUUID的字符串不能随便写 4.options参数传了没用,还是可以搜到所有的设备 0.苹果官方文档 [myCentralManager scanForPeripheralsWithServices:nil options:nil]; If you specify nil .

feelinghappy的专栏 2184

iOS开发——为什么我的scanForPeripheralsWithServices根本不起作用

为什么CoreBluetooth里scanForPeripheralsWithServices不起作用?本文将告诉你答案!

Absorb Like A Sponge 8946

iOS开发 之 可穿戴设备 蓝牙4.0 BLE 开发

1 前言当前有越来越多的可穿戴设备使用了蓝牙4.0 BLE(Bluetooth Low Energy)。对于iOS开发而言,Apple之前专门推出CoreBluetooth的Framework来支持BLE开发。对于硬件开发有了解的朋友应该知道,在之前使用低版本的蓝牙的设备,要连接到iOS设备上,需要注册MFI,拥有MFI协议才能进行相应的开发。如果大家关注我之前对LEGO EV3的研究,就可以发现

Flood Sung的专栏 3万+

视频教程-iOS开发-全面解析iOS蓝牙BLE4.0开发-iOS

iOS开发-全面解析iOS蓝牙BLE4.0开发 Android研发工程师 ...

weixin_29647029的博客 461

iOS - 蓝牙BLE4.0开发

蓝牙BLE4.0开发

chaneylauok的博客 1629

iOS开发蓝牙4.0(BLE)与外设连接及收发数据的流程

苹果在iOS 6系统之后开始支持BLE 4.0,iPhone4s,iPod 5,iPad 3等之后的机型开始内嵌BLE4.0硬件,因此在开发前请先确认你的开发环境符合上述要求,并且苹果在BLE4.0之后,对外部的连接设备已经不再需要MFI认证了,当然你的外设肯定得要有蓝牙4.0模块。 开发BLE4.0的App,你需要在你的项目里面导入框架: 在需要使用到蓝牙的文件里面导入头文件,并且在你的...

Alexander Wei的博客 1万+

iOS蓝牙4.0(BLE)-开发

1 前言当前有越来越多的可穿戴设备使用了蓝牙4.0 BLE(Bluetooth Low Energy)。 对于iOS开发而言,Apple之前专门推出CoreBluetooth的Framework来支持BLE开发。 对于硬件开发有了解的朋友应该知道,在之前使用低版本的蓝牙的设备,要连接到iOS设备上,需要注册MFI,拥有MFI协议才能进行相应的开发。如果大家关注我之前对LEGO EV3的研究,就

jeikerxiao 1087

iOS开发蓝牙BLE4.0技术实践范例

蓝牙低功耗技术,即Bluetooth Low Energy(BLE),是蓝牙技术的一个子集,专为低功耗通信而设计。BLE自推出以来,一直是可穿戴设备和物联网应用中的关键技术之一。

weixin_42430341的博客 940

iOS蓝牙开发---CoreBluetooth[BLE 4.0] 初级篇[内附Demo地址]

一、蓝牙基础知识 (一)常见简称 1.MFI make for ipad ,iphone, itouch 专们为苹果设备制作的设备,开发使用ExternalAccessory 框架(认证流程貌似挺复杂的,而且对公司的资质要求较高),详见:关于MFi认证你所必须要知道的事情 2.BLE buletouch low ...

weixin_34025151的博客 127

IOS 蓝牙(BLE)4.0 的认知与开发

随着物联网的发展,有关蓝牙开发也是越来越多,以 IOS 的框架为核心,简述一下自己的经历对蓝牙的认知与开发过程。 1.蓝牙 框架的发展:gamekit -- > mutipeer --> coreBluetooth. 现在的蓝牙开发基本上都是以  coreBluetooth 来开发,其低功耗,应用广泛。 2.开发模式: 1) 中心者模式:  以手机作为中心来连接外部设备

清晰的轮廓 1043

IOS蓝牙4.0BLE开发一基本概念

一、IOS中关于蓝牙的框架有四个: (1)GameKit.framework 多用于游戏开发,仅限于ios设备之间的连接。 (2)MultipeerConnectivity.framework 这个就是ios设备之间互相传文件用的。 (3)ExternalAccessory.framework 这个框架可以用于和第三方蓝牙进行交互,但是必须是MFI(make for iphone,iPad,

Walden_tinghou的专栏 669

iOS开发(OC)——蓝牙BLE

iOS开发交流群:301058503 导入头文件 #import <CoreBluetooth/CoreBluetooth.h> 定义代理 CBCentralManagerDelegate,CBPeripheralDelegate 定义全局变量 @property (nonatomic,strong)CBCentralManager *central...

菜鸟历程 4095
上一篇: 用遗传算法求解TSP问题
下一篇: iOS开发之Objective-C(Swift)与JavaScript交互·WebViewJavascriptBridge使用篇
張行之
博客等级 码龄12年 91粉丝 30原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值