iOS蓝牙4.0开发流程
2015-10-20 10:27
447 查看
// // ViewController.m // CoreBluetooth // // Created by rimi on 15/10/19. // Copyright © 2015年 RIMI. All rights reserved. // #import "ViewController.h" #import <CoreBluetooth/CoreBluetooth.h> @interface ViewController ()<CBCentralManagerDelegate,CBPeripheralDelegate> @property (nonatomic,strong) NSMutableArray *peripherals; @property (nonatomic,strong) CBCentralManager *mgr; @property (nonatomic,strong) CBPeripheral *test_peripheral; @property (nonatomic,strong) CBCharacteristic *writeCharcteristic; @property (nonatomic,strong) CBCharacteristic *readCharcteristic; @end @implementation ViewController - (NSMutableArray *)peripherals{ if (!_peripherals) { _peripherals = [NSMutableArray array]; } return _peripherals; } - (void)viewDidLoad { [super viewDidLoad]; //1.创建中心设备 CBCentralManager *mgr = [[CBCentralManager alloc]init]; self.mgr = mgr; //设置代理 mgr.delegate = self; //利用中心设备扫描外部设备 /* 如果指定数组代表只烧苗指定的设备 */ [mgr scanForPeripheralsWithServices:nil options:nil]; // Do any additional setup after loading the view, typically from a nib. } #pragma mark --<CBCentralManagerDelegate> - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI{ //保存扫描得到外部设备 // 判断如果数组中不包含当前扫描到得外部设备才保存 if (![self.peripherals containsObject:peripheral]) { peripheral.delegate = self; [self.peripherals addObject:peripheral]; } } /* 模拟点击,然后连接所有的外设 */ - (void)start{ for (CBPeripheral *peripheral in self.peripherals) { [self.mgr connectPeripheral:peripheral options:nil]; } } /* 连接外设成功调用 */ - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{ //扫描外设中得服务 [peripheral discoverServices:nil]; } /* 连接外设失败调用 */ - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{ NSLog(@"failed"); } #pragma mark --CBPeripheralDelegate /* 只要扫描到服务就会调用 */ - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{ //获取外设中所有扫描到得服务 NSArray *services = peripheral.services; for (CBService *service in services) { if ([service.UUID.UUIDString isEqualToString:@"123"]) { //从需要的服务中查找需要的特征 //从peripheral中得service中扫描特征 [peripheral discoverCharacteristics:nil forService:service]; } } } /* 只要扫描到特征就会调用 * @param peripheral 特征所属的外设 * @param service 特征所属的服务 */ - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{ //拿到服务中所有的特征 NSArray *characteristics = service.characteristics; // 便利特征,拿到需要的特征处理 for (CBCharacteristic *characteristic in characteristics) { if ([characteristic.UUID.UUIDString isEqualToString:@"write"]) { NSLog(@"设置闹钟"); } if ([characteristic.UUID.UUIDString isEqualToString:@"read"]) { NSLog(@"可读的特征"); } } } - (void)centralManagerDidUpdateState:(CBCentralManager *)central{ } //与外设做数据交互 - (void)write:(NSData*)data{ [self.test_peripheral writeValue:data forCharacteristic: _writeCharcteristic type:CBCharacteristicWriteWithoutResponse]; } - (void)sendMessage{ unsigned char commod[512] = {0}; unsigned char *ptmp; int sendLength = 0; ptmp = commod; NSData *data = [[NSData alloc]initWithBytes:&commod length:sendLength]; [self write:data];//写入数据到特征中 } //监听设备 - (void)startSubscribe{ [_test_peripheral setNotifyValue:YES forCharacteristic:_readCharcteristic]; } //当设备有数据返回时,同样是通过一个系统回调通知我,如下所示: - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{ } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
苹果官方推荐文档地址:https://developer.apple.com/library/ios/samplecode/BTLE_Transfer/Listings/BTLE_Transfer_BTLECentralViewController_m.html#//apple_ref/doc/uid/DTS40012927-BTLE_Transfer_BTLECentralViewController_m-DontLinkElementID_6
相关文章推荐
- IOS开发—无需自定义,修改tabbar背景颜色
- iOS TextFiled、TextView 关于键盘的收起以及处理键盘遮挡
- ReactiveCocoa,最受欢迎的iOS函数响应式编程库(2.5版),没有之一!
- iOS 的 XMPPFramework 简介
- Block - 3
- ios系统框架一: Core Animation
- iOS_mapKit与Core Location
- IOS中自己关于Ivar和objc_property_t的理解,整理下网上资料
- iOS开发中常用的第三方类库
- iOS开发中更改手机状态栏和隐藏手机状态栏的方法
- iOS9 定位遇到的一点问题
- IOS中NSUserDefaults的用法(轻量级本地数据存储)
- IOS FMDB框架 简单粗暴的使用
- IOS view的圆角和阴影并存
- iOS开发中对文件目录的访问及管理的基本方法小结
- iOS8后 屏幕适配 Size Classes 、 AutoLayout
- iOS 中的 Block
- iOS开发-常用第三方开源框架
- IOS开发笔记-02 图片浏览&amp;Tom 猫-01.放大缩小 02.首尾式动画 03.位移形变
- iOS不同版本的新特性小结