iOS 蓝牙使用小结 bluetooth

首先推荐去看官方文档哦


最近做一项目,本来蓝牙通讯这块不是我负责的,但是负责这块的同事要走,只好咬咬牙学习了。呜呜呜。。。。。


现将创建蓝牙工程的要点总结一下,由于工程主要涉及中心模式,所以只总结中心模式的用法

1,引入CoreBluetooth.framework

2,实现蓝牙协议,如:

.h文件如下

@protocol CBCentralManagerDelegate;

@protocol CBPeripheralDelegate;


@interface ViewController :UIViewController


.m文件如下

#import "CoreBluetooth/CoreBluetooth.h"

另外还有代理部分请自行添加


3,下面是使蓝牙动起来的过程

3.1创建CBCentralManager实例

self.cbCentralMgr = [[CBCentralManageralloc] initWithDelegate:selfqueue:nil];

设置代理,比如:

self.cbCentralMgr.delegate =self;


创建数组管理外设

self.peripheralArray = [NSMutableArrayarray];


3.2扫描周围的蓝牙

实际上周围的蓝牙如果可被发现,则会一直往外发送广告消息,中心设备就是通过接收这些消息来发现周围的蓝牙的

NSDictionary * dic = [NSDictionarydictionaryWithObjectsAndKeys:[NSNumbernumberWithBool:false],CBCentralManagerScanOptionAllowDuplicatesKey,nil];

[self.cbCentralMgrscanForPeripheralsWithServices:niloptions:dic];


3.3发现一个蓝牙设备

也就是收到了一个周围的蓝牙发来的广告信息,这是CBCentralManager会通知代理来处理

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

{

}

如果周围的蓝牙有多个,则这个方法会被调用多次,你可以通过tableView或其他的控件把这些周围的蓝牙的信息打印出来

3.4连接一个蓝牙

[self.cbCentralMgrconnectPeripheral:peripheraloptions:[NSDictionarydictionaryWithObject:[NSNumbernumberWithBool:YES]forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]];

一个中心设备可以同时连接多个周围的蓝牙设备

当连接上某个蓝牙之后,CBCentralManager会通知代理处理


- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral

{

}


因为在后面我们要从外设蓝牙那边再获取一些信息,并与之通讯,这些过程会有一些事件可能要处理,所以要给这个外设设置代理,比如:

peripheral.delegate =self;

3.5查询蓝牙服务

[peripheral discoverServices:nil];

返回的蓝牙服务通知通过代理实现

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

{


for (CBService* servicein peripheral.services){

}

}

3.6查询服务所带的特征值

[peripheral discoverCharacteristics:nilforService:service];

返回的蓝牙特征值通知通过代理实现

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

{

for (CBCharacteristic * characteristicin service.characteristics) {

}

}

3.7给蓝牙发数据

[peripheral writeValue:dataforCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];

这时还会触发一个代理事件

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

{

}

3.8处理蓝牙发过来的数据

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

{

}


3.9 retrievePeripheralsWithIdentifiers使用例子

-(IBAction) Retrieve:(id)Sender

{

[self.tvLogsetText:@""];

NSMutableArray * Identifiers = [NSMutableArrayarray];

for (CBPeripheral * peripheralin self.peripheralArray) {

[IdentifiersaddObject:peripheral.identifier];

}


[selfaddLog:@"[self.cbCentralMgr retrievePeripheralsWithIdentifiers:self.PeripheralIdentifiers]"];

self.retrievePeripherals = [self.cbCentralMgrretrievePeripheralsWithIdentifiers:Identifiers];

for (CBPeripheral* peripheralin self.retrievePeripherals) {

[selfaddLog:[NSStringstringWithFormat: @"%@ name:%@",peripheral,peripheral.name]];

}

[self.tableViewPeripheralreloadData];

}


3.10 retrieveConnectedPeripheralsWithServices 使用例子


-(IBAction) Retrieve:(id)Sender

{

[self.tvLogsetText:@""];

NSMutableArray * services = [NSMutableArrayarray];

for (CBPeripheral * peripheralin self.peripheralArray) {

if (peripheral.isConnected) {

for (CBService *servicein peripheral.services) {

[servicesaddObject:service.UUID];

}

}

}

[selfaddLog:@"[self.cbCentralMgr retrieveConnectedPeripheralsWithServices:peripheral.services]"];

self.retrievePeripherals = [self.cbCentralMgrretrieveConnectedPeripheralsWithServices:services];

for (CBPeripheral* peripheralin self.retrievePeripherals) {

[selfaddLog:[NSStringstringWithFormat: @"%@ name:%@",peripheral,peripheral.name]];

}

[self.tableViewPeripheralreloadData];

}




大概就这个个流程,例子中的参数设置,及其其他的一些代理请自己研究,因为我也是刚入门

例子在此,需要的请下载参考。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值