欧洲街头赛车,樱木花道结局,隋棠金钟春光乍泄
最近做了几个蓝牙项目,写一下过程,方便大家。
第一步 遵循代理
@interface dbblemanager ()
第二步 设置蓝牙属性
@property (nonatomic ,strong)cbcentralmanager *mycentralmanager;
第三步 实例化
_mycentralmanager = [[cbcentralmanager alloc]initwithdelegate:self queue:nil options:nil];
第四步 实现代理方法
- (void)centralmanagerdidupdatestate:(cbcentralmanager *)central { if (central.state == cbcentralmanagerstatepoweredon) { nslog(@"蓝牙已打开"); nsdictionary *options = [nsdictionary dictionarywithobjectsandkeys:[nsnumber numberwithbool:yes], cbcentralmanagerscanoptionallowduplicateskey, nil]; 扫描 [_mycentralmanager scanforperipheralswithservices:nil options:options]; }else { nslog(@"蓝牙已关闭"); nsurl *url=[nsurl urlwithstring:@"prefs:root=bluetooth"]; [[uiapplication sharedapplication]openurl:url]; [dbtipview progresshud:@"设置-蓝牙-打开蓝牙"]; } }
第五步 代理返回蓝牙外设 连接
- (void)centralmanager:(cbcentralmanager *)central diddiscoverperipheral:(cbperipheral *)peripheral advertisementdata:(nsdictionary *)advertisementdata rssi:(nsnumber *)rssi { [_mycentralmanager connectperipheral:peripheral options:nil]; }
第六步
连接上蓝牙 - (void)centralmanager:(cbcentralmanager *)central didconnectperipheral:(cbperipheral *)peripheral { [_mycentralmanager stopscan]; } //连接失败 - (void)centralmanager:(cbcentralmanager *)central didfailtoconnectperipheral:(cbperipheral *)peripheral error:(nullable nserror *)error{ } //失去连接 -(void)centralmanager:(cbcentralmanager *)central diddisconnectperipheral:(cbperipheral *)peripheral error:(nserror *)error{ }
接下来会开放一个 蓝牙的简单封装。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
网友评论