跟L大帝一起做~iOS蓝牙开发

2017-01-13 15:17:50来源:http://www.jianshu.com/p/b3b41d98c393作者:L大帝人点击

第七城市
首先进一则广告:

蓝牙技术联盟(Bluetooth SIG)2010年7月7日宣布,正式采纳蓝牙4.0核心规范(Bluetooth Core Specification Version 4.0 ),并启动对应的认证计划。会员厂商可以提交其产品进行测试,通过后将获得蓝牙4.0标准认证。 该技术拥有极低的运行和待机功耗,使用一粒纽扣电池甚至可连续工作数年之久。所以蓝牙技术还是可以被长久利用的一种链接技术。

然后直接进入正题。

大家也许会注意到一个问题。安卓和安卓手机可以用蓝牙相互链接,但是苹果和苹果手机就不行。因为苹果链接用的是airdrop,所以也就不需要蓝牙传输大文件。大家也就忽略了这个现象。其实airdrop本质也是蓝牙建立链接的。。。。。。。

今天主要就是用一款app去读写蓝牙设备。应用场景就是手机扫描外设,链接外设,找到外设的服务和属性,对服务和属性进行读写操作;

代码实现流程。

1.建立中心管理控制对象

```objc

#import <CoreBluetooth/CoreBluetooth.h>(导入头文件)

<CBCentralManagerDelegate,CBPeripheralDelegate>继承代理协议

//系统蓝牙设备中心管理对象可以去扫描和链接外设

CBCentralManager *_manager;

//用于保存被发现设备

NSMutableArray *_discoverPeripherals;

//初始化并设置委托和线程队列,最好一个线程的参数可以为nil,默认会就main线程

manager = [[CBCentralManager alloc]initWithDelegate:self queue:dispatch_get_main_queue()];

//创建外设数组,保存发现的设备。不保存设备会导致代理方法不可用

discoverPeripherals = [[NSMutableArray array];

```

2.扫描外设,只有设备打开状态,才可以扫描。(错误原因之一)

介绍一下

CBCentralManagerDelegate的主要代理方法。

必须实现的:

- (void)centralManagerDidUpdateState:(CBCentralManager *)central;//主设备状态改变的委托,在初始化CBCentralManager的时候会打开设备,只有当设备正确打开后才能使用

其他选择实现的代理方法。

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI; //找到外设的委托

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral;//连接外设成功的委托

- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error;//外设连接失败的委托

- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error;//断开外设的委托

//工程中的代码不适合直接放出来,就用示例demo中的了。(感谢这些伟大的开发工作者)

这些状态不需要我一一翻译了吧,如果不懂可以私信我。我一定让你自己查字典去。。。

-(void)centralManagerDidUpdateState:(CBCentralManager *)central{

switch (central.state) {

case CBCentralManagerStateUnknown:

NSLog(@">>>CBCentralManagerStateUnknown");

break;

case CBCentralManagerStateResetting:

NSLog(@">>>CBCentralManagerStateResetting");

break;

case CBCentralManagerStateUnsupported:

NSLog(@">>>CBCentralManagerStateUnsupported");

break;

case CBCentralManagerStateUnauthorized:

NSLog(@">>>CBCentralManagerStateUnauthorized");

break;

case CBCentralManagerStatePoweredOff:

NSLog(@">>>CBCentralManagerStatePoweredOff");

break;

case CBCentralManagerStatePoweredOn:

NSLog(@">>>CBCentralManagerStatePoweredOn");

//开始扫描周围的外设********** 一定要在设备开启状态再扫描外设*********。

/*

第一个参数nil就是扫描周围所有的外设,可以特定你想扫描到的符合要求的设备。扫描到外设后会进入

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI;

*/

[central scanForPeripheralsWithServices:nil options:nil];

break;

default:

break;

}

}

3.扫描完成,进入连接方法进行设备连接。

-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{

NSLog(@"当扫描到设备:%@",peripheral.name);

//这里自己去设置下连接规则,这里就是不同厂商特定设备链接的判断规则。L是我喜欢的字母。

//    if ([peripheral.name hasPrefix:@"L"]){

/*

一个主设备最多能连7个外设,每个外设最多只能给一个主设备连接,连接成功,失败,断开会进入各自的委托

//这个没有验证过。没有那么多测试设备。。。。

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral;//连接外设成功的委托

- (void)centra`lManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error;//外设连接失败的委托

- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error;//断开外设的委托

*/

//找到的设备必须持有它,否则CBCentralManager中也不会保存peripheral,那么CBPeripheralDelegate中的方法也不会被调用!!(错误之一)

[discoverPeripherals addObject:peripheral];

[central connectPeripheral:peripheral options:nil];

//    }

}

