iOS基于蓝牙4.0开发代码示例
本文小编整理了关于手机APP蓝牙开发中各个阶段的代码示例:
1建立中心角色
#import <CoreBluetooth/CoreBluetooth.h>
CBCentralManager *manager;
manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
2扫描外设
[manager scanForPeripheralsWithServices:nil options:options];
3连接外设
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
if(![_dicoveredPeripherals containsObject:peripheral])
[_dicoveredPeripherals addObject:peripheral];
NSLog(@"dicoveredPeripherals:%@", _dicoveredPeripherals);
}
//连接指定的设备
-(BOOL)connect:(CBPeripheral *)peripheral
{
NSLog(@"connect start");
_testPeripheral = nil;
[manager connectPeripheral:peripheral
options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]];
//开一个定时器监控连接超时的情况
connectTimer = [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(connectTimeout:) userInfo:peripheral repeats:NO];
return (YES);
}
4扫描外设中的服务和特征
连接成功后,系统会通过回调函数告诉我们,然后我们就在这个回调里去扫描设备下所有的服务和特征,代码如下
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
[connectTimer invalidate];//停止时钟
NSLog(@"Did connect to peripheral: %@", peripheral);
_testPeripheral = peripheral;
[peripheral setDelegate:self];
[peripheral discoverServices:nil];
}
一个设备里的服务和特征往往比较多,大部分情况下我们只是关心其中几个,所以一般会在发现服务和特征的回调里去匹配我们关心那些
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
NSLog(@"didDiscoverServices");
if (error)
{
NSLog(@"Discovered services for %@ with error: %@", peripheral.name, [error localizedDescription]);
if ([self.delegate respondsToSelector:@selector(DidNotifyFailConnectService:withPeripheral:error:)])
[self.delegate DidNotifyFailConnectService:nil withPeripheral:nil error:nil];
return;
}
for (CBService *service in peripheral.services)
{
if ([service.UUID isEqual:[CBUUID UUIDWithString:UUIDSTR_ISSC_PROPRIETARY_SERVICE]])
{
NSLog(@"Service found with UUID: %@", service.UUID);
[peripheral discoverCharacteristics:nil forService:service];
isVPOS3356 = YES;
break;
}
}
}
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
if (error)
{
NSLog(@"Discovered characteristics for %@ with error: %@", service.UUID, [error localizedDescription]);
if ([self.delegate respondsToSelector:@selector(DidNotifyFailConnectChar:withPeripheral:error:)])
[self.delegate DidNotifyFailConnectChar:nil withPeripheral:nil error:nil];
return;
}
for (CBCharacteristic *characteristic in service.characteristics)
{
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:UUIDSTR_ISSC_TRANS_TX]])
{
NSLog(@"Discovered read characteristics:%@ for service: %@", characteristic.UUID, service.UUID);
_readCharacteristic = characteristic;//保存读的特征
if ([self.delegate respondsToSelector:@selector(DidFoundReadChar:)])
[self.delegate DidFoundReadChar:characteristic];
break;
}
}
for (CBCharacteristic * characteristic in service.characteristics)
{
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:UUIDSTR_ISSC_TRANS_RX]])
{
NSLog(@"Discovered write characteristics:%@ for service: %@", characteristic.UUID, service.UUID);
_writeCharacteristic = characteristic;//保存写的特征
if ([self.delegate respondsToSelector:@selector(DidFoundWriteChar:)])
[self.delegate DidFoundWriteChar:characteristic];
break;
}
}
if ([self.delegate respondsToSelector:@selector(DidFoundCharacteristic:withPeripheral:error:)])
[self.delegate DidFoundCharacteristic:nil withPeripheral:nil error:nil];
}
5与外设做数据交互
//写数据
-(void)writeChar:(NSData *)data
{
[_testPeripheral writeValue:data forCharacteristic:_writeCharacteristic type:CBCharacteristicWriteWithResponse];
}
6 断开连接
//主动断开设备
-(void)disConnect
{
if (_testPeripheral != nil)
{
NSLog(@"disConnect start");
[manager cancelPeripheralConnection:_testPeripheral];
}
}
- 天猴科技-嘉兴手机APP制作
- 找革网-嘉兴APP制作公司
- 培优教育-嘉兴APP设计制作
- 蚂蚁养车-嘉兴APP开发公司
- 日本电产新宝-平湖手机APP制作
- 爱购电子-平湖APP制作公司
- 敏感物资-平湖APP设计制作
- 返利网-平湖APP开发公司
- 和万金融-海宁手机APP制作
- 小杨汽车-海宁APP制作公司
- 含妃时装-海宁APP设计制作
- 凌通电子-海宁APP开发公司
- 育婴堂母婴-海盐手机APP制作
- 乡村人家-海盐APP制作公司
- 华亚织造-海盐APP设计制作
- 后汉装饰-海盐APP开发公司
- 易采-桐乡手机APP制作
- 名品商标-桐乡APP制作公司
- 诚信保-桐乡APP设计制作
- 宜美进出口-桐乡APP开发公司
- 荣昌轴承-嘉善手机APP制作
- 宏昌塑业-嘉善APP制作公司
- 冠农-嘉善APP设计制作
- 盛康医药-嘉善APP开发公司
- 一元夺宝-嘉善一元夺宝系统
- 一元云构-嘉兴一元云购平台
- 一元商城-海宁一元商城开发
- 一元购物-桐乡一元购物软件
推荐阅读
热门文章
随机推荐
服务内容
- 嘉兴APP开发
- 嘉兴APP制作
- 嘉兴APP公司
- 嘉兴APP应用
- 嘉兴APP软件
- 嘉兴APP推广
- 嘉兴APP营销
- 嘉兴APP客户端
- 嘉兴APP开发教程
- 嘉兴APP手机客户端
- 嘉兴APP软件开发
- 嘉兴APP开发公司
- 嘉兴APP应用软件
- 嘉兴APP应用程序
- 嘉兴手机APP
- 嘉兴手机客户端
- 嘉兴手机APP推广
- 嘉兴手机APP营销
- 嘉兴手机APP制作
- 嘉兴客户端推广
- 嘉兴手机开发
- 嘉兴手机应用
- 嘉兴手机APP开发
- 嘉兴软件制作
- 嘉兴手机软件开发
- 嘉兴手机应用软件
- 嘉兴手机应用开发
- 嘉兴手机网站制作
- 嘉兴手机APP软件
- 嘉兴安卓APP开发
- 嘉兴安卓软件开发
- 嘉兴移动APP
- 嘉兴移动APP软件
- 嘉兴移动APP开发
- 嘉兴安卓手机开发
- 嘉兴企业APP制作
- 嘉兴公司APP制作
- 嘉兴企业APP开发
- 嘉兴APP是什么意思
- 嘉兴APP手机软件
- 嘉兴android手机开发
- 嘉兴android开发教程
- 嘉兴android软件开发
- 嘉兴手机客户端开发
- 嘉兴APP客户端开发
- 嘉兴移动互联网推广
- 嘉兴移动互联网营销
- 嘉兴移动客户端开发
- 嘉兴移动APP客户端
- 嘉兴移动APP软件开发
- 嘉兴APP软件开发公司
- 嘉兴APP软件开发价格
- 嘉兴APP手机客户端开发
- 嘉兴安卓手机软件开发
- 嘉兴安卓手机APP开发
- 嘉兴手机APP开发教程
- 嘉兴手机APP软件开发
- 嘉兴手机APP开发软件
- 嘉兴手机APP开发工具
- 嘉兴手机APP如何开发
- 嘉兴手机APP开发教程
- 嘉兴移动APP开发工具
- 嘉兴移动APP开发软件
- 嘉兴手机APP开发教程
- 嘉兴手机APP开发流程
- 嘉兴移动APP开发教程
- 嘉兴安卓手机客户端开发
- 嘉兴移动应用程序开发
- 嘉兴手机应用程序开发
- 嘉兴安卓应用开发软件
- 嘉兴安卓手机应用开发教程