藍(lán)牙使用的基本流程
1.創(chuàng)建中心設(shè)備
2.利用中心設(shè)備掃描外部設(shè)備
3、模擬點(diǎn)擊, 然后連接指定的外設(shè)
4喉磁、連接外設(shè)成功調(diào)用载绿,掃描外設(shè)中得服務(wù)
5、從需要的服務(wù)中查找需要的特征幔荒, 從peripheral中得service中掃描特征
6、遍歷特征, 拿到需要的特征處理
7梳玫、對(duì)特征進(jìn)行讀寫
藍(lán)牙協(xié)議的常用方法
<CBCentralManagerDelegate,CBPeripheralDelegate>
4,CBCentralManager *cm = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
cm.delegate = self;
//scan外設(shè)
NSDictionary* scanOptions = [NSDictionary dictionaryWithObject:[NSNumbernumberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];
[cm scanForPeripheralsWithServices:nil options:scanOptions];
- (void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI//scan到設(shè)備就會(huì)調(diào)用此方法
//connect設(shè)備
NSDictionary* scanOptions = [NSDictionary dictionaryWithObject:[NSNumbernumberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];
[cm connectPeripheral:(CBPeripheral *)[_peripheralArrayobjectAtIndex:indexPath.row] options:scanOptions];
- (void) centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error//連接外設(shè)時(shí)調(diào)用
- (void) centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral*)peripheral//當(dāng)連接上一個(gè)外設(shè) CBCentralManager 代理 處理此方法
4,//查詢?cè)O(shè)備服務(wù)特征爹梁,注意 這塊的peripheral 一定得為連接上的設(shè)備 后續(xù)發(fā)送接收數(shù)據(jù)有用
[peripheral setDelegate:self];
[peripheral discoverServices:nil];
#pragma mark -- CBPeripheralDelegate
//返回的藍(lán)牙服務(wù)通知通過代理實(shí)現(xiàn)[_peripheral discoverServices:nil];
- (void) peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
//查詢服務(wù)所帶的特征值[_peripheral discoverCharacteristics:nil forService:myService];
- (void) peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
5,
//發(fā)送數(shù)據(jù)
[peripheral writeValue:datastr forCharacteristic:writeCharacteristictype:CBCharacteristicWriteWithResponse];
//發(fā)送成功調(diào)用
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
//處理藍(lán)牙發(fā)過來的數(shù)據(jù)
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
實(shí)例.m文件
#import "ViewController.h"
#import <CoreBluetooth/CoreBluetooth.h>
@interface ViewController ()<CBCentralManagerDelegate, CBPeripheralDelegate>
/**
* 外設(shè)
*/
@property (nonatomic, strong) NSMutableArray *peripherals;
/**
* 中心管理者
*/
@property (nonatomic, strong) CBCentralManager *mgr;
@end
@implementation ViewController
- (NSMutableArray *)peripherals
{
if (!_peripherals) {
_peripherals = [NSMutableArray array];
}
return _peripherals;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 1.創(chuàng)建中心設(shè)備
CBCentralManager *mgr = [[CBCentralManager alloc] init];
self.mgr = mgr;
// 設(shè)置代理
mgr.delegate = self;
// 2.利用中心設(shè)備掃描外部設(shè)備
/*
如果指定數(shù)組代表只掃描指定的設(shè)備
*/
[mgr scanForPeripheralsWithServices:nil options:nil];
}
#pragma mark - CBCentralManagerDelegate
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
// 保存掃描到得外部設(shè)備
// 判斷如果數(shù)組中不包含當(dāng)前掃描到得外部設(shè)置才保存
if (![self.peripherals containsObject:peripheral]) {
peripheral.delegate = self;
[self.peripherals addObject:peripheral];
}
}
/**
* 模擬點(diǎn)擊, 然后連接所有的外設(shè)
*/
- (void)start
{
for (CBPeripheral *peripheral in self.peripherals) {
/**
* 連接外設(shè)
*/
[self.mgr connectPeripheral:peripheral options:nil];
}
}
/**
* 連接外設(shè)成功調(diào)用
*/
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
// 掃描外設(shè)中得服務(wù)
[peripheral discoverServices:nil];
}
/**
* 連接外設(shè)失敗調(diào)用
*/
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
}
#pragma makr - CBPeripheralDelegate
/**
* 只要掃描到服務(wù)就會(huì)調(diào)用
*
* @param peripheral 服務(wù)所在的外設(shè)
*/
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
// 獲取外設(shè)中所有掃描到得服務(wù)
NSArray *services = peripheral.services;
for (CBService *service in services) {
// 拿到需要的服務(wù)
if ([service.UUID.UUIDString isEqualToString:@"123"])
{
// 從需要的服務(wù)中查找需要的特征
// 從peripheral中得service中掃描特征
[peripheral discoverCharacteristics:nil forService:service];
}
}
}
/**
* 只要掃描到特征就會(huì)調(diào)用
*
* @param peripheral 特征所屬的外設(shè)
* @param service 特征所屬的服務(wù)
*/
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
// 拿到服務(wù)中所有的特診
NSArray *characteristics = service.characteristics;
// 遍歷特征, 拿到需要的特征處理
for (CBCharacteristic * characteristic in characteristics) {
if ([characteristic.UUID.UUIDString isEqualToString:@"8888"]) {
NSLog(@"設(shè)置鬧鐘");
}
}
}
@end