iOS蓝牙开发数据实时传输
程序员文章站
2023-11-22 12:52:28
随着ios项目开发 很多app需要通过蓝牙与设备连接
蓝牙开发注意:
先定义中心设备和外围设备以及遵守蓝牙协议
@interface viewcontroll...
随着ios项目开发 很多app需要通过蓝牙与设备连接
蓝牙开发注意:
先定义中心设备和外围设备以及遵守蓝牙协议
@interface viewcontroller()<cbcentralmanagerdelegate,cbperipheraldelegate> @property (strong, nonatomic) cbcentralmanager *manager; @property (nonatomic, strong) cbperipheral *peripheral; @property (nonatomic, weak)nstimer * connenttimer; @end
再实现delegate方法
1.判断蓝牙状态,如成功则扫描指定uuid设备(如不指定uuid,则无法后台持续连接)
2.当发现指定设备后,连接该设备
3.当连接指定外围设备成功,编写定时器,每秒读取1次rssi
4.当监听到失去和外围设备连接,重新建立连接
5.当读取到rssi值,打印出它的值
//蓝牙状态 - (void)centralmanagerdidupdatestate:(cbcentralmanager *)central { nsstring * state = nil; switch ([central state]) { case cbcentralmanagerstateunsupported: state = @"the platform/hardware doesn't support bluetooth low energy."; break; //应用程序没有被授权使用蓝牙 case cbcentralmanagerstateunauthorized: state = @"the app is not authorized to use bluetooth low energy."; break; //尚未打开蓝牙 case cbcentralmanagerstatepoweredoff: state = @"bluetooth is currently powered off."; break; //连接成功 case cbcentralmanagerstatepoweredon: [self.manager scanforperipheralswithservices:nil options:nil]; state = @"work"; break; case cbcentralmanagerstateunknown: default: ; } nslog(@"central manager state: %@", state); } //查找设备 - (void)centralmanager:(cbcentralmanager *)central diddiscoverperipheral:(cbperipheral *)peripheral advertisementdata:(nsdictionary *)advertisementdata rssi:(nsnumber *)rssi { //每个蓝牙设备有自己唯一的标识符,根据标识符确认自己要连接的设备 if ([peripheral.identifier isequal:self.peripheral.identifier]) { self.peripheral = peripheral; //数据连接定时器 self.connenttimer = [nstimer scheduledtimerwithtimeinterval:1 target:self selector:@selector(connentperipheral) userinfo:@"timer" repeats:yes]; [self.connenttimer fire]; } } - (void)connentperipheral { //连接外设 self.manager.delegate = self; [self.manager connectperipheral:_peripheral options:[nsdictionary dictionarywithobject:[nsnumber numberwithbool:yes] forkey:cbconnectperipheraloptionnotifyondisconnectionkey]]; } //连接成功后调用 - (void)centralmanager:(cbcentralmanager *)central didconnectperipheral:(cbperipheral *)peripheral { nslog(@"did connect to peripheral: %@,%@", peripheral,peripheral.name); [peripheral setdelegate:self]; //查找服务 [peripheral discoverservices:nil]; [self.connenttimer invalidate]; //监测设备是否断开了 // [self createworkdatasourcewithtimeinterval:1]; } //当监听到失去和外围设备连接,重新建立连接 //这个方法是必须实现的,因为蓝牙会中断连接,正好触发这个方法重建连接。重建连接可能造成数秒后才能读取到rssi。 - (void)centralmanager:(cbcentralmanager *)central diddisconnectperipheral:(cbperipheral *)peripheral error:(nserror *)error { [self.manager connectperipheral:peripheral options:nil]; } - (void)centralmanager:(cbcentralmanager *)central didfailtoconnectperipheral:(cbperipheral *)peripheral error:(nserror *)error { nslog(@"%@",error.description); } //返回的蓝牙服务通知通过代理实现 - (void)peripheral:(cbperipheral *)peripheral diddiscoverservices:(nserror *)error { if (error) { nslog(@"discovered services for %@ with error: %@", peripheral.name, [error localizeddescription]); return; } for (cbservice *service in peripheral.services) { // nslog(@"service found with uuid: %@", service.uuid.uuidstring); //发现服务 if ([service.uuid isequal:[cbuuid uuidwithstring:@"180d"]])//heart rate { //在一个服务中寻找特征值 [peripheral discovercharacteristics:nil forservice:service]; } } } //返回的蓝牙特征值通知通过代理实现 - (void)peripheral:(cbperipheral *)peripheral diddiscovercharacteristicsforservice:(cbservice *)service error:(nserror *)error { if (error) { nslog(@"discovered characteristics for %@ with error: %@", service.uuid, [error localizeddescription]); return; } for (cbcharacteristic * characteristic in service.characteristics) { nslog(@"characteristic:%@",characteristic); if( [characteristic.uuid isequal:[cbuuid uuidwithstring:@"2a37"]]) { [self notification:service.uuid characteristicuuid:characteristic.uuid peripheral:peripheral on:yes]; // [self.peripheral setnotifyvalue:yes forcharacteristic:characteristic]; } } } //处理蓝牙发过来的数据 - (void)peripheral:(cbperipheral *)peripheral didupdatevalueforcharacteristic:(cbcharacteristic *)characteristic error:(nserror *)error { } -(void) notification:(cbuuid *) serviceuuid characteristicuuid:(cbuuid *)characteristicuuid peripheral:(cbperipheral *)p on:(bool)on { cbservice *service = [self getservicefromuuid:serviceuuid p:p]; if (!service) { // if (p.uuid == null) return; // zach ios6 addedche // nslog(@"could not find service with uuid on peripheral with uuid \n"); return; } cbcharacteristic *characteristic = [self getcharacteristicfromuuid:characteristicuuid service:service]; if (!characteristic) { // if (p.uuid == null) return; // zach ios6 added // nslog(@"could not find characteristic with uuid on service with uuid on peripheral with uuid\n"); return; } [p setnotifyvalue:on forcharacteristic:characteristic]; } -(cbservice *) getservicefromuuid:(cbuuid *)uuid p:(cbperipheral *)p { for (cbservice* s in p.services) { if ([s.uuid isequal:uuid]) return s; } return nil; //service not found on this peripheral } -(cbcharacteristic *) getcharacteristicfromuuid:(cbuuid *)uuid service:(cbservice*)service { for (cbcharacteristic* c in service.characteristics) { if ([c.uuid isequal:uuid]) return c; } return nil; //characteristic not found on this service }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。