iOS蓝牙——test
来源:互联网 发布:台湾大学知乎 编辑:程序博客网 时间:2024/06/07 15:31
#import "ViewController.h"
#import <CoreBluetooth/CoreBluetooth.h>
@interface ViewController ()<CBCentralManagerDelegate,CBPeripheralDelegate>
@end
@implementation ViewController{
//设备的集合
NSMutableArray *array;
//服务的集合
NSMutableArray *array_service;
//蓝牙管理者
CBCentralManager *blueToothManage;
//发现的设备
CBPeripheral *myPeripheral;
}
- (void)viewDidLoad {
[superviewDidLoad];
array=[NSMutableArrayarrayWithCapacity:5];
array_service=[NSMutableArrayarrayWithCapacity:5];
// Do any additional setup after loading the view, typically from a nib.
dispatch_queue_t myQueue=dispatch_queue_create("com.lanya",DISPATCH_QUEUE_SERIAL);
blueToothManage=[[CBCentralManageralloc]initWithDelegate:selfqueue:myQueue];
UIView *myview=[[UIViewalloc]initWithFrame:CGRectMake(0,30, 300,150)];
myview.backgroundColor=[UIColorblueColor];
UITapGestureRecognizer *ui=[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(dianji)];
myview.userInteractionEnabled=YES;
[myview addGestureRecognizer:ui];
[self.viewaddSubview:myview];
}
-(void)dianji{
myPeripheral.delegate=self;
[blueToothManageconnectPeripheral:myPeripheraloptions:nil];
};
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
switch (central.state) {
case1:
NSLog(@"不支持");
break;
case2:
NSLog(@"未授权");
break;
case3:
NSLog(@"该设备蓝牙未授权,请检查系统设置");
break;
case4:
NSLog(@"该设备尚未打开蓝牙,请在设置中打开");
break;
case5:
NSLog(@"蓝牙已经成功开启,请稍后再试");
[blueToothManagescanForPeripheralsWithServices:niloptions:nil];
break;
default:
break;
}
};
//发现服务的回调
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(nullableNSError *)error{
//array_service= [peripheral services];
NSLog(@"服务数量:%lu",[peripheralservices].count);
};
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
NSLog(@"%@",@"已经建立连接");
//停止扫描
[blueToothManagestopScan];
[myPeripheraldiscoverServices:nil];
};
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI{
NSLog(@"%@",@"发现设备");
NSLog(@"name:%@",peripheral);
NSLog(@"dicoveredPeripherals:%@", peripheral.name);
if([peripheral.nameisEqualToString:@"BT05"]){
myPeripheral=peripheral;
NSLog(@"找到BT05");
}
if(![arraycontainsObject:peripheral]){
[arrayaddObject:peripheral];
NSLog(@"dicoveredPeripherals:%@", peripheral.name);
NSLog(@"数据的长度:%lu",(unsignedlong)array.count);
}
}
@end
- iOS蓝牙——test
- IOS系列——蓝牙
- IOS系列——蓝牙(中心)2
- IOS开发之——IOS模拟器调试蓝牙BLE
- IOS开发之——蓝牙通信浅谈
- iOS 开发——CoreBluetooth蓝牙开发(一)
- iOS开发——CoreBluetooth蓝牙开发(二)
- iOS开发——CoreBluetooth蓝牙开发(三)
- IOS开发之——蓝牙通信浅谈
- IOS开发之——蓝牙通信浅谈
- IOS开发之——蓝牙通信浅谈
- iOS基础——实现蓝牙BLE4.0通信功能
- iOS之蓝牙开发—CoreBluetooth详解一
- iOS之蓝牙开发—CoreBluetooth详解二
- ios 蓝牙
- ios 蓝牙
- iOS蓝牙
- iOS 蓝牙
- CSS学习笔记之关联选择器
- struts2文件上传与下载与图片显示所遇到的问题
- 测试用例基础概念
- pat 乙 1003. 我要通过!
- 解决Xcode7.1上传成功但iTunesconnect一直不能构建版本的问题
- iOS蓝牙——test
- 基础知识补漏-数据库的启动和关闭
- ndk编程(3)--java类型转C
- MySQL SQL优化
- 图像编程总结
- 使用富文本SpannableString实现标签
- 依赖注入
- 网易笔试题:比较重量
- 基础知识补漏-控制文件和引导