//连接到Peripherals-成功(其他操作不是我们想要的,自己做相关处理)

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{    NSLog(@">>>连接到名称为(%@)的设备-成功",peripheral.name);    //设置的peripheral委托CBPeripheralDelegate    //@interface ViewController : UIViewController[peripheral setDelegate:self];

//扫描外设Services,成功后会进入方法:-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{

[peripheral discoverServices:nil];

}

4.接下来是发现设备的服务,对相关服务中的特性进行操作。

//扫描到Services

-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{

//  NSLog(@">>>扫描到服务:%@",peripheral.services);

if (error)

{

NSLog(@">>>Discovered services for %@ with error: %@", peripheral.name, [error localizedDescription]);

return;

}

for (CBService *service in peripheral.services) {

NSLog(@"%@",service.UUID);

//扫描每个service的Characteristics,扫描到后会进入方法: -(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error

[peripheral discoverCharacteristics:nil forService:service];

}

}

//扫描到Characteristics

-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{

if (error)

{

NSLog(@"error Discovered characteristics for %@ with error: %@", service.UUID, [error localizedDescription]);

return;

}

for (CBCharacteristic *characteristic in service.characteristics)

{

NSLog(@"service:%@ 的 Characteristic: %@",service.UUID,characteristic.UUID);

}

//获取Characteristic的值,读到数据会进入方法:-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error

for (CBCharacteristic *characteristic in service.characteristics){

{

[peripheral readValueForCharacteristic:characteristic];

}

}

//搜索Characteristic的Descriptors,读到数据会进入方法:-(void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error

for (CBCharacteristic *characteristic in service.characteristics){

[peripheral discoverDescriptorsForCharacteristic:characteristic];

}

}

//获取的charateristic的值

-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{

//打印出characteristic的UUID和值

//!注意,value的类型是NSData,具体开发时,会根据外设协议制定的方式去解析数据

NSLog(@"characteristic uuid:%@  value:%@",characteristic.UUID,characteristic.value);

}

//搜索到Characteristic的Descriptors

