对coreBluetooth的第三方框架DarkBlue的封装
之前写过一篇关于使用对coreBluetooth的第三方框架一个介绍:《Core Bluetooth ——DarkBlue-master第三方框架》
http://www.cnblogs.com/yuanjunliang/articles/4633726.html
下面是对该第三方具体实现的一个封装
//
// ViewController.m
// core Bluetooth
//
// Created by 袁俊亮 on 15/7/13.
// Copyright (c) 2015年 yuanjunliang. All rights reserved.
//
#import "ViewController.h"
#import "BTServer.h"
@interface ViewController ()<BTServerDelegate>
@property (nonatomic, weak) BTServer *defaultBTServer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.defaultBTServer = [BTServer defaultBTServer];
self.defaultBTServer.delegate = (id)self;
[self.defaultBTServer startScan];
}
/**
* 发现设备并连接
*/
- (void)didFoundPeripheral
{
[self selectPeripheralByRSSIWith:self.defaultBTServer];
}
#warning 测试的时候不使用这段代码,因为测试的时候连接特定的设备
/**
* 根据蓝牙信号的强度来选择设备
*/
- (CBPeripheral *)selectPeripheralByRSSIWith:(BTServer *)btserver
{
dispatch_async(dispatch_get_main_queue(), ^{
int i = 0;
btserver.selectPeripheral = btserver.discoveredPeripherals[0];
for (; i < btserver.discoveredPeripherals.count; i++) {
PeriperalInfo *peri = self.defaultBTServer.discoveredPeripherals[i];
if (btserver.selectPeripheral.RSSI > peri.RSSI) {
btserver.selectPeripheral = btserver.discoveredPeripherals[i];
NSLog(@"当前信号最强的设备%@, %@",peri.name,peri.RSSI);
}
i++;
}
});
return btserver.selectPeripheral;
}
#warning 测试的时候也不使用这段代码,因为这段代码需要根据上边那种方法获取到的设备来填写参数
/**
* 连接所选择的设备
*/
- (void)connectPeripheralWithSelectedPeripheral:(CBPeripheral *)selectedPeripheral
{
dispatch_async(dispatch_get_main_queue(), ^{
int i = 0;
self.defaultBTServer.selectPeripheral = self.defaultBTServer.discoveredPeripherals[0];
for (; i < self.defaultBTServer.discoveredPeripherals.count; i++) {
PeriperalInfo *peri = self.defaultBTServer.discoveredPeripherals[i];
if ([peri.name isEqualToString:@"ColomoBLE"]) {
self.defaultBTServer.selectPeripheral = self.defaultBTServer.discoveredPeripherals[i];
NSLog(@"连接设备%@, %@",peri.name,peri.RSSI);
[self.defaultBTServer connect:peri withFinishCB:^(CBPeripheral *peripheral, BOOL status, NSError *error) {
if (status) {
[self showServiceWithPeripheral:self.defaultBTServer.selectPeripheral];
}else{
NSLog(@"连接失败");
}
}];
}
i++;
}
});
}
/**
* 通过选择的设备查找设备的服务
*/
- (void)showServiceWithPeripheral:(CBPeripheral *)peripheral
{
for (int i = 0; i < [peripheral.services count]; i++) {
CBService *service = peripheral.services[i];
[self.defaultBTServer discoverService:service];
if ([self.defaultBTServer getCharacteristicState] == KING) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.defaultBTServer.discoveredSevice = service;
[self showCharacteristicWithService:self.defaultBTServer.discoveredSevice];
});
}else if([self.defaultBTServer getCharacteristicState] == KFAILED){
NSLog(@"没有获取到特征");
}
}
}
/**
* 查询服务的特征的UUID
*/
- (void)showCharacteristicWithService:(CBService *)selectedService
{
dispatch_async(dispatch_get_main_queue(), ^{
for (int i = 0; i < [selectedService.characteristics count]; i++) {
CBCharacteristic *selectCharecter = selectedService.characteristics[i];
if ([[selectCharecter.UUID UUIDString] isEqualToString:@"FFF2"]) {
[self readValueWith:selectCharecter];
}
}
});
}
/**
* 读取所选特征里的值
*
*/
- (void)readValueWith:(CBCharacteristic *)selectedCharacteristic
{
[self.defaultBTServer readValue:selectedCharacteristic];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"%@",[self.defaultBTServer.selectCharacteristic.UUID UUIDString]);
NSData *data = self.defaultBTServer.selectCharacteristic.value;
NSLog(@"读到的特征值为:%@",[NSString stringWithFormat:@"%@",data]);
});
}
#pragma mark - BTServerDelegate
- (void)didDisconnect
{
[self.defaultBTServer startScan];
}
@end
// core Bluetooth
//
// Created by 袁俊亮 on 15/7/13.
// Copyright (c) 2015年 yuanjunliang. All rights reserved.
//
#import "ViewController.h"
#import "BTServer.h"
@interface ViewController ()<BTServerDelegate>
@property (nonatomic, weak) BTServer *defaultBTServer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.defaultBTServer = [BTServer defaultBTServer];
self.defaultBTServer.delegate = (id)self;
[self.defaultBTServer startScan];
}
/**
* 发现设备并连接
*/
- (void)didFoundPeripheral
{
[self selectPeripheralByRSSIWith:self.defaultBTServer];
}
#warning 测试的时候不使用这段代码,因为测试的时候连接特定的设备
/**
* 根据蓝牙信号的强度来选择设备
*/
- (CBPeripheral *)selectPeripheralByRSSIWith:(BTServer *)btserver
{
dispatch_async(dispatch_get_main_queue(), ^{
int i = 0;
btserver.selectPeripheral = btserver.discoveredPeripherals[0];
for (; i < btserver.discoveredPeripherals.count; i++) {
PeriperalInfo *peri = self.defaultBTServer.discoveredPeripherals[i];
if (btserver.selectPeripheral.RSSI > peri.RSSI) {
btserver.selectPeripheral = btserver.discoveredPeripherals[i];
NSLog(@"当前信号最强的设备%@, %@",peri.name,peri.RSSI);
}
i++;
}
});
return btserver.selectPeripheral;
}
#warning 测试的时候也不使用这段代码,因为这段代码需要根据上边那种方法获取到的设备来填写参数
/**
* 连接所选择的设备
*/
- (void)connectPeripheralWithSelectedPeripheral:(CBPeripheral *)selectedPeripheral
{
dispatch_async(dispatch_get_main_queue(), ^{
int i = 0;
self.defaultBTServer.selectPeripheral = self.defaultBTServer.discoveredPeripherals[0];
for (; i < self.defaultBTServer.discoveredPeripherals.count; i++) {
PeriperalInfo *peri = self.defaultBTServer.discoveredPeripherals[i];
if ([peri.name isEqualToString:@"ColomoBLE"]) {
self.defaultBTServer.selectPeripheral = self.defaultBTServer.discoveredPeripherals[i];
NSLog(@"连接设备%@, %@",peri.name,peri.RSSI);
[self.defaultBTServer connect:peri withFinishCB:^(CBPeripheral *peripheral, BOOL status, NSError *error) {
if (status) {
[self showServiceWithPeripheral:self.defaultBTServer.selectPeripheral];
}else{
NSLog(@"连接失败");
}
}];
}
i++;
}
});
}
/**
* 通过选择的设备查找设备的服务
*/
- (void)showServiceWithPeripheral:(CBPeripheral *)peripheral
{
for (int i = 0; i < [peripheral.services count]; i++) {
CBService *service = peripheral.services[i];
[self.defaultBTServer discoverService:service];
if ([self.defaultBTServer getCharacteristicState] == KING) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.defaultBTServer.discoveredSevice = service;
[self showCharacteristicWithService:self.defaultBTServer.discoveredSevice];
});
}else if([self.defaultBTServer getCharacteristicState] == KFAILED){
NSLog(@"没有获取到特征");
}
}
}
/**
* 查询服务的特征的UUID
*/
- (void)showCharacteristicWithService:(CBService *)selectedService
{
dispatch_async(dispatch_get_main_queue(), ^{
for (int i = 0; i < [selectedService.characteristics count]; i++) {
CBCharacteristic *selectCharecter = selectedService.characteristics[i];
if ([[selectCharecter.UUID UUIDString] isEqualToString:@"FFF2"]) {
[self readValueWith:selectCharecter];
}
}
});
}
/**
* 读取所选特征里的值
*
*/
- (void)readValueWith:(CBCharacteristic *)selectedCharacteristic
{
[self.defaultBTServer readValue:selectedCharacteristic];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"%@",[self.defaultBTServer.selectCharacteristic.UUID UUIDString]);
NSData *data = self.defaultBTServer.selectCharacteristic.value;
NSLog(@"读到的特征值为:%@",[NSString stringWithFormat:@"%@",data]);
});
}
#pragma mark - BTServerDelegate
- (void)didDisconnect
{
[self.defaultBTServer startScan];
}
@end
由于该项目中使用了和本地设备的一个测试连接,具体使用的时候需要根据自己的设备的UUID进行调整