前言:

自己做的项目里面有这么一个功能,总结归纳一下。

先导入必要的框架  CoreBluetooth.framework

在要用到蓝牙的文件里面导入以下头文件

#import <CoreBluetooth/CoreBluetooth.h>
#import<CoreBluetooth/CBService.h>

接着需要实现两个协议,CBCentralManagerDelegate ,CBPeripheralDelegate

1.先实例化

CBCentralManager

[self cMgr];
- (CBCentralManager *)cMgr
{
if (!_cMgr) {
_cMgr = [[CBCentralManager alloc] initWithDelegate:self
queue:dispatch_get_main_queue() options:nil];
}
return _cMgr;
}

判断一下设备的蓝牙是否打开

 if (self.cMgr.state == CBCentralManagerStatePoweredOn) {}

开始扫描蓝牙设备

[self.cMgr scanForPeripheralsWithServices:nil // 通过某些服务筛选外设
options:nil]; // dict,条件

-   (void)centralManagerDidUpdateState:(CBCentralManager *)central

这个方法主要是来检查IOS设备的蓝牙硬件的状态的,比如说你的设备不支持蓝牙4.0,

如果周围有正在广播的外设,且被扫描到了的话会触发

- (void)centralManager:(CBCentralManager *)central // 中心管理者

 didDiscoverPeripheral:(CBPeripheral *)peripheral // 外设

     advertisementData:(NSDictionary *)advertisementData // 外设携带的数据

                  RSSI:(NSNumber *)RSSI

在这个方法里面,可以对扫描到的设备进行筛选出自己想要的目标外设,进行连接

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

连接成功/失败/丢失连接的话会触发相应的

- (void)centralManager:(CBCentralManager *)central // 中心管理者

  didConnectPeripheral:(CBPeripheral *)peripheral

- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error

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

连接已经成功,我们就该考虑是不是要停止中心管理设备的扫描动作了,要不然在你和已经连接好的外设进行数据沟通时,如果又有一个外设进行广播且符合你的连接条件,那么你的IOS设备就会也去连接这个设备(因为IOS BLE4.0是支持一对多连接的),导致数据的混乱。所以我们在这个方法里面停止扫描动作:

[self.cMgr stopScan];

接下来是让我们的第二个实例对像出场了  CBPeripheral

连接成功后 调

[self.peripheral discoverServices:nil];

可以进一步了解到Serverce里面的Characteristic

之后会回调

// 发现外设服务里的特征的时候调用的代理方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error

接着我们可以在这个方法里面继续了解到某个CBService 里面的特征值UUID:

就会回调下面的方法:

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

接下来就是一些对某个特征值的读写操作了,当然我们首先要找到我们需要写数据的特征值UUID了,这里我们可以简单地通过循环来找出外设含有的某个特征值UUID:

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
if (error) {
NSLog(@"%s, line = %d, error = %@", __FUNCTION__, __LINE__, error.localizedDescription);
NSLog(@"%@",error.localizedDescription);
return;
}
for (CBCharacteristic *cha in service.characteristics) {
if ([[cha UUID] isEqual:[CBUUID UUIDWithString:@"FFE4"]])
{
[peripheral setNotifyValue:YES forCharacteristic:cha];
}
else if ([[cha UUID] isEqual:[CBUUID UUIDWithString:@"FFE9"]])
{ self.sendCharacteristic = cha;
[self dealWithCmd];
} }
}

// 更新特征的value的时候会调用

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

// 更新特征的value的时候会调用
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
//接收到数据的话,解析数据,根据sign判断
if ([[characteristic UUID] isEqual:[CBUUID UUIDWithString:@"FFE4"]]) {
NSLog(@"更新特征%@",characteristic.value);

}
// 更新特征的描述的值的时候会调用
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForDescriptor:(CBDescriptor *)descriptor error:(NSError *)error
{
NSLog(@"%s, line = %d", __FUNCTION__, __LINE__); [peripheral readValueForDescriptor:descriptor];
} // 发现外设的特征的描述数组
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(nonnull CBCharacteristic *)characteristic error:(nullable NSError *)error
{
NSLog(@"%s, line = %d", __FUNCTION__, __LINE__); // 在此处读取描述即可
for (CBDescriptor *descriptor in characteristic.descriptors) {
// 它会触发
[peripheral readValueForDescriptor:descriptor];
}
}

