1建立中心角色

1
2
3
#import <CoreBluetooth/CoreBluetooth.h> 
CBCentralManager *manager; 
manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; 

2扫描外设(discover)

[manager scanForPeripheralsWithServices:nil options:options]; 

3连接外设(connect)

1
2
3
4
5
6
7
8
9
10
11
12
13
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI 
{
        if([peripheral.name  isEqualToString:BLE_SERVICE_NAME]){
                [self connect:peripheral];
        }
s); 
}       
 
-(BOOL)connect:(CBPeripheral *)peripheral{
        self.manager.delegate = self;
        [self.manager connectPeripheral:peripheral
                                options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]];
}

  

4扫描外设中的服务和特征(discover)

1
2
3
4
5
6
7
8
9
10
11
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral 
       
    NSLog(@"Did connect to peripheral: %@", peripheral); 
    _testPeripheral = peripheral; 
       
    [peripheral setDelegate:self];  <br>//查找服务
    [peripheral discoverServices:nil]; 
       
       
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error 
   
       
    NSLog(@"didDiscoverServices"); 
       
    if (error) 
    
        NSLog(@"Discovered services for %@ with error: %@", peripheral.name, [error localizedDescription]); 
           
        if ([self.delegate respondsToSelector:@selector(DidNotifyFailConnectService:withPeripheral:error:)]) 
            [self.delegate DidNotifyFailConnectService:nil withPeripheral:nil error:nil]; 
           
        return
    
       
   
    for (CBService *service in peripheral.services) 
    
         //发现服务
        if ([service.UUID isEqual:[CBUUID UUIDWithString:UUIDSTR_ISSC_PROPRIETARY_SERVICE]]) 
        
            NSLog(@"Service found with UUID: %@", service.UUID);  <br>//查找特征
            [peripheral discoverCharacteristics:nil forService:service]; 
            break
        
           
           
    
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
     
    if (error)
    {
        NSLog(@"Discovered characteristics for %@ with error: %@", service.UUID, [error localizedDescription]);
         
        [self error];
        return;
    }
     
    NSLog(@"服务:%@",service.UUID);
    for (CBCharacteristic *characteristic in service.characteristics)
    {
       //发现特征
            if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"xxxxxxx"]]) {
                NSLog(@"监听:%@",characteristic);<br>//监听特征
                [self.peripheral setNotifyValue:YES forCharacteristic:characteristic];
            }
         
    }
}

5与外设做数据交互(读 与 写)  

1
2
3
4
5
6
7
8
9
10
11
12
13
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
    if (error)
    {
        NSLog(@"Error updating value for characteristic %@ error: %@", characteristic.UUID, [error localizedDescription]);
        self.error_b = BluetoothError_System;
        [self error];
        return;
    }
     
//    NSLog(@"收到的数据:%@",characteristic.value);
    [self decodeData:characteristic.value];
}

