蓝牙iOS

2017-04-07  本文已影响0人  涛涛灬灬

(转)http://blog.sina.com.cn/s/blog_147d0371c0102vzut.html

项目源码

import

@protocol connectDeviceDelegate <<span style="color: rgb(52, 149, 175);">NSObject>

@end

@interface DataCollectViewController : UIViewController

@property(nonatomic,assign)UINavigationController *nav;
@property(nonatomic,assign)id delegate;

@property (nonatomic, weak) id <<span style="color: rgb(52, 149, 175);">connectDeviceDelegate> connectDelegate;

@end

-------------------------------------------------- 分割线 ------

import "DataCollectViewController.h"

import

import "BLECtl.h"

import "Constants.h"

import "AppUtilities.h"

import "CharacteristicReader.h"

@interface DataCollectViewController ()<</span>CBCentralManagerDelegate,CBPeripheralDelegate,BLECtlDelegate,UIAlertViewDelegate>
{
CBUUID *bpmServiceUUID;
CBUUID *bpmBloodPressureMeasurementCharacteristicUUID;
CBUUID *bpmIntermediateCuffPressureCharacteristicUUID;
CBUUID *batteryServiceUUID;
CBUUID *batteryLevelCharacteristicUUID;

__weak IBOutlet UIButton *setBtn;
__weak IBOutlet UIButton *clearBtn;
__weak IBOutlet UIButton *saveBtn;

__weak IBOutlet UIButton *kongBtn;
__weak IBOutlet UIButton *canBtn;

__weak IBOutlet UILabel *titleLB;
__weak IBOutlet UITextField *highTF;
__weak IBOutlet UITextField *lowTF;
__weak IBOutlet UITextField *pulseTF;
__weak IBOutlet UITextField *sugarTF;
__weak IBOutlet UITextField *tempuratureTF;

__weak IBOutlet UIView *personsView;
__weak IBOutlet UITableView *personTV;

__weak IBOutlet UIScrollView *contentView;

NSString *bloodId;
NSString *sugarId;

NSString *gluType;
NSMutableArray *personList;

}

@property (nonatomic, strong) CBCentralManager *centralManager;
@property (nonatomic, strong) CBPeripheral *discoveredBloodPeripheral;
@property (nonatomic, strong) CBPeripheral *discoveredSugarPeripheral;
@property (nonatomic, weak) NSTimer *connectTimer;//重新连接血糖仪的定时器
@property (nonatomic, assign) BOOL isFirstConnectSuger;
@property (nonatomic, assign) BOOL isConnectedBlood;
@property (nonatomic, assign) BOOL isConnectedSugar;
@property (nonatomic, strong) NSThread *bloodThread;
@property (nonatomic, strong) NSThread *sugarThread;

@end

@implementation DataCollectViewController
@synthesize delegate,nav;

//初始化蓝牙相关信息
-(void)initBluetooth
{
bpmServiceUUID = [CBUUID UUIDWithString:bpmServiceUUIDString];
bpmBloodPressureMeasurementCharacteristicUUID = [CBUUID UUIDWithString:bpmBloodPressureMeasurementCharacteristicUUIDString];
bpmIntermediateCuffPressureCharacteristicUUID = [CBUUID UUIDWithString:bpmIntermediateCuffPressureCharacteristicUUIDString];
batteryServiceUUID = [CBUUID UUIDWithString:batteryServiceUUIDString];
batteryLevelCharacteristicUUID = [CBUUID UUIDWithString:batteryLevelCharacteristicUUIDString];

self.isConnectedBlood = NO;
self.isConnectedSugar = NO;

self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

}

//开始查看服务,蓝牙开启

//搜索蓝牙设备

//查到外设后,启动线程连接设备

    if (name.length>=19&&[[name substringToIndex:19] isEqualToString:@"Yuwell BloodPressur"]&&self.isConnectedBlood==NO) {
        NSLog(@"1 thread start,连接血压");
        self.discoveredBloodPeripheral = peripheral;
        _bloodThread=[[NSThread alloc]initWithTarget:self selector:@selector(conn:) object:peripheral];
        [_bloodThread start];
        self.isConnectedBlood = YES;
    }

    if ([name isEqual:@"Sinocare"]&&self.isConnectedSugar==NO) {
        NSLog(@"2 thread start,连接血糖");
        self.discoveredSugarPeripheral = peripheral;
        _sugarThread=[[NSThread alloc]initWithTarget:self selector:@selector(conn:) object:peripheral];
        [_sugarThread start];
        self.isConnectedSugar = YES;
    }

}

//连接外设成功,开始发现服务

//连接外设失败

//已发现服务

//已搜索到Characteristics

//获取外设发来的数据,不论是read和notify,获取数据都是从这个方法中读取。

//清空

//保存

//选择空腹

//选择餐后2小时

//点击空白处隐藏人员页面

-(BOOL)isTrue:(UITextField *)textField replacementString:(NSString *)string charRange:(NSRange)range
{
BOOL isHaveDian = YES;
if ([textField.text rangeOfString:@"."].location==NSNotFound) {
isHaveDian=NO;
}
if ([string length]>0){
unichar single=[string characterAtIndex:0];//当前输入的字符
if ((single >='0' && single<='9') || single=='.')//数据格式正确
{
//首字母不能为小数点
if([textField.text length]==0&&single=='.'){
return NO;
}
//在最前端不能插入.
if ([textField.text length]>0&&range.location==0&&single == '.') {
return NO;
}
if (single=='.'){
if(!isHaveDian){
isHaveDian=YES;
return YES;
}else{
return NO;
}
}else{
if (isHaveDian){
//小数点的位数
NSRange ran=[textField.text rangeOfString:@"."];
if(range.location>ran.location){
NSUInteger tt=[textField.text length]-1-ran.location;
if (tt < 1){
return YES;
}else{
return NO;
}
}else{
if(ran.location< 2){
return YES;
}else{
return NO;
}
}
}else{
if([textField.text length]<2){
return YES;
}else{
return NO;
}
}
}
}else{//输入的数据格式不正确
return NO;
}
}else{
return YES;
}
}

@end

上一篇 下一篇

猜你喜欢

热点阅读