一、服务端(也叫周边设备吧。。脑残的翻译)

1.实现类必须遵守协议 CBPeripheralManagerDelegate

2.需要的主要类有:

@property(strong,nonatomic) CBPeripheralManager *peripheraManager;

@property(strong,nonatomic) CBMutableCharacteristic *customerCharacteristic;

@property (strong,nonatomic) CBMutableService *customerService;

3.调用流程代码中有注释

  1. //
  2. // ViewController.m
  3. // BlueToothDemo
  4. //
  5. // Created by PSH_Chen_Tao on 7/3/13.
  6. // Copyright (c) 2013 wolfman. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10. static NSString *const kCharacteristicUUID = @"CCE62C0F-1098-4CD0-ADFA-C8FC7EA2EE90";
  11.  
  12. static NSString *const kServiceUUID = @"50BD367B-6B17-4E81-B6E9-F62016F26E7B";
  13. @interface ViewController ()
  14.  
  15. @end
  16.  
  17. @implementation ViewController
  18.  
  19. @synthesize peripheraManager;
  20. @synthesize customerCharacteristic;
  21. @synthesize customerService;
  22. - (void)viewDidLoad
  23. {
  24. [super viewDidLoad];
  25. // Do any additional setup after loading the view, typically from a nib.
  26. //初始化后会直接调用代理的 - (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral
  27.  
  28. peripheraManager = [[CBPeripheralManager alloc]initWithDelegate:self queue:nil];
  29. // [peripheraManager startAdvertising:nil];
  30.  
  31. }
  32.  
  33. -(void)setUp{
  34. CBUUID *characteristicUUID = [CBUUID UUIDWithString:kCharacteristicUUID];
  35. customerCharacteristic = [[CBMutableCharacteristic alloc]initWithType:characteristicUUID properties:CBCharacteristicPropertyNotify value:nil permissions:CBAttributePermissionsReadable];
  36. CBUUID *serviceUUID = [CBUUID UUIDWithString:kServiceUUID];
  37. customerService = [[CBMutableService alloc]initWithType:serviceUUID primary:YES];
  38. [customerService setCharacteristics:@[characteristicUUID]];
  39. //添加后就会调用代理的- (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(NSError *)error
  40. [peripheraManager addService:customerService];
  41.  
  42. }
  43.  
  44. - (void)didReceiveMemoryWarning
  45. {
  46. [super didReceiveMemoryWarning];
  47. // Dispose of any resources that can be recreated.
  48. }
  49.  
  50. #pragma mark -- CBPeripheralManagerDelegate
  51. - (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral{
  52. switch (peripheral.state) {
  53. //在这里判断蓝牙设别的状态 当开启了则可调用 setUp方法(自定义)
  54. case CBPeripheralManagerStatePoweredOn:
  55. NSLog(@"powered on");
  56. [self setUp];
  57. break;
  58. case CBPeripheralManagerStatePoweredOff:
  59. NSLog(@"powered off");
  60. break;
  61.  
  62. default:
  63. break;
  64. }
  65. }
  66.  
  67. - (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(NSError *)error{
  68. if (error == nil) {
  69. //添加服务后可以在此向外界发出通告 调用完这个方法后会调用代理的
  70. //(void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(NSError *)error
  71. [peripheraManager startAdvertising:@{CBAdvertisementDataLocalNameKey : @"Service",CBAdvertisementDataServiceUUIDsKey : [CBUUID UUIDWithString:kServiceUUID]}];
  72. }
  73.  
  74. }
  75.  
  76. - (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(NSError *)error{
  77. NSLog(@"in peripheralManagerDidStartAdvertisiong:error");
  78. }
  79. @end

二、客户端(也叫中心设备吧)

1.实现类要遵守协议<CBCentralManagerDelegate,CBPeripheralDelegate>

2.主要用到的类有

@property(strong,nonatomic)CBCentralManager *centralManager;

@property(strong,nonatomic)NSMutableData *mutableData;

@property(strong,nonatomic)CBPeripheral *peripheral;

3.一般的流程

  1. //
  2. // ViewController.m
  3. // BlueToothClient
  4. //
  5. // Created by PSH_Chen_Tao on 7/3/13.
  6. // Copyright (c) 2013 wolfman. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10. static NSString *const kCharacteristicUUID = @"CCE62C0F-1098-4CD0-ADFA-C8FC7EA2EE90";
  11.  
  12. static NSString *const kServiceUUID = @"50BD367B-6B17-4E81-B6E9-F62016F26E7B";
  13. @interface ViewController ()
  14.  
  15. @end
  16.  
  17. @implementation ViewController
  18.  
  19. @synthesize centralManager;
  20. @synthesize mutableData;
  21. @synthesize peripheral;
  22.  
  23. - (void)viewDidLoad
  24. {
  25. [super viewDidLoad];
  26. // Do any additional setup after loading the view, typically from a nib.
  27. //初始化后会调用代理CBCentralManagerDelegate 的 - (void)centralManagerDidUpdateState:(CBCentralManager *)central
  28. centralManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil];
  29.  
  30. }
  31.  
  32. - (void)didReceiveMemoryWarning
  33. {
  34. [super didReceiveMemoryWarning];
  35. // Dispose of any resources that can be recreated.
  36. }
  37.  
  38. #pragma mark -- CBCentralManagerDelegate
  39. - (void)centralManagerDidUpdateState:(CBCentralManager *)central{
  40. switch (central.state) {
  41. //判断状态开始扫瞄周围设备 第一个参数为空则会扫瞄所有的可连接设备 你可以
  42. //指定一个CBUUID对象 从而只扫瞄注册用指定服务的设备
  43. //scanForPeripheralsWithServices方法调用完后会调用代理CBCentralManagerDelegate的
  44. //- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI方法
  45. case CBCentralManagerStatePoweredOn:
  46. [centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:kServiceUUID]] options:@{CBCentralManagerScanOptionAllowDuplicatesKey : YES}];
  47.  
  48. break;
  49.  
  50. default:
  51. break;
  52. }
  53. }
  54.  
  55. - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
  56. if (peripheral) {
  57. self.peripheral = peripheral;
  58. //发现设备后即可连接该设备 调用完该方法后会调用代理CBCentralManagerDelegate的- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral表示连接上了设别
  59. //如果不能连接会调用 - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
  60. [centralManager connectPeripheral:peripheral options:@{CBConnectPeripheralOptionNotifyOnConnectionKey : YES}];
  61. }
  62. }
  63.  
  64. - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
  65. NSLog(@"has connected");
  66. [mutableData setLength:];
  67. self.peripheral.delegate = self;
  68. //此时设备已经连接上了 你要做的就是找到该设备上的指定服务 调用完该方法后会调用代理CBPeripheralDelegate(现在开始调用另一个代理的方法了)的
  69. //- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
  70. [self.peripheral discoverServices:@[[CBUUID UUIDWithString:kServiceUUID]]];
  71.  
  72. }
  73.  
  74. - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
  75. //此时连接发生错误
  76. NSLog(@"connected periphheral failed");
  77. }
  78.  
  79. #pragma mark -- CBPeripheralDelegate
  80. - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
  81. if (error==nil) {
  82. //在这个方法中我们要查找到我们需要的服务 然后调用discoverCharacteristics方法查找我们需要的特性
  83. //该discoverCharacteristics方法调用完后会调用代理CBPeripheralDelegate的
  84. //- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
  85. for (CBService *service in peripheral.services) {
  86. if ([service.UUID isEqual:[CBUUID UUIDWithString:kServiceUUID]]) {
  87. [peripheral discoverCharacteristics:@[[CBUUID UUIDWithString:kCharacteristicUUID]] forService:service];
  88. }
  89. }
  90. }
  91. }
  92.  
  93. - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
  94. if (error==nil) {
  95. //在这个方法中我们要找到我们所需的服务的特性 然后调用setNotifyValue方法告知我们要监测这个服务特性的状态变化
  96. //当setNotifyValue方法调用后调用代理CBPeripheralDelegate的- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
  97. for (CBCharacteristic *characteristic in service.characteristics) {
  98. if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:kCharacteristicUUID]]) {
  99.  
  100. [peripheral setNotifyValue:YES forCharacteristic:characteristic];
  101. }
  102. }
  103. }
  104. }
  105.  
  106. - (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
  107. if (error==nil) {
  108. //调用下面的方法后 会调用到代理的- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
  109. [peripheral readValueForCharacteristic:characteristic];
  110. }
  111. }
  112.  
  113. - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
  114.  
  115. }
  116.  
  117. @end