IOS BLE蓝牙4.0的更多相关文章

  1. Android ble 蓝牙4.0 总结

    本文介绍Android ble 蓝牙4.0,也就是说API level >= 18,且支持蓝牙4.0的手机才可以使用,如果手机系统版本API level < 18,也是用不了蓝牙4.0的哦 ...

  2. Android ble 蓝牙4.0 总结一

    本文介绍Android ble 蓝牙4.0,也就是说API level >= 18,且支持蓝牙4.0的手机才可以使用,如果手机系统版本API level < 18,也是用不了蓝牙4.0的哦 ...

  3. Android5.0(Lollipop) BLE蓝牙4.0+浅析demo连接(三)

    作者:Bgwan链接:https://zhuanlan.zhihu.com/p/23363591来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. Android5.0(L ...

  4. Android5.0(Lollipop) BLE蓝牙4.0+浅析code(二)

    作者:Bgwan链接:https://zhuanlan.zhihu.com/p/23347612来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. Android5.0(L ...

  5. android5.0 BLE 蓝牙4.0+浅析demo搜索(一)

    作者:Bgwan链接:https://zhuanlan.zhihu.com/p/23341414来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 作者:Bgwan 莳萝花 ...

  6. Android5.0(Lollipop) BLE蓝牙4.0+浅析概念(四)

    作者:Bgwan链接:https://zhuanlan.zhihu.com/p/23679793来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 置顶:此文转载CSDN博 ...

  7. 蓝牙BLE: 蓝牙4.0 BLE广播数据解析(转)

    BLE 设备工作的第一步就是向外广播数据.广播数据中带有设备相关的信息.本文主要说一下 BLE 的广播中的数据的规范以及广播包的解析. 1. 广播模式 BLE 中有两种角色 Central 和 Per ...

  8. iOS中 蓝牙2.0详解/ios蓝牙设备详解 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 整体布局如下:     程序结构如右图: 每日更新关注:http://weibo.com/hanjunqiang  ...

  9. iOS蓝牙4.0协议简单介绍

    iOS开发蓝牙4.0的框架是CoreBluetooth,本文主要介绍CoreBluetooth的使用,关于本文中的代码片段大多来自github上的一个demo,地址是myz1104/Bluetooth ...

随机推荐

  1. mybatis中 ${}和#取值小记(Parameter index out of range)

    mybatis mapperxml文件中有两种取值法.${}和#{} $的是原样,#的是取值并转成指定?#{ele1,jdbcType=VARCHAR} 有个坑, 错误的写法 <if test= ...

  2. eclipse遇到不会部署的情况

    1.先看下右下角有没有在进行的进程,例如validating  验证中.那就关闭验证的选项 2.看下problem栏有没有问题.会导致building不了.

  3. 只需一点小修改,HTC Vive画面会更清晰锐利

    这里要先谢谢@NB81rkd0qB,他的那个帖子里其实很多碰到的问题都可以解决,但是目前有点乱,所以我这里斗胆整理一下,希望能帮助一下朋友们.第一步:我们要找到[steamvr.vrsettings] ...

  4. 重温Javascript第一章

    一.script标签 script标签有6个属性,其中一个废弃,五个可选. 按照传统的写法,<script>的标签都是放在<head>元素中,但是在<head>中包 ...

  5. linux 挂载光盘:mount: you must specify the filesystem type

    尝试挂载光盘镜像时出现mount: you must specify the filesystem type 使用-t auto -t iso9660 或不加参数都搞不定,最后在以下链接找到解决办法: ...

  6. 从下往上看--新皮层资料的读后感 第四部分 来自神经元的设计-perceptron 感知机

    搬地方了,其他的部分看知乎:https://zhuanlan.zhihu.com/p/22114481 直到50年代,perceptron被Frank Rosenblatt搞了出来.perceptro ...

  7. 纯CSS tooltip 提示

    一般的tooltip,使用超链接的title,或者是css+javascript生成. 如果页面布局合理,样式结构清晰,可以使用纯CSS的提示. demo如下: a.tooltip { positio ...

  8. JavaScript toLocaleString() 方法

    JavaScript toLocaleString() 方法 JavaScript Array 对象 定义和用法 把数组转换为本地字符串. 语法 arrayObject.toLocaleString( ...

  9. css3放大效果

    参考案例: http://www.web-designers.cn/ http://www.harmay.com/ 鼠标经过图片,图片放大. 1.html: <body> <div& ...

  10. chrome 扩展包 postman 的安装

    由于chrome网上应用不能使用.添加扩展程序,需要其他的办法. 1.下载postman安装包.下载地址 2.这一步按照这个下载包中的方法,也可以,可以忽略其错误. 先解压出crx,使用两个办法,使用 ...