1. 框架
利用蓝牙CoreBluetooth框架 在7.0以上就用这个连接蓝牙框架。
2.连接
连接多台蓝牙设备与单台连接蓝牙设备的道理是一样,同样需要首先发现蓝牙设备然后把发现的设备添加到一个 NSMutableArray 保存起来。
具体看下面代码:
首先 我们需要创建
@property (nonatomic,strong ) CBCentralManager *manager;// 中心设备
@property (nonatomic,strong ) NSMutableArray *devices;// 扫描到的外围设备
@property (nonatomic, strong) NSMutableArray *connectSuccess;//连接成功的的外设
上面 CBCentralManager 是指定的中心设备(手机 ,平板…)
而 CBPeripheral 所指的是需要连接的蓝牙设备(耳机,手表,手环…等等)
需要一个 connectSuccess 用来保存连接成功的设备。
设置 CBCentralManager 代理
@interface ViewController ()<CBCentralManagerDelegate>
当打开程序的时候就要实现此代理 CBCentralManagerDelegate
并自动检查手机蓝牙是否打开,只有打开的时候才会去自动的扫描蓝牙设备
//中心设备状态改变的代理必须实现
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
switch (central.state)
{
case CBManagerStatePoweredOn:
NSLog(@"蓝牙已打开");
//自动开始扫描
[self startScan];
break;
case CBManagerStateUnknown:
break;
case CBManagerStatePoweredOff:
NSLog(@"蓝牙未打开");
break;
case CBManagerStateResetting:
// [self showInfo:@"蓝牙初始化中"];
break;
case CBManagerStateUnsupported:
NSLog(@"蓝牙不支持状态");
// [self showInfo:@"蓝牙不支持状态"];
break;
case CBManagerStateUnauthorized:
// [self showInfo:@"蓝牙设备未授权"];
break;
default:
break;
}
}
扫描蓝牙
下面的代码 扫描不是一直在进行 15秒后结束扫描
/*
* 开始扫描
*/
-(void) startScan{
NSLog(@"scan....");
[self.manager scanForPeripheralsWithServices:nil options:nil];
//15秒后停止。
//(开启扫描后会不停的扫描)
[self performSelector:@selector(stopScan) withObject:nil afterDelay:15];
}
停止扫描
中心设备停止(手机、平板…)
/**
* 停止扫描
*/
-(void)stopScan{
[self.manager stopScan];
NSLog(@"stopScan....");
}
扫描到设备就要添加设备 最好是放到tableview中
并所有扫描出来的 peripheral 添加到 self.devices 中
相应的tableview也要刷新一下
/**
* 扫描蓝牙的代理
*
* @param central 中心设备
* @param peripheral 扫描到的蓝牙
* @param advertisementData 在ios中蓝牙广播信息中通常会包含以下4种类型的信息。ios的蓝牙通信协议中不接受其他类型的广播信息。
因此需要注意的是,如果需要在扫描设备时,通过蓝牙设备的Mac地址来唯一辨别设备,那么需要与蓝牙设备的硬件工程师沟通好:将所需要的Mac地址放到一下几种类型的广播信息中。
通常放到kCBAdvDataManufacturerData这个字段中。
kCBAdvDataIsConnectable = 1;
kCBAdvDataLocalName = XXXXXX;
kCBAdvDataManufacturerData = ;
kCBAdvDataTxPowerLevel = 0;
* @param RSSI 信号强度
*/
//扫描到的蓝牙设备添加到devices数组中,刷新列表
-(void)centralManager:(CBCentralManager *)central
didDiscoverPeripheral:(CBPeripheral *)peripheral
advertisementData:(NSDictionary *)advertisementData
RSSI:(NSNumber *)RSSI{
if (![self.devices containsObject:peripheral]) {
[self.devices addObject:peripheral];
[self.tableView reloadData];
NSLog(@"发现外围设备:%@---RSSI:%@---advertisementData:%@",peripheral,RSSI,advertisementData);
}
}
点击tableview中各个蓝牙选择项 并连接蓝牙 提示给用户 连接成功
并把连接成功的 peripheral 添加到 self.connectSuccess中
/**
* 蓝牙连接成功时候的代理
*
* @param central 中心设备
* @param peripheral 当前连接的设备
*/
-(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
NSLog(@"%@名字的蓝牙连接成功",peripheral.name);
_maincell.txtDeviceContent.text = @"已连接";
//cell.detailTextLabel.text = @"已连接";
[self.connectSuccess addObject:peripheral];
}
同样也有失败和断开的时候
失败的时候不做处理 也可以做处理
但是断开的时候 需要把 连接成功的 self.connectSuccess 设备移除掉
并在tableview中把 连接成功的文字清空。这样就变为没有连接的状态
/**
* 蓝牙链接失败的代理
*
* @param central 中心设备
* @param peripheral 当前连接的外设
* @param error 错误信息
*/
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error;{
NSLog(@"%@名字的蓝牙连接失败",peripheral.name);
}
/**
* 蓝牙断开连接的代理
*
* @param central 中心设备
* @param peripheral 当前需要断开连接的外设
* @param error 错误信息
*/
-(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
NSLog(@"%@名字的蓝牙断开链接",peripheral.name);
_maincell.txtDeviceContent.text = @"";
for(int i=0;i<_connectSuccess.count;i++){
CBPeripheral *p = [_connectSuccess objectAtIndex:i];
if ([p.identifier.UUIDString isEqualToString:peripheral.identifier.UUIDString]) {
[self.connectSuccess removeObject:p];
}
}
}
具体的连接的代码如下:
头部没有东西
ViewController.m
//
// ViewController.m
//
#import "ViewController.h"
#import
#import "mainViewCell.h"
@interface ViewController ()<CBCentralManagerDelegate,UITableViewDelegate,UITableViewDataSource>
- (IBAction)btnSure:(id)sender;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong) mainViewCell *maincell;
@property (nonatomic,strong ) CBCentralManager *manager;// 中心设备
@property (nonatomic,strong ) NSMutableArray *devices;// 扫描到的外围设备
@property (nonatomic, strong) NSMutableArray *connectSuccess;//链接成功的的外设
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIColor *blues = [self stringTOColor:@"#436EEE"];
[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil]];
[self.navigationController.navigationBar setBarTintColor:blues];
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = item;
_maincell = [[mainViewCell alloc] init];
self.manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
self.devices = [NSMutableArray array];
self.connectSuccess = [NSMutableArray array];
self.tableView.delegate = self;
self.tableView.dataSource = self;
//创建下啦刷新
//UIRefreshControl *rc = [[UIRefreshControl alloc] init];
//rc.attributedTitle = [[NSAttributedString alloc] initWithString:@"Drop Scan"];
//[rc addTarget:self action:@selector(redreshTableView) forControlEvents:UIControlEventValueChanged];
//self.refreshControl = rc;
}
/*
* 字符串转为颜色值
*/
- (UIColor *) stringTOColor:(NSString *)str
{
if (!str || [str isEqualToString:@""]) {
return nil;
}
unsigned red,green,blue;
NSRange range;
range.length = 2;
range.location = 1;
[[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&red];
range.location = 3;
[[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&green];
range.location = 5;
[[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&blue];
UIColor *color= [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:1];
return color;
}
/*
* 开始扫描
*/
-(void) startScan{
NSLog(@"scan....");
[self.manager scanForPeripheralsWithServices:nil options:nil];
//[self.manager scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey:@YES}];
//3秒后停止。(开启扫描后会不停的扫描)
[self performSelector:@selector(stopScan) withObject:nil afterDelay:15];
}
/**
* 停止扫描
*/
-(void)stopScan{
[self.manager stopScan];
NSLog(@"stopScan....");
}
//中心设备状态改变的代理必须实现
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
switch (central.state)
{
case CBManagerStatePoweredOn:
NSLog(@"蓝牙已打开");
//自动开始扫描
[self startScan];
break;
case CBManagerStateUnknown:
break;
case CBManagerStatePoweredOff:
NSLog(@"蓝牙未打开");
break;
case CBManagerStateResetting:
// [self showInfo:@"蓝牙初始化中"];
break;
case CBManagerStateUnsupported:
NSLog(@"蓝牙不支持状态");
// [self showInfo:@"蓝牙不支持状态"];
break;
case CBManagerStateUnauthorized:
// [self showInfo:@"蓝牙设备未授权"];
break;
default:
break;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/**
* 扫描蓝牙的代理
*
* @param central 中心设备
* @param peripheral 扫描到的蓝牙
* @param advertisementData 在ios中蓝牙广播信息中通常会包含以下4种类型的信息。ios的蓝牙通信协议中不接受其他类型的广播信息。
因此需要注意的是,如果需要在扫描设备时,通过蓝牙设备的Mac地址来唯一辨别设备,那么需要与蓝牙设备的硬件工程师沟通好:将所需要的Mac地址放到一下几种类型的广播信息中。
通常放到kCBAdvDataManufacturerData这个字段中。
kCBAdvDataIsConnectable = 1;
kCBAdvDataLocalName = XXXXXX;
kCBAdvDataManufacturerData = ;
kCBAdvDataTxPowerLevel = 0;
* @param RSSI 信号强度
*/
//扫描到的蓝牙设备添加到devices数组中,刷新列表
-(void)centralManager:(CBCentralManager *)central
didDiscoverPeripheral:(CBPeripheral *)peripheral
advertisementData:(NSDictionary<NSString *,id> *)advertisementData
RSSI:(NSNumber *)RSSI{
if (![self.devices containsObject:peripheral]) {
[self.devices addObject:peripheral];
[self.tableView reloadData];
NSLog(@"发现外围设备:%@---RSSI:%@---advertisementData:%@",peripheral,RSSI,advertisementData);
}
}
/**
* 蓝牙连接成功时候的代理
*
* @param central 中心设备
* @param peripheral 当前连接的设备
*/
-(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
NSLog(@"%@名字的蓝牙连接成功",peripheral.name);
_maincell.txtDeviceContent.text = @"已连接";
//cell.detailTextLabel.text = @"已连接";
[self.connectSuccess addObject:peripheral];
}
/**
* 蓝牙链接失败的代理
*
* @param central 中心设备
* @param peripheral 当前连接的外设
* @param error 错误信息
*/
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error;{
NSLog(@"%@名字的蓝牙连接失败",peripheral.name);
}
/**
* 蓝牙断开连接的代理
*
* @param central 中心设备
* @param peripheral 当前需要断开连接的外设
* @param error 错误信息
*/
-(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
NSLog(@"%@名字的蓝牙断开链接",peripheral.name);
_maincell.txtDeviceContent.text = @"";
for(int i=0;i<_connectSuccess.count;i++){
CBPeripheral *p = [_connectSuccess objectAtIndex:i];
if ([p.identifier.UUIDString isEqualToString:peripheral.identifier.UUIDString]) {
[self.connectSuccess removeObject:p];
}
}
/*for (CBPeripheral *p in self.connectSuccess) {
if ([p.identifier.UUIDString isEqualToString:peripheral.identifier.UUIDString]) {
[self.connectSuccess removeObject:p];
}
}*/
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.manager connectPeripheral:self.devices[indexPath.row] options:nil];
_maincell = [tableView cellForRowAtIndexPath:indexPath];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.devices.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"ViewCell";
mainViewCell *cell1 = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell1 == nil) {
cell1 = [[mainViewCell alloc]
initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:CellIdentifier];
}
if (!self.devices[indexPath.row].name) {
cell1.txtDeviceName.text=@"null";
}else{
cell1.txtDeviceName.text=self.devices[indexPath.row].name;
//cell1.txtDeviceAddre.text=self.devices[indexPath.row].identifier;
NSString *c = [NSString stringWithFormat:@"%@",self.devices[indexPath.row].identifier];
NSString *b = NULL;
if ([[UIDevice currentDevice].systemVersion floatValue] < 10.0) {
b = [c substringFromIndex:30];
}else{
b = [c substringFromIndex:30];
}
cell1.txtDeviceAddre.text = b;
}
return cell1;
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
mainViewCell *cell2 = [tableView cellForRowAtIndexPath:indexPath];
if ([cell2.txtDeviceContent.text isEqualToString:@"已连接"]) {
return YES;
}
return NO;
}
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
_maincell = [tableView cellForRowAtIndexPath:indexPath];
UITableViewRowAction *disconnect = [UITableViewRowAction rowActionWithStyle:1 title:@"断开连接" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
//断开蓝牙连接
CBPeripheral *peripheral = self.devices[indexPath.row];
[self.manager cancelPeripheralConnection:peripheral];
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}];
return @[disconnect];
}
- (IBAction)btnSure:(id)sender {
LinkViewController *link = [[LinkViewController alloc] init];
if (self.connectSuccess.count > 0)
{
for (int i = 0; i < self.connectSuccess.count; i++) {
CBPeripheral *p = self.connectSuccess[i];
if (i == 0) {
link.oneperipheral = p;
}
else if (i == 1) {
link.twoperipheral = p;
}
else if(i == 2){
link.threeperipheral = p;
}
else if(i==3){
link.fourperipheral = p;
}
else if(i==4){
link.fiveperipheral = p;
}
else if(i==5){
link.sexperipheral = p;
}
else if(i==6){
link.sevenperipheral = p;
}
}
[self.navigationController pushViewController:link animated:YES];
}
}
@end
3.数据读取
这里的数据读取是分开来写的 如果是同样的设备 并底层给的UUID 接收和发送数据是同样的话可以写一个就可以并可以用tabview 中的cell来写
但是如果需要连接不相同的UUID的时候 并解析格式不一样介意使用多个
我创建的是view 可以使用MVC 来实现 创建NSObject也可以 毕竟是 数据获取
用不上view
如 OneView.h
#import
#import
@interface OneView : UIView<CBPeripheralDelegate>
@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) CBCharacteristic *oneNotiC, *oneWriteC;
@property (nonatomic, strong) NSMutableString *mutStr;
@end
OneView.m 如下
在这个里面 我们连接到设备之后 需要读取 peripheral 里面的UUID 包含了服务和特征的 UUID
首先读取服务的 一样的要实现 CBPeripheralDelegate 代理
#pragma mark - 外围设备服务的代理
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
//NSLog(@"123");
for (CBService *s in peripheral.services) {
NSLog(@"%@",[s.UUID UUIDString]);
[peripheral discoverCharacteristics:nil forService:s];
}
}
当发现了服务UUID之后 我们需要 服务里面的具体携带的特征UUID 也是数据解析的 UUID
这里面有些是多余的你可以选择你自己需要的
#pragma mark 2. 外围设备 扫描到服务下有哪些特征
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(nonnull CBService *)service error:(nullable NSError *)error
{
if (error){
NSLog(@"扫描特征出现错误,错误原因:%@",error);
}else{
//拿到服务中所有的特征
NSArray *characteristics = service.characteristics;
//遍历特征,拿到需要的特征进行处理
for (CBCharacteristic *characteristic in characteristics){
NSLog(@"所有的特征为:%@",characteristic.UUID.UUIDString);
//如果是温度数据处理
if([characteristic.UUID.UUIDString isEqualToString:@"底层给你的UUID"]){
//将全部的特征信息打印出来
//_peripheral = peripheral;
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
NSLog(@"%@",characteristic);
}
//这里是发送 设备设置蓝牙发送温度数据的值
else if([characteristic.UUID.UUIDString isEqualToString:@"底层给你的UUID"]){
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
NSLog(@"写入的值为:%@",characteristic);
}
//如果是ota进行ota升级
else if([characteristic.UUID.UUIDString isEqualToString:@"底层给你的UUID"]){
//_peripheral = peripheral;
//设置通知
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
NSString *s = @"OTA";
NSDictionary *tempOta = [NSDictionary dictionaryWithObject:s forKey:@"ota"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"tempOTA" object:nil userInfo:tempOta];
}
//无键按钮DFU
else if([characteristic.UUID.UUIDString isEqualToString:@"底层给你的UUID"]){
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
NSLog(@"写入的OTA值为:%@",characteristic);
}
}
}
}
当读取到你需要的UUID的时候直接读取数据
在这里面你 可以用你们底层给你们的算法去解析数据
我写的不代表你的
#pragma mark 3.接收外围设备的数据(这里就能接到蓝牙发送过来的数据,具体协议就要看你们是怎么定义的)
-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
NSString *temps=[self getTempData:characteristic];
//self.textView.text = temps;
//发送广播来更新通知信息
//第一个的温度数据
NSString *blename = peripheral.name;
NSString *c = [NSString stringWithFormat:@"%@",peripheral.identifier];
NSString *b = NULL;
if ([[UIDevice currentDevice].systemVersion floatValue] < 10.0) {
b = [c substringFromIndex:30];
}else{
b = [c substringFromIndex:30];
}
NSString *bleaddre = b;
NSString *tempss = [temps stringByAppendingString:@","];
NSString *tempsss = [blename stringByAppendingString:@","];
NSString *tempssss = [tempsss stringByAppendingString:bleaddre];
NSString *tempsssss = [tempss stringByAppendingString:tempssss];
NSDictionary *tempDict = [NSDictionary dictionaryWithObject:tempsssss forKey:@"tempOne"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"tempNofictionOne" object:nil userInfo:tempDict];
}
还需要打开通知 我这个项目是通过接收通知来更新温度数据的
//设置通知
-(void)notifyCharacteristic:(CBPeripheral *)peripheral
characteristic:(CBCharacteristic *)characteristic{
//设置通知,数据通知会进入:didUpdateValueForCharacteristic方法
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
//[_centerManager stopScan];
}
OneView.m详细代码
//
// OneView.m
// LHTempProject
//
// Created by rnd on 2017/5/2.
// Copyright © 2017年 GoDream. All rights reserved.
//
#import "OneView.h"
@implementation OneView
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.mutStr = [NSMutableString string];
//[self addTextView];
}
return self;
}
//添加textView
-(void)addTextView{
self.textView = [[UITextView alloc] initWithFrame:self.bounds];
self.textView.backgroundColor = [UIColor whiteColor];
[self addSubview:self.textView];
}
#pragma mark - 外围设备服务的代理
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
//NSLog(@"123");
for (CBService *s in peripheral.services) {
NSLog(@"%@",[s.UUID UUIDString]);
[peripheral discoverCharacteristics:nil forService:s];
}
}
#pragma mark 2. 外围设备 扫描到服务下有哪些特征
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(nonnull CBService *)service error:(nullable NSError *)error
{
if (error){
NSLog(@"扫描特征出现错误,错误原因:%@",error);
}else{
//拿到服务中所有的特征
NSArray *characteristics = service.characteristics;
//遍历特征,拿到需要的特征进行处理
for (CBCharacteristic *characteristic in characteristics){
NSLog(@"所有的特征为:%@",characteristic.UUID.UUIDString);
//如果是温度数据处理
if([characteristic.UUID.UUIDString isEqualToString:@"783F2991-23E0-4BDC-AC16-78601BD84B39"]){
//将全部的特征信息打印出来
//_peripheral = peripheral;
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
NSLog(@"%@",characteristic);
}
//这里是发送 设备设置蓝牙发送温度数据的值
else if([characteristic.UUID.UUIDString isEqualToString:@"849B5163-93BE-4229-9A6A-BB38D6471917"]){
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
NSLog(@"写入的值为:%@",characteristic);
}
//如果是ota进行ota升级
else if([characteristic.UUID.UUIDString isEqualToString:@"00060001-F8CE-11E4-ABF4-0002A5D5C51B"]){
//_peripheral = peripheral;
//设置通知
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
NSString *s = @"OTA";
NSDictionary *tempOta = [NSDictionary dictionaryWithObject:s forKey:@"ota"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"tempOTA" object:nil userInfo:tempOta];
}
//无键按钮DFU
else if([characteristic.UUID.UUIDString isEqualToString:@"33040179-CAC9-4A06-B83D-433ED3F8C55E"]){
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
NSLog(@"写入的OTA值为:%@",characteristic);
}
}
}
}
#pragma mark 3.接收外围设备的数据(这里就能接到蓝牙发送过来的数据,具体协议就要看你们是怎么定义的)
-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
NSString *temps=[self getTempData:characteristic];
//self.textView.text = temps;
//发送广播来更新通知信息
//第一个的温度数据
NSString *blename = peripheral.name;
NSString *c = [NSString stringWithFormat:@"%@",peripheral.identifier];
NSString *b = NULL;
if ([[UIDevice currentDevice].systemVersion floatValue] < 10.0) {
b = [c substringFromIndex:30];
}else{
b = [c substringFromIndex:30];
}
NSString *bleaddre = b;
NSString *tempss = [temps stringByAppendingString:@","];
NSString *tempsss = [blename stringByAppendingString:@","];
NSString *tempssss = [tempsss stringByAppendingString:bleaddre];
NSString *tempsssss = [tempss stringByAppendingString:tempssss];
NSDictionary *tempDict = [NSDictionary dictionaryWithObject:tempsssss forKey:@"tempOne"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"tempNofictionOne" object:nil userInfo:tempDict];
}
//设置通知
-(void)notifyCharacteristic:(CBPeripheral *)peripheral
characteristic:(CBCharacteristic *)characteristic{
//设置通知,数据通知会进入:didUpdateValueForCharacteristic方法
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
//[_centerManager stopScan];
}
/*
* 温度数据的转换
*/
-(NSString *)getTempDatas:(CBCharacteristic *)characteristic{
NSString *tempUnit;
int temps = 0;
NSData *data= characteristic.value;
if(data!=nil){
const uint8_t *bytes = [data bytes];
int b0,b1,b2,b3;
b0 = bytes[1]&0xff;
b1 = bytes[2]&0xff;
b2 = bytes[3]&0xff;
b3 = bytes[4]&0xff;
temps = (b0<<24)|(b1<<16)|(b2<<8)|b3;
const uint8_t flagByte = bytes[0];
if ((flagByte & 0x01) != 0) {
tempUnit = @"°F";
//[_userDefaults setObject:tempUnit forKey:@"tempUnit"]; //存储温度数据
} else {
tempUnit = @"°C";
//[_userDefaults setObject:tempUnit forKey:@"tempUnit"];
}
}
float a = temps;
//保留小数点后面的1位小数
NSString *s = [NSString stringWithFormat:@"%.1f",a/10];
NSString *ss = [NSString stringWithFormat:@"%@,%@",s,tempUnit];
return ss;
}
-(NSString *)getTempData:(CBCharacteristic *)characteristic{
NSString *tempUnit;
int temps = 0;
int alarm = 0;
int normal = 0;
NSData *data= characteristic.value;
if(data!=nil){
const uint8_t *bytes = [data bytes];
int b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
b0 = bytes[0]&0xff;
b1 = bytes[1]&0xff;
b2 = bytes[2]&0xff;
b3 = bytes[3]&0xff;
b4 = bytes[4]&0xff;
b5 = bytes[5]&0xff;
b6 = bytes[6]&0xff;
b7 = bytes[7]&0xff;
b8 = bytes[8]&0xff;
b9 = bytes[9]&0xff;
temps = (b5<<24)|(b4<<16)|(b3<<8)|b2;
alarm = (b9<<24)|(b8<<16)|(b7<<8)|b6;
const uint8_t flagBB = bytes[1];
normal = flagBB&0x01;
const uint8_t flagByte = bytes[0];
if ((flagByte & 0x01) != 0) {
tempUnit = @"°F";
//[_userDefaults setObject:tempUnit forKey:@"tempUnit"]; //存储温度数据
} else {
tempUnit = @"°C";
//[_userDefaults setObject:tempUnit forKey:@"tempUnit"];
}
}
float a = temps;
//保留小数点后面的1位小数
NSString *am = [NSString stringWithFormat:@"%d",alarm];
NSString *no = [NSString stringWithFormat:@"%d",normal];
NSString *zuhe = [am stringByAppendingString:@","];
NSString *zuhe1 = [zuhe stringByAppendingString:no];
NSString *s = [NSString stringWithFormat:@"%.1f",a];
//NSString *ss = [NSString stringWithFormat:@"%@,%@",s,tempUnit];
NSString *ss = [NSString stringWithFormat:@"%@,%@,%@",s,tempUnit,zuhe1];
return ss;
}
@end
4.展示数据
把读取出来的数据放到一个界面里面显示
LinkViewController.h
//
// LinkViewController.h
// LHTempProject
//
// Created by rnd on 2017/5/2.
// Copyright © 2017年 GoDream. All rights reserved.
//
#import
#import "OneView.h"
#import "TwoView.h"
#import "ThreeView.h"
#import "FourView.h"
#import "FiveView.h"
#import "SixView.h"
#import "SevenView.h"
#import "NormalUtil.h"
#import
@interface LinkViewController : UIViewController
@property(nonatomic,strong) CBPeripheral *oneperipheral;
@property(nonatomic,strong) CBPeripheral *twoperipheral;
@property(nonatomic,strong) CBPeripheral *threeperipheral;
@property(nonatomic,strong) CBPeripheral *fourperipheral;
@property(nonatomic,strong) CBPeripheral *fiveperipheral;
@property(nonatomic,strong) CBPeripheral *sexperipheral;
@property(nonatomic,strong) CBPeripheral *sevenperipheral;
//显示温度的数值 关键 切换
@property(nonatomic,strong) UILabel *labelTempOne;
@property(nonatomic,strong) UILabel *labelSymbolOne;
@property(nonatomic,strong) UILabel *labelTempTwo;
@property(nonatomic,strong) UILabel *labelSymbolTwo;
@property(nonatomic,strong) UILabel *labelTempThree;
@property(nonatomic,strong) UILabel *labelSymbolThree;
@property(nonatomic,strong) UILabel *labelTempFour;
@property(nonatomic,strong) UILabel *labelSymbolFour;
@property(nonatomic,strong) UILabel *labelTempFive;
@property(nonatomic,strong) UILabel *labelSymbolFive;
@property(nonatomic,strong) UILabel *labelTempSix;
@property(nonatomic,strong) UILabel *labelSymbolSix;
@property(nonatomic,strong) UILabel *labelTempSeven;
@property(nonatomic,strong) UILabel *labelSymbolSeven;
//两个label
@property(nonatomic,strong) UILabel *labelNameOne;
@property(nonatomic,strong) UILabel *labelAddreOne;
@property(nonatomic,strong) UILabel *labelNameTwo;
@property(nonatomic,strong) UILabel *labelAddreTwo;
@property(nonatomic,strong) UILabel *labelNameThree;
@property(nonatomic,strong) UILabel *labelAddreThree;
@property(nonatomic,strong) UILabel *labelNameFour;
@property(nonatomic,strong) UILabel *labelAddreFour;
@property(nonatomic,strong) UILabel *labelNameFive;
@property(nonatomic,strong) UILabel *labelAddreFive;
@property(nonatomic,strong) UILabel *labelNameSix;
@property(nonatomic,strong) UILabel *labelAddreSix;
@property(nonatomic,strong) UILabel *labelNameSeven;
@property(nonatomic,strong) UILabel *labelAddreSeven;
@property(nonatomic,strong) NormalUtil *normalUtil;
@end
LinkViewController.m
//
// LinkViewController.m
// LHTempProject
//
// Created by rnd on 2017/5/2.
// Copyright © 2017年 GoDream. All rights reserved.
//
#import "LinkViewController.h"
#ifdef __OBJC__
//define this constant if you want to use Masonry without the 'mas_' prefix
#define MAS_SHORTHAND
//define this constant if you want to enable auto-boxing for default syntax
#define MAS_SHORTHAND_GLOBALS
#import "Masonry.h"
#endif
@interface LinkViewController ()
@property(nonatomic,strong)OneView *one;
@property(nonatomic,strong)TwoView *two;
@property(nonatomic,strong)ThreeView *three;
@property(nonatomic,strong)FourView *four;
@property(nonatomic,strong)FiveView *five;
@property(nonatomic,strong)SixView *six;
@property(nonatomic,strong)SevenView *seven;
@property(nonatomic,strong)UIScrollView *myscrollview;
@end
@implementation LinkViewController
- (void)viewDidLoad {
[super viewDidLoad];
_normalUtil = [NormalUtil sharedManager];
[self setUIView];
[self setValues];
//发现左边外设的服务 (运行到这个方法会进入leftView.m文件中的外围设备服务的代理)
[self.oneperipheral discoverServices:nil];
//发现右边外设的服务
[self.twoperipheral discoverServices:nil];
[self setNotification];
}
//初始化值显示
-(void) setValues{
//_labelTempOne.text = @"No Data";
_labelTempOne.text = @"Load.";
_labelTempTwo.text = @"Load.";
_labelTempThree.text = @"Load.";
_labelTempFour.text = @"Load.";
_labelTempFive.text = @"Load.";
_labelTempSix.text = @"Load.";
_labelTempSeven.text = @"Load.";
_labelTempOne.textAlignment = NSTextAlignmentCenter;
_labelTempTwo.textAlignment = NSTextAlignmentCenter;
_labelTempThree.textAlignment = NSTextAlignmentCenter;
_labelTempFour.textAlignment = NSTextAlignmentCenter;
_labelTempFive.textAlignment = NSTextAlignmentCenter;
_labelTempSix.textAlignment = NSTextAlignmentCenter;
_labelTempSeven.textAlignment = NSTextAlignmentCenter;
_labelNameOne.text = @"";
_labelAddreOne.text = @"";
_labelNameTwo.text = @"";
_labelAddreTwo.text = @"";
_labelNameThree.text = @"";
_labelAddreThree.text = @"";
_labelNameFour.text = @"";
_labelAddreFour.text = @"";
_labelNameFive.text = @"";
_labelAddreFive.text = @"";
_labelNameSix.text = @"";
_labelAddreSix.text = @"";
_labelNameSeven.text = @"";
_labelAddreSeven.text = @"";
}
//接收通知
-(void)setNotification{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tempCompletionOne:) name:@"tempNofictionOne" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tempCompletionTwo:) name:@"tempNofictionTwo" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tempCompletionThree:) name:@"tempNofictionThree" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tempCompletionFour:) name:@"tempNofictionFour" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tempCompletionFive:) name:@"tempNofictionFive" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tempCompletionSix:) name:@"tempNofictionSix" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tempCompletionSeven:) name:@"tempNofictionSeven" object:nil];
}
/*
接到温度数据的通知
*/
-(void)tempCompletionOne:(NSNotification*)notification{
NSDictionary *temps = [notification userInfo];
NSString *all = [temps objectForKey:@"tempOne"];
NSArray *allarry = [all componentsSeparatedByString:@","];
_labelTempOne.textAlignment = NSTextAlignmentCenter;
_labelSymbolOne.textAlignment = NSTextAlignmentCenter;
//这里面做数据判断
//int temp = [allarry[0] intValue];
int normal = [allarry[3] intValue];
if(normal==0){
_labelTempOne.text = allarry[0];
}else if(normal == 1){
_labelTempOne.text = @"HHH";
}else if(normal == 2){
_labelTempOne.text = @"LLL";
}else{
_labelTempOne.text = @"NONE";
}
_labelSymbolOne.text = allarry[1];
_labelNameOne.text = allarry[5];
_labelAddreOne.text = allarry[4];
}
-(void)tempCompletionTwo:(NSNotification*)notification{
NSDictionary *temps = [notification userInfo];
NSString *all = [temps objectForKey:@"tempTwo"];
NSArray *allarry = [all componentsSeparatedByString:@","];
_labelTempTwo.textAlignment = NSTextAlignmentCenter;
_labelSymbolTwo.textAlignment = NSTextAlignmentCenter;
//int a = [allarry[0] intValue];
int normal = [allarry[3] intValue];
if(normal==0){
_labelTempTwo.text = allarry[0];
}else if(normal == 1){
_labelTempTwo.text = @"HHH";
}else if(normal == 2){
_labelTempTwo.text = @"LLL";
}else{
_labelTempTwo.text = @"NONE";
}
_labelSymbolTwo.text = allarry[1];
_labelNameTwo.text = allarry[5];
_labelAddreTwo.text = allarry[4];
}
-(void)tempCompletionThree:(NSNotification*)notification{
NSDictionary *temps = [notification userInfo];
NSString *all = [temps objectForKey:@"tempThree"];
NSArray *allarry = [all componentsSeparatedByString:@","];
_labelTempThree.textAlignment = NSTextAlignmentCenter;
_labelSymbolThree.textAlignment = NSTextAlignmentCenter;
//int a = [allarry[0] intValue];
int normal = [allarry[3] intValue];
if(normal==0){
_labelTempThree.text = allarry[0];
}else if(normal == 1){
_labelTempThree.text = @"HHH";
}else if(normal == 2){
_labelTempThree.text = @"LLL";
}else{
_labelTempThree.text = @"NONE";
}
_labelSymbolThree.text = allarry[1];
_labelNameThree.text = allarry[5];
_labelAddreThree.text = allarry[4];
}
-(void)tempCompletionFour:(NSNotification*)notification{
NSDictionary *temps = [notification userInfo];
NSString *all = [temps objectForKey:@"tempFour"];
NSArray *allarry = [all componentsSeparatedByString:@","];
_labelTempFour.textAlignment = NSTextAlignmentCenter;
_labelSymbolFour.textAlignment = NSTextAlignmentCenter;
//int a = [allarry[0] intValue];
int normal = [allarry[3] intValue];
if(normal==0){
_labelTempFour.text = allarry[0];
}else if(normal == 1){
_labelTempFour.text = @"HHH";
}else if(normal == 2){
_labelTempFour.text = @"LLL";
}else{
_labelTempFour.text = @"NONE";
}
_labelSymbolFour.text = allarry[1];
_labelNameFour.text = allarry[5];
_labelAddreFour.text = allarry[4];
}
-(void)tempCompletionFive:(NSNotification*)notification{
NSDictionary *temps = [notification userInfo];
NSString *all = [temps objectForKey:@"tempFive"];
NSArray *allarry = [all componentsSeparatedByString:@","];
_labelTempFive.textAlignment = NSTextAlignmentCenter;
_labelSymbolFive.textAlignment = NSTextAlignmentCenter;
//int a = [allarry[0] intValue];
int normal = [allarry[3] intValue];
if(normal==0){
_labelTempFive.text = allarry[0];
}else if(normal == 1){
_labelTempFive.text = @"HHH";
}else if(normal == 2){
_labelTempFive.text = @"LLL";
}else{
_labelTempFive.text = @"NONE";
}
_labelSymbolFive.text = allarry[1];
_labelNameFive.text = allarry[5];
_labelAddreFive.text = allarry[4];
}
-(void)tempCompletionSix:(NSNotification*)notification{
NSDictionary *temps = [notification userInfo];
NSString *all = [temps objectForKey:@"tempSix"];
NSArray *allarry = [all componentsSeparatedByString:@","];
_labelTempSix.textAlignment = NSTextAlignmentCenter;
_labelSymbolSix.textAlignment = NSTextAlignmentCenter;
//int a = [allarry[0] intValue];
int normal = [allarry[3] intValue];
if(normal==0){
_labelTempSix.text = allarry[0];
}else if(normal == 1){
_labelTempSix.text = @"HHH";
}else if(normal == 2){
_labelTempSix.text = @"LLL";
}else{
_labelTempSix.text = @"NONE";
}
_labelSymbolSix.text = allarry[1];
_labelNameSix.text = allarry[5];
_labelAddreSix.text = allarry[4];
}
-(void)tempCompletionSeven:(NSNotification*)notification{
NSDictionary *temps = [notification userInfo];
NSString *all = [temps objectForKey:@"tempSeven"];
NSArray *allarry = [all componentsSeparatedByString:@","];
_labelTempSeven.textAlignment = NSTextAlignmentCenter;
_labelSymbolSeven.textAlignment = NSTextAlignmentCenter;
//int a = [allarry[0] intValue];
int normal = [allarry[3] intValue];
if(normal==0){
_labelTempSeven.text = allarry[0];
}else if(normal == 1){
_labelTempSeven.text = @"HHH";
}else if(normal == 2){
_labelTempSeven.text = @"LLL";
}else{
_labelTempSeven.text = @"NONE";
}
_labelSymbolSeven.text = allarry[1];
_labelNameSeven.text = allarry[5];
_labelAddreSeven.text = allarry[4];}
//加载UIView
-(void)setUIView{
NSString *getpm = [_normalUtil getPingMuWindow];
int a = [getpm intValue];
_myscrollview = [[UIScrollView alloc] init];
_myscrollview.delegate = self;
// 设置内容大小
_myscrollview.contentSize = CGSizeMake(a, 1100);
// 提示用户,Indicators flash
//[UIScrollView flashScrollIndicators];
// 是否同时运动,lock
_myscrollview.scrollEnabled = YES;
_myscrollview.directionalLockEnabled = YES;
_myscrollview.backgroundColor = [self stringTOColor:@"#D6D6D6"];
[self.view addSubview:_myscrollview];
[_myscrollview makeConstraints:^(MASConstraintMaker *make){
make.left.equalTo(self.view).offset(0);
make.right.equalTo(self.view).offset(0);
make.top.equalTo(self.view).offset(0);
make.bottom.equalTo(self.view).offset(0);
}];
//创建主要的View
UIView *mainView = [[UIView alloc] init];
mainView.backgroundColor = [self stringTOColor:@"#D6D6D6"];
[_myscrollview addSubview:mainView];
//给主view添加约束
[mainView makeConstraints:^(MASConstraintMaker *make){
make.edges.equalTo(_myscrollview);
make.width.equalTo(_myscrollview);
//make.top.equalTo(_myscrollview).offset(64);
make.height.equalTo(_myscrollview).multipliedBy(1.5);
}];
/*UIColor *blues = [self stringTOColor:@"#436EEE"];
//创建一个导航栏
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, a, 64)];
//创建一个导航栏集合
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:@"111"];
//在这个集合Item中添加标题,按钮
//style:设置按钮的风格,一共有三种选择
//action:@selector:设置按钮的点击事件
//创建一个左边按钮
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"< Back" style:UIBarButtonItemStyleDone target:self action:@selector(clickLeftButton)];
//设置导航栏的内容
[navItem setTitle:@"Display Device"];
//把导航栏集合添加到导航栏中,设置动画关闭
[navBar pushNavigationItem:navItem animated:NO];
[navBar setBarTintColor:blues];
//[navBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil]];
[navBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],
NSFontAttributeName:[UIFont boldSystemFontOfSize:20]
}];
//把左右两个按钮添加到导航栏集合中去
[navItem setLeftBarButtonItem:leftButton];
[leftButton setTintColor:[UIColor whiteColor]];
//将标题栏中的内容全部添加到主视图当中
[self.view addSubview:navBar];*/
//添加View头部
UIView *topView = [UIView new];
[mainView addSubview:topView];
[topView makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(mainView).offset(0);
make.left.equalTo(mainView).offset(0);
make.right.equalTo(mainView).offset(0);
make.height.equalTo(@0);
}];
//添加第一个显示温度view
_one = [[OneView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 100)];
_one.backgroundColor = [UIColor whiteColor];
[mainView addSubview:_one];
_oneperipheral.delegate = _one;
//添加里面的布局模式
//UIColor *bgColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"circle.png"]];
UIView *c1 = [[UIView alloc] init];
[_one addSubview:c1];
[c1 makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(_one).offset(10);
make.left.equalTo(_one).offset(35);
make.width.equalTo(@80);
make.height.equalTo(@80);
}];
UIImageView *circle1 = [[UIImageView alloc] init];
circle1.image = [UIImage imageNamed:@"circle.png"];
//[circle1 setBackgroundColor:bgColor];
[_one addSubview:circle1];
[circle1 makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(c1).offset(0);
make.left.equalTo(c1).offset(0);
make.width.equalTo(@80);
make.height.equalTo(@80);
}];
_labelTempOne = [[UILabel alloc] init];
[circle1 addSubview:_labelTempOne];
[_labelTempOne makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(c1).offset(15);
make.centerX.equalTo(c1);
make.width.equalTo(@80);
make.height.equalTo(@30);
}];
_labelSymbolOne = [[UILabel alloc] init];
[circle1 addSubview:_labelSymbolOne];
[_labelSymbolOne makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(_labelTempOne).offset(25);
make.centerX.equalTo(c1);
make.width.equalTo(@80);
make.height.equalTo(@25);
}];
//添加第一个2个label
_labelNameOne = [[UILabel alloc] init];
_labelNameOne.text = @"";
_labelNameOne.textColor = [UIColor blackColor];
//设置字体的大小
[_one addSubview:_labelNameOne];
[_labelNameOne makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(_one).offset(20);
make.left.equalTo(_one).offset(130);
make.width.equalTo(@100);
make.height.equalTo(@30);
}];
_labelAddreOne = [[UILabel alloc] init];
_labelAddreOne.text = @"";
_labelAddreOne.textColor = [UIColor blackColor];
[_one addSubview:_labelAddreOne];
[_labelAddreOne makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(_one).offset(50);
make.left.equalTo(_one).offset(130);
make.width.equalTo(@200);
make.height.equalTo(@30);
}];
//添加第二个view
_two = [[TwoView alloc]initWithFrame:CGRectMake(0, 101, [UIScreen mainScreen].bounds.size.width, 100)];
_two.backgroundColor = [UIColor whiteColor];
[mainView addSubview:_two];
_twoperipheral.delegate = _two;
UIView *c2 = [[UIView alloc] init];
[_two addSubview:c2];
[c2 makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(_two).offset(10);
make.left.equalTo(_two).offset(35);
make.width.equalTo(@80);
make.height.equalTo(@80);
}];
UIImageView *circle2 = [[UIImageView alloc] init];
circle2.image = [UIImage imageNamed:@"circle.png"];
//[circle1 setBackgroundColor:bgColor];
[c2 addSubview:circle2];
[circle2 makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(c2).offset(0);
make.left.equalTo(c2).offset(0);
make.width.equalTo(@80);
make.height.equalTo(@80);
}];
_labelTempTwo = [[UILabel alloc] init];
[c2 addSubview:_labelTempTwo];
[_labelTempTwo makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(c2).offset(15);
make.centerX.equalTo(c2);
make.width.equalTo(@80);
make.height.equalTo(@30);
}];
_labelSymbolTwo = [[UILabel alloc] init];
[c2 addSubview:_labelSymbolTwo];
[_labelSymbolTwo makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(_labelTempTwo).offset(25);
make.centerX.equalTo(c2);
make.width.equalTo(@80);
make.height.equalTo(@25);
}];
//添加第一个2个label
_labelNameTwo = [[UILabel alloc] init];
_labelNameTwo.text = @"";
_labelNameTwo.textColor = [UIColor blackColor];
[_two addSubview:_labelNameTwo];
[_labelNameTwo makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(_two).offset(20);
make.left.equalTo(_two).offset(130);
make.width.equalTo(@100);
make.height.equalTo(@30);
}];
_labelAddreTwo = [[UILabel alloc] init];
_labelAddreTwo.text = @"";
_labelAddreTwo.textColor = [UIColor blackColor];
[_two addSubview:_labelAddreTwo];
[_labelAddreTwo makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(_two).offset(50);
make.left.equalTo(_two).offset(130);
make.width.equalTo(@100);
make.height.equalTo(@30);
}];
//添加第三个view
_three = [[ThreeView alloc]initWithFrame:CGRectMake(0, 202, [UIScreen mainScreen].bounds.size.width, 100)];
_three.backgroundColor = [UIColor whiteColor];
[mainView addSubview:_three];
_threeperipheral.delegate = _three;
UIView *c3 = [[UIView alloc] init];
[_three addSubview:c3];
[c3 makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(_three).offset(10);
make.left.equalTo(_three).offset(35);
make.width.equalTo(@80);
make.height.equalTo(@80);
}];
UIImageView *circle3 = [[UIImageView alloc] init];
circle3.image = [UIImage imageNamed:@"circle.png"];
//[circle1 setBackgroundColor:bgColor];
[c3 addSubview:circle3];
[circle3 makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(c3).offset(0);
make.left.equalTo(c3).offset(0);
make.width.equalTo(@80);
make.height.equalTo(@80);
}];
_labelTempThree = [[UILabel alloc] init];
[c3 addSubview:_labelTempThree];
[_labelTempThree makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(c3).offset(15);
make.centerX.equalTo(c3);
make.width.equalTo(@80);
make.height.equalTo(@30);
}];
_labelSymbolThree = [[UILabel alloc] init];
[c3 addSubview:_labelSymbolThree];
[_labelSymbolThree makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(_labelTempThree).offset(25);
make.centerX.equalTo(c3);
make.width.equalTo(@80);
make.height.equalTo(@25);
}];
//添加第一个2个label
_labelNameThree = [[UILabel alloc] init];
_labelNameThree.text = @"";
_labelNameThree.textColor = [UIColor blackColor];
[_three addSubview:_labelNameThree];
[_labelNameThree makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(_three).offset(20);
make.left.equalTo(_three).offset(130);
make.width.equalTo(@100);
make.height.equalTo(@30);
}];
_labelAddreThree = [[UILabel alloc] init];
_labelAddreThree.text = @"";
_labelAddreThree.textColor = [UIColor blackColor];
[_three addSubview:_labelAddreThree];
[_labelAddreThree makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(_three).offset(50);
make.left.equalTo(_three).offset(130);
make.width.equalTo(@100);
make.height.equalTo(@30);
}];
//添加第4个view
_four = [[FourView alloc]initWithFrame:CGRectMake(0, 303, [UIScreen mainScreen].bounds.size.width, 100)];
_four.backgroundColor = [UIColor whiteColor];
[mainView addSubview:_four];
_fourperipheral.delegate = _four;
UIView *c4 = [[UIView alloc] init];
[_four addSubview:c4];
[c4 makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(_four).offset(10);
make.left.equalTo(_four).offset(35);
make.width.equalTo(@80);
make.height.equalTo(@80);
}];
UIImageView *circle4 = [[UIImageView alloc] init];
circle4.image = [UIImage imageNamed:@"circle.png"];
//[circle1 setBackgroundColor:bgColor];
[c4 addSubview:circle4];
[circle4 makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(c4).offset(0);
make.left.equalTo(c4).offset(0);
make.width.equalTo(@80);
make.height.equalTo(@80);
}];
_labelTempFour = [[UILabel alloc] init];
[c4 addSubview:_labelTempFour];
[_labelTempFour makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(c4).offset(15);
make.centerX.equalTo(c4);
make.width.equalTo(@80);
make.height.equalTo(@30);
}];
_labelSymbolFour = [[UILabel alloc] init];
[c4 addSubview:_labelSymbolFour];
[_labelSymbolFour makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(_labelTempFour).offset(25);
make.centerX.equalTo(c4);
make.width.equalTo(@80);
make.height.equalTo(@25);
}];
//添加第一个2个label
_labelNameFour = [[UILabel alloc] init];
_labelNameFour.text = @"";
_labelNameFour.textColor = [UIColor blackColor];
[_four addSubview:_labelNameFour];
[_labelNameFour makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(_four).offset(20);
make.left.equalTo(_four).offset(130);
make.width.equalTo(@100);
make.height.equalTo(@30);
}];
_labelAddreFour = [[UILabel alloc] init];
_labelAddreFour.text = @"";
_labelAddreFour.textColor = [UIColor blackColor];
[_four addSubview:_labelAddreFour];
[_labelAddreFour makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(_four).offset(50);
make.left.equalTo(_four).offset(130);
make.width.equalTo(@100);
make.height.equalTo(@30);
}];
//添加第5个view
_five = [[FiveView alloc]initWithFrame:CGRectMake(0, 404, [UIScreen mainScreen].bounds.size.width, 100)];
_five.backgroundColor = [UIColor whiteColor];
[mainView addSubview:_five];
_fiveperipheral.delegate = _five;
UIView *c5 = [[UIView alloc] init];
[_five addSubview:c5];
[c5 makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(_five).offset(10);
make.left.equalTo(_five).offset(35);
make.width.equalTo(@80);
make.height.equalTo(@80);
}];
UIImageView *circle5 = [[UIImageView alloc] init];
circle5.image = [UIImage imageNamed:@"circle.png"];
//[circle1 setBackgroundColor:bgColor];
[c5 addSubview:circle5];
[circle5 makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(c5).offset(0);
make.left.equalTo(c5).offset(0);
make.width.equalTo(@80);
make.height.equalTo(@80);
}];
_labelTempFive = [[UILabel alloc] init];
[c5 addSubview:_labelTempFive];
[_labelTempFive makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(c5).offset(15);
make.centerX.equalTo(c5);
make.width.equalTo(@80);
make.height.equalTo(@30);
}];
_labelSymbolFive = [[UILabel alloc] init];
[c5 addSubview:_labelSymbolFive];
[_labelSymbolFive makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(_labelTempFive).offset(25);
make.centerX.equalTo(c4);
make.width.equalTo(@80);
make.height.equalTo(@25);
}];
//添加第一个2个label
_labelNameFive = [[UILabel alloc] init];
_labelNameFive.text = @"";
_labelNameFive.textColor = [UIColor blackColor];
[_five addSubview:_labelNameFive];
[_labelNameFive makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(_five).offset(20);
make.left.equalTo(_five).offset(130);
make.width.equalTo(@100);
make.height.equalTo(@30);
}];
_labelAddreFive = [[UILabel alloc] init];
_labelAddreFive.text = @"";
_labelAddreFive.textColor = [UIColor blackColor];
[_five addSubview:_labelAddreFive];
[_labelAddreFive makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(_five).offset(50);
make.left.equalTo(_five).offset(130);
make.width.equalTo(@100);
make.height.equalTo(@30);
}];
//第六个view
_six = [[SixView alloc]initWithFrame:CGRectMake(0, 505, [UIScreen mainScreen].bounds.size.width, 100)];
_six.backgroundColor = [UIColor whiteColor];
[mainView addSubview:_six];
_sexperipheral.delegate = _six;
UIView *c6 = [[UIView alloc] init];
[_six addSubview:c6];
[c6 makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(_six).offset(10);
make.left.equalTo(_six).offset(35);
make.width.equalTo(@80);
make.height.equalTo(@80);
}];
UIImageView *circle6 = [[UIImageView alloc] init];
circle6.image = [UIImage imageNamed:@"circle.png"];
//[circle1 setBackgroundColor:bgColor];
[c6 addSubview:circle6];
[circle6 makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(c6).offset(0);
make.left.equalTo(c6).offset(0);
make.width.equalTo(@80);
make.height.equalTo(@80);
}];
_labelTempSix = [[UILabel alloc] init];
[c6 addSubview:_labelTempSix];
[_labelTempSix makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(c6).offset(15);
make.centerX.equalTo(c6);
make.width.equalTo(@80);
make.height.equalTo(@30);
}];
_labelSymbolSix = [[UILabel alloc] init];
[c6 addSubview:_labelSymbolSix];
[_labelSymbolSix makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(_labelTempSix).offset(25);
make.centerX.equalTo(c6);
make.width.equalTo(@80);
make.height.equalTo(@25);
}];
//添加第一个2个label
_labelNameSix = [[UILabel alloc] init];
_labelNameSix.text = @"";
_labelNameSix.textColor = [UIColor blackColor];
[_six addSubview:_labelNameSix];
[_labelNameSix makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(_six).offset(20);
make.left.equalTo(_six).offset(130);
make.width.equalTo(@100);
make.height.equalTo(@30);
}];
_labelAddreSix = [[UILabel alloc] init];
_labelAddreSix.text = @"";
_labelAddreSix.textColor = [UIColor blackColor];
[_six addSubview:_labelAddreSix];
[_labelAddreSix makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(_six).offset(50);
make.left.equalTo(_six).offset(130);
make.width.equalTo(@100);
make.height.equalTo(@30);
}];
//第7个view
_seven = [[SevenView alloc]initWithFrame:CGRectMake(0, 606, [UIScreen mainScreen].bounds.size.width, 100)];
_seven.backgroundColor = [UIColor whiteColor];
[mainView addSubview:_seven];
_sevenperipheral.delegate = _seven;
UIView *c7 = [[UIView alloc] init];
[_seven addSubview:c7];
[c7 makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(_seven).offset(10);
make.left.equalTo(_seven).offset(35);
make.width.equalTo(@80);
make.height.equalTo(@80);
}];
UIImageView *circle7 = [[UIImageView alloc] init];
circle7.image = [UIImage imageNamed:@"circle.png"];
//[circle1 setBackgroundColor:bgColor];
[c7 addSubview:circle7];
[circle7 makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(c7).offset(0);
make.left.equalTo(c7).offset(0);
make.width.equalTo(@80);
make.height.equalTo(@80);
}];
_labelTempSeven = [[UILabel alloc] init];
[c7 addSubview:_labelTempSeven];
[_labelTempSeven makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(c7).offset(15);
make.centerX.equalTo(c7);
make.width.equalTo(@80);
make.height.equalTo(@30);
}];
_labelSymbolSeven = [[UILabel alloc] init];
[c7 addSubview:_labelSymbolSeven];
[_labelSymbolSeven makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(_labelTempSeven).offset(25);
make.centerX.equalTo(c6);
make.width.equalTo(@80);
make.height.equalTo(@25);
}];
//添加第一个2个label
_labelNameSeven = [[UILabel alloc] init];
_labelNameSeven.text = @"";
_labelNameSeven.textColor = [UIColor blackColor];
[_seven addSubview:_labelNameSeven];
[_labelNameSeven makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(_seven).offset(20);
make.left.equalTo(_seven).offset(130);
make.width.equalTo(@100);
make.height.equalTo(@30);
}];
_labelAddreSeven = [[UILabel alloc] init];
_labelAddreSeven.text = @"";
_labelAddreSeven.textColor = [UIColor blackColor];
[_seven addSubview:_labelAddreSeven];
[_labelAddreSeven makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(_seven).offset(50);
make.left.equalTo(_seven).offset(130);
make.width.equalTo(@100);
make.height.equalTo(@30);
}];
//备用一个
UIView *cys8 = [[UIView alloc] init];
cys8.backgroundColor = [UIColor whiteColor];
[mainView addSubview:cys8];
[cys8 makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(mainView).offset(707);
make.left.equalTo(mainView).offset(0);
make.right.equalTo(mainView).offset(0);
make.bottom.equalTo(mainView).offset(0);
}];
}
-(void)clickLeftButton{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (BOOL)navigationShouldPopOnBackButton{
[self dismissViewControllerAnimated:YES completion:nil];
return YES;
}
/*
* 字符串转为颜色值
*/
- (UIColor *) stringTOColor:(NSString *)str
{
if (!str || [str isEqualToString:@""]) {
return nil;
}
unsigned red,green,blue;
NSRange range;
range.length = 2;
range.location = 1;
[[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&red];
range.location = 3;
[[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&green];
range.location = 5;
[[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&blue];
UIColor *color= [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:1];
return color;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
// 是否支持滑动至顶部
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView
{
return YES;
}
@end
上面的代码 我用的是通过 接收到蓝牙的通知来 更新温度数据的显示。
如果有什么问题可以 联系QQ群 482915243
源码如下:
http://download.csdn.net/download/a1989214/9968056