iOS蓝牙调用的一般流程的更多相关文章

  1. iOS蓝牙开发

    蓝牙常见名称和缩写 MFI ======= make for ipad ,iphone, itouch 专们为苹果设备制作的设备 BLE ==== buletouch low energy,蓝牙4.0 ...

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

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

  3. ios 蓝牙相关

      ios蓝牙开发项目实战 -(附小米手环实例)   前言 最近一直在开发关于蓝牙的功能,本来是不想写这一篇文章,因为网上关于ios蓝牙开发的文章实在太多了,成吨成吨的文章出现,但是很遗憾都只是一些皮 ...

  4. iOS蓝牙开发(二)蓝牙相关基础知识

    原文链接: http://liuyanwei.jumppo.com/2015/07/17/ios-BLE-1.html iOS蓝牙开发(一)蓝牙相关基础知识: 蓝牙常见名称和缩写 MFI ====== ...

  5. iOS 蓝牙开发(四)BabyBluetooth蓝牙库介绍(转)

    转载自:http://www.cocoachina.com/ios/20151106/14072.html 原文作者:刘彦玮 BabyBluetooth 是一个最简单易用的蓝牙库,基于CoreBlue ...

  6. IOS 蓝牙相关-BabyBluetooth蓝牙库介绍(4)

    BabyBluetooth 是一个最简单易用的蓝牙库,基于CoreBluetooth的封装,并兼容ios和mac osx. 特色: 基于原生CoreBluetooth框架封装的轻量级的开源库,可以帮你 ...

  7. ios蓝牙开发(四)BabyBluetooth蓝牙库

    BabyBluetooth 是一个最简单易用的蓝牙库,基于CoreBluetooth的封装,并兼容ios和mac osx. 特色: 基于原生CoreBluetooth框架封装的轻量级的开源库,可以帮你 ...

  8. https://github.com/coolnameismy/BabyBluetooth github上的一个ios 蓝牙4.0的库并带文档和教程

    The easiest way to use Bluetooth (BLE )in ios,even bady can use. 简单易用的蓝牙库,基于CoreBluetooth的封装,并兼容ios和 ...

  9. iOS蓝牙BLE开发

    蓝牙是一个标准的无线通讯协议,具有设备成本低.传输距离近和功耗低等特点,被广泛的应用在多种场合.蓝牙一般分为传统蓝牙和BLE两种模式:传统蓝牙可以传输音频等较大数据量,距离近.功耗相对大:而BLE则用 ...

