iOS蓝牙4.0开发流程
来源:互联网 发布:旺旺号是不是淘宝昵称 编辑:程序博客网 时间:2024/06/06 16:31
//// 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
0 0
- iOS蓝牙4.0开发流程
- iOS蓝牙4.0开发流程
- iOS蓝牙开发流程
- iOS 蓝牙开发流程
- iOS 蓝牙 4.0 开发
- iOS蓝牙4.0开发
- iOS蓝牙4.0开发
- iOS 蓝牙4.0开发
- iOS 蓝牙4.0开发
- Swift学习第十三枪-使用Swift开发IOS中蓝牙4.0的开发流程
- iOS 蓝牙 基本流程
- iOS开发: iOS蓝牙4.0开发例子
- iOS蓝牙4.0开发例子
- iOS蓝牙4.0开发例子
- iOS蓝牙4.0(BLE)-开发
- iOS蓝牙4.0开发例子
- iOS 蓝牙4.0 开发体会
- iOS蓝牙4.0开发例子
- 大数据社区研讨会 (第一届)
- 学习C语言第一天
- dubbo_rpc包中thrift协议的使用过程
- IOS 宏定义
- Android WebView网页自适应手机,可放大缩小设置代码
- iOS蓝牙4.0开发流程
- 操作系统学习笔记(3)——进程描述与进程状态变化
- cannot resolve symbol 'XXXX'
- 黑马程序员——java学习日记二
- SystemUi->虚拟按键
- 标准模板库STL(Standard Template Library)
- Hadoop源码学习积累2
- ios逆向工程-静态分析
- 图像处理之玻璃水印特效(祝大家圣诞节快乐)