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

首先进一则广告:

蓝牙技术联盟(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也是可以的,做人呢最重要是开心。。。哈哈哈哈

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容