随机推荐

  1. POJ1258 基础最小生成树

    本文出自:http://blog.csdn.net/svitter 题意:给出一个数字n代表邻接矩阵的大小,随后给出邻接矩阵的值.输出最小生成树的权值. 题解: prime算法的基本解法: 1.选择一 ...

  2. 【LeetCode】 sort list 单清单归并

    称号:Sort a linked list in O(n log n) time using constant space complexity. 思路:要求时间复杂度O(nlogn) 知识点:归并排 ...

  3. Java中关于继承、类、多态、接口的知识点

    继承 含义:在面向对象编程中,可以通过扩展一个已有的类,并继承该类的属性和行为,来创建一个新的类 优点:1)代码的重用性:2)子类扩展父类的属性和方法:3)父类的属性和方法可用于子类:4)设计应用程序 ...

  4. easyui datagrid shift 多选

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta na ...

  5. js 上一天、下一天

    function Previousday() { var curDate = new Date(CreateDate_Temp); * * * ); var strDate = preDate.get ...

  6. .net mvc mssql easyui treegrid 及时 编辑 ,支持拖拽

    这里提到了,1个问题,怎么扩展 Easyui 参见: http://blog.csdn.net/chenkai6529/article/details/17528833 @{ ViewBag.Titl ...

  7. C# foreach 有用方法具体解释

    网上查资料,说foreach 不能改动迭代变量,仅仅能訪问迭代变量.自己理解也不是非常深,通过几个代码进行验证,发现foreach的使用方法还有点特别 验证方法: 1. 迭代变量 为int int[] ...

  8. hibernate缓存机制和事务隔离机制

    一级缓存( Session缓存) }         一级缓存的管理 ◦          应用程序调用Session的save().update().saveOrUpdate().get()或loa ...

  9. LeetCode——Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  10. php——SoapClient访问webservice

    原文:php--SoapClient访问webservice 通过SoapClient调用天气预报 <?phpheader ( "Content-Type: text/html; ch ...