-(void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{

//打印出Characteristic和他的Descriptors

NSLog(@"characteristic uuid:%@",characteristic.UUID);

for (CBDescriptor *d in characteristic.descriptors) {

NSLog(@"Descriptor uuid:%@",d.UUID);

}

}

//获取到Descriptors的值

-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForDescriptor:(CBDescriptor *)descriptor error:(NSError *)error{

//打印出DescriptorsUUID 和value

//这个descriptor都是对于characteristic的描述,一般都是字符串,所以这里我们转换成字符串去解析

NSLog(@"characteristic uuid:%@  value:%@",[NSString stringWithFormat:@"%@",descriptor.UUID],descriptor.value);

}

5.解析到这些数据,就可以满足日常计步,心率等一系列只读数据的操作。接下来是向外设写数据。

//写数据

-(void)writeCharacteristic:(CBPeripheral *)peripheral

characteristic:(CBCharacteristic *)characteristic

value:(NSData *)value{

//打印出 characteristic 的权限,可以看到有很多种,这是一个NS_OPTIONS,就是可以同时用于好几个值,常见的有read,write,notify,indicate,知道这几个基本就够用了,前几个是读写权限,后两个都是通知,两种不同的通知方式。

/*

typedef NS_OPTIONS(NSUInteger, CBCharacteristicProperties) {

CBCharacteristicPropertyBroadcast= 0x01,

CBCharacteristicPropertyRead= 0x02,

CBCharacteristicPropertyWriteWithoutResponse= 0x04,

CBCharacteristicPropertyWrite= 0x08,

CBCharacteristicPropertyNotify= 0x10,

CBCharacteristicPropertyIndicate= 0x20,

CBCharacteristicPropertyAuthenticatedSignedWrites= 0x40,

CBCharacteristicPropertyExtendedProperties= 0x80,

CBCharacteristicPropertyNotifyEncryptionRequired NS_ENUM_AVAILABLE(NA, 6_0)= 0x100,

CBCharacteristicPropertyIndicateEncryptionRequired NS_ENUM_AVAILABLE(NA, 6_0)= 0x200

};

*/

NSLog(@"%lu", (unsigned long)characteristic.properties);

//只有 characteristic.properties 有write的权限才可以写

if(characteristic.properties & CBCharacteristicPropertyWrite){

/*

最好一个type参数可以为CBCharacteristicWriteWithResponse或type:CBCharacteristicWriteWithResponse,区别是是否会有反馈

*/

[peripheral writeValue:value forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];

}else{

NSLog(@"该字段不可写!");

}

}

//订阅外设特性的通知。

//设置通知,数据通知会进入:didUpdateValueForCharacteristic方法

[peripheral setNotifyValue:YES forCharacteristic:characteristic];

//取消通知

[peripheral setNotifyValue:NO forCharacteristic:characteristic];

//设置通知和取消通知均可以触发此方法。

- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;

6.操作完成 或者电量不足要进行停止扫描断开设备操作。

//停止扫描并断开连接

-(void)disconnectPeripheral:(CBCentralManager *)centralManager

peripheral:(CBPeripheral *)peripheral{

//停止扫描

[centralManager stopScan];

//断开连接

[centralManager cancelPeripheralConnection:peripheral];

}

本来蓝牙开发就需要硬件工程师定义好每个硬件对应的服务,每个服务的描述以及服务特性的属性。所以开发中的代码一般都已经定义好了。最重要的就是了解各种方法是什么作用。在各自开发的过程中是否必须用到。为了让大家有个跟更加进一步的了解。接下来我再说一下服务特性的具体读写与订阅。当然我也是搜索了很多资料。借鉴了很多大神的资源。当时在github上下载了好多东西。具体是哪位大神写的也不晓得。反正很崇拜这些人。。。O(∩_∩)O哈哈哈~

我们看一下外设管理类

以上写的是centralManager的内容接下来是peripheralManager。

1.初始化外设管理者

//外设管理者

CBPeripheralManager *_peripheralManager;

//首先初始化外设管理者

peripheralManager = [[CBPeripheralManager alloc]initWithDelegate:self queue:nil];(默认为主队列)

//在你用过之后记得停止广播

//[peripheralManager stopAdvertising];

2.配置蓝牙读写的属性以及描述

//注意此方法是在保证外设在CBPeripheralManagerStatePoweredOn状态下才可调用***********

-(void)initService{

//characteristics字段描述

CBUUID *CBUUIDCharacteristicUserDescriptionStringUUID = [CBUUID UUIDWithString:CBUUIDCharacteristicUserDescriptionString];

/*

可以给主设备发送通知的特性 

properties:CBCharacteristicPropertyNotify

许可特性可读

permissions CBAttributePermissionsReadable

*/

CBMutableCharacteristic *notiyCharacteristic = [[CBMutableCharacteristic alloc]initWithType:[CBUUID UUIDWithString:notiyCharacteristicUUID] properties:CBCharacteristicPropertyNotify value:nil permissions:CBAttributePermissionsReadable];

/*

可读写的特性

properties:CBCharacteristicPropertyWrite | CBCharacteristicPropertyRead

permissions CBAttributePermissionsReadable | CBAttributePermissionsWriteable

*/

CBMutableCharacteristic *readwriteCharacteristic = [[CBMutableCharacteristic alloc]initWithType:[CBUUID UUIDWithString:readwriteCharacteristicUUID] properties:CBCharacteristicPropertyWrite | CBCharacteristicPropertyRead value:nil permissions:CBAttributePermissionsReadable | CBAttributePermissionsWriteable];

//设置description

CBMutableDescriptor *readwriteCharacteristicDescription1 = [[CBMutableDescriptor alloc]initWithType: CBUUIDCharacteristicUserDescriptionStringUUID value:@"name"];

[readwriteCharacteristic setDescriptors:@[readwriteCharacteristicDescription1]];

/*

只读的Characteristic

properties:CBCharacteristicPropertyRead

permissions CBAttributePermissionsReadable

*/

CBMutableCharacteristic *readCharacteristic = [[CBMutableCharacteristic alloc]initWithType:[CBUUID UUIDWithString:readCharacteristicUUID] properties:CBCharacteristicPropertyRead value:nil permissions:CBAttributePermissionsReadable];

//serviceOne初始化并加入两个characteristics

CBMutableService *serviceOne = [[CBMutableService alloc]initWithType:[CBUUID UUIDWithString:ServiceUUID1] primary:YES];

NSLog(@"%@",serviceOne.UUID);

[serviceOne setCharacteristics:@[notiyCharacteristic,readwriteCharacteristic]];

//serviceTwo初始化并加入一个characteristics

CBMutableService *serviceTwo = [[CBMutableService alloc]initWithType:[CBUUID UUIDWithString:ServiceUUID2] primary:YES];

[serviceTwo setCharacteristics:@[readCharacteristic]];

//添加后就会调用代理的

- (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(NSError *)error

[peripheralManager addService:serviceOne];

[peripheralManager addService:serviceTwo];

}

3.外设管理者相关的代理方法介绍

//perihpheral添加了service就调用此方法

- (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(NSError *)error{

if (!error) {

//两次都添加完成后才去发送广播

//添加服务后可以在此向外界发出通告 调用完这个方法后会调用代理的

//-(void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(NSError *)error

//开始发送广播

[peripheralManager startAdvertising:@{

CBAdvertisementDataServiceUUIDsKey : @[[CBUUID UUIDWithString:ServiceUUID1],[CBUUID UUIDWithString:ServiceUUID2]],

CBAdvertisementDataLocalNameKey : LocalNameKey

}

];

}

}


//peripheral开始发送advertising

- (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(NSError *)error{

//在这里做一些发送广播时需要的记录或者动作。

}

//This method is invoked when a central configurescharacteristicto notify or indicate. *                          It should be used as a cue to start sending updates as the characteristic value changes.

//当特性的值发生改变就会调用这个方法

//订阅characteristics

-(void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic{

NSLog(@"订阅了 %@的数据",characteristic.UUID);

//每秒执行一次给主设备发送一个当前时间的秒数(这里就是最主要的发送数据的方法)

timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(sendDate:) userInfo:characteristic  repeats:YES];

}

//发送数据,发送当前时间的秒数(这里自己定制)

-(BOOL)sendData:(NSTimer *)t {

CBMutableCharacteristic *characteristic = t.userInfo;

NSDateFormatter *dft = [[NSDateFormatter alloc]init];

[dft setDateFormat:@"ss"];

NSLog(@"%@",[dft stringFromDate:[NSDate date]]);

//执行回应Central通知数据

return  [peripheralManager updateValue:[[dft stringFromDate:[NSDate date]] dataUsingEncoding:NSUTF8StringEncoding] forCharacteristic:(CBMutableCharacteristic *)characteristic onSubscribedCentrals:nil];

}

//读characteristics请求

- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveReadRequest:(CBATTRequest *)request{

NSLog(@"didReceiveReadRequest");

//判断是否有读数据的权限

if (request.characteristic.properties & CBCharacteristicPropertyRead) {

NSData *data = request.characteristic.value;

[request setValue:data];

//对请求作出成功响应

[peripheralManager respondToRequest:request withResult:CBATTErrorSuccess];

}else{

[peripheralManager respondToRequest:request withResult:CBATTErrorWriteNotPermitted];

}

}

//写characteristics请求

- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray *)requests{

NSLog(@"didReceiveWriteRequests");

CBATTRequest *request = requests[0];

//判断是否有写数据的权限

if (request.characteristic.properties & CBCharacteristicPropertyWrite) {

//需要转换成CBMutableCharacteristic对象才能进行写值

CBMutableCharacteristic *c =(CBMutableCharacteristic *)request.characteristic;

c.value = request.value;

[peripheralManager respondToRequest:request withResult:CBATTErrorSuccess];

}else{

[peripheralManager respondToRequest:request withResult:CBATTErrorWriteNotPermitted];

}

}

至此蓝牙的整个连接发送数据的过程也就完成了。苹果的文档中也介绍的很清楚。如果你觉得某种情况需要操作但是本文没有写到的。可以看看苹果的文档。也许你会发现更多利用起来很方便的方法。

学习新知识最好站在巨人的肩膀上。这样可以少走弯路。感谢那些大公无私的为大家提供学习知识的大牛。你觉得挺不错可以给个喜欢哟。动动手指关注我也是可以的,做个好基友也是可以的,玩个lol也是可以的,做人呢最重要是开心。。。哈哈哈哈




第七城市

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台