1
2
NSData *d2 = [[PBABluetoothDecode sharedManager] HexStringToNSData:@"0x02"];
                [self.peripheral writeValue:d2 forCharacteristic:characteristic type:CBCharacteristicWriteWithoutResponse

转载于:https://www.cnblogs.com/Free-Thinker/p/5081209.html

iOS蓝牙4.0开发例子相关推荐

  1. iOS 蓝牙4.0开发使用(内附Demo)

    一: 介绍 近几年,智能设备越来越火,这些智能设备中,有很大一部分是通过手机来控制硬件设备,来达到预期的效果,这中间少不了要使用到蓝牙功能,通过蓝牙来通信来控制设备. 蓝牙分为蓝牙2.0和蓝牙4.0. ...

  2. iOS 蓝牙 4.0 开发

    随着蓝牙低功耗技术BLE(Bluetooth Low Energy)的发展,蓝牙技术正在一步步成熟,如今的大部分移动设备都配备有蓝牙4.0,相比之前的蓝牙技术耗电量大大降低.从iOS的发展史也不难看出 ...

  3. iOS 蓝牙4.0开发

    1. 什么是蓝牙 4.0 ?   全新的蓝牙4.0技术并不是一种技术,而是由传统蓝牙,高速蓝牙和低功耗蓝牙合而为一.并且这三种蓝牙可以组合使用,也可以单独使用.其中,低功耗蓝牙即BLE是蓝牙4.0的核 ...

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

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

  5. iOS开发之蓝牙4.0开发使用

    转载自https://www.jianshu.com/p/f0e4b0e98336 2015年的时候自己写过一个蓝牙4.0项目,当忘了写博客,最近看到这篇博客写的挺详细,所以转载一下 一: 介绍 蓝牙 ...

  6. IOS蓝牙4.0与读卡器通讯

    IOS学习也一段时间了,该上点干货了.前段时间研究了一下IOS蓝牙通讯相关的东西,把研究的一个成果给大家分享一下. 一 项目背景 简单介绍一下做的东西,设备是一个金融刷卡器,通过蓝牙与iphone手机 ...

  7. Android BLE蓝牙4.0开发 实现扫描、连接、通讯、获取通知、特性等 (一、打开蓝牙 进行扫描)

    目录 首先说下蓝牙4.0 目前android蓝牙有2种: ble蓝牙开发流程: 1.首先当然是进行权限申请啦: 2.获取蓝牙适配器 BluetoothAdapter 3.打开蓝牙 4.关闭蓝牙 5.在 ...

  8. Android BLE蓝牙4.0开发详解

    这篇博客主要讲解 蓝牙 BLE 的用法.在讲解之前先讲一些概念性的东西,对于之前没接触过蓝牙开发,现在手上又有个蓝牙BLE项目需要做的人,先看下这些概念还是很重要的.因为我之前就是这样,之前没有接触过 ...

  9. Android蓝牙4.0开发及发送指令到蓝牙硬件设备,简单好用,方法已写好直接可用

    近日,接到需要用到蓝牙解锁硬件设备的新需求,开发过程中呢也遇到许多硬件的坑,开发协议文档较简单,几句话就完了,第一次搞得我自己一脸懵逼,本来一两个小时就能写完并测试完成的过程用了两三天.哎!默默地回到 ...

最新文章

  1. JD boto3 flask 上传 demo
  2. 有什么类型的MPLS?
  3. HttpClient实现CSDN自动登录
  4. 关于C#调用API的理解(汇多考勤机HD4K)
  5. Node入门之创建第一个HelloNode
  6. SAP Fiori issue排查:why search by ID does not work
  7. Android之解析XML总结(SAX、Pull、Dom三种方式)
  8. php 405跳转,php – 返回HTTP 405的CORS预检请求
  9. php中money用什么显示,如何使用PHP将十进制数转换为单词(money格式)?
  10. laytpl语法_浅谈laytpl 模板空值显示null的解决方法及简单的js表达式
  11. Codeforces Round 1 - 10总结 【@Abandon】
  12. 爬虫的一些知识点 目录 1. 网络爬虫 1 2. 产生背景 垂直领域搜索引擎 2 3. 1 聚焦爬虫工作原理以及关键技术概述 3 4. 涉及技术 3 4.1. 下载网页 一般是通过net api
  13. 用WPF实现屏幕文字提示,徐汇区网站设计
  14. 医学CT图像三维重建代码
  15. 通过池塘配置ip实验
  16. 【IoT】产品设计:OEM、ODM、EMS 的区别是什么?
  17. HTML5实现手机QQ表情功能
  18. MMO与弱交互游戏的服务端技术区别
  19. NAS信令学习笔记 ——GUTI
  20. NFT Insider #62:The Sandbox宣布与狮门影业达成合作,红杉资本继续扩大Web3领域投资

热门文章

  1. 我使用Python和Django在自己的网站上建立了一个会员专区。 这是我学到的东西。
  2. 15-5重构_重构-糟糕,我一直在向后做。
  3. vue和react相同点_我在React和Vue中创建了相同的应用程序。 这是区别。
  4. jpg在线合并jpg_JPG如何运作
  5. 手动部署OpenStack环境(一:Virtual Box 5.1 环境的安装及配置)
  6. 1007 Maximum Subsequence Sum(两种思路)
  7. 1078 Hashing
  8. Python组合数据类型之集合类型
  9. java源码推荐_基于java的推荐系统实现源代码
  10. Python培训:try-except语句与else子句联合使用处理可能出现的程序异常