// // ENBPDevice.m // ENBLEProject // // Created by lvwang2002 on 16/4/8. // Copyright © 2016年 Facebook. All rights reserved. // #import "ENBPDevice.h" #define kStartCheck @"开始测量" #define kStopCheck @"停止测量" // 发给血压计 (带校验码) Byte SEND_CHECK_POWER_PRESSURE[] = { 0xcc, 0x85, 0x02, 0x03, 0x04, 0x04, 0x00, 0x01 };// 查询电量 Byte SEND_CONNECT_PRESSURE[] = { 0xcc, 0x85, 0x02, 0x03, 0x01, 0x01, 0x00, 0x01 };// 连接血压计 Byte SEND_START_PRESSURE[] = { 0xcc, 0x85, 0x02, 0x03, 0x01, 0x02, 0x00, 0x02 };// 开始测量 Byte SEND_STOP_PRESSURE[] = { 0xcc, 0x85, 0x02, 0x03, 0x01, 0x03, 0x00, 0x03 };// 停止测量 Byte SEND_CLOSE_PRESSURE[] = { 0xcc, 0x85, 0x02, 0x03, 0x01, 0x04, 0x00, 0x04 };// 关仪器 // 收到血压计之后(血压计还会回复指令)匹配是否成功的字节数组 (带校验码) Byte RECEIVE_CHECK_POWER_PRESSURE[] = { 0xaa, 0x85, 0x02, 0x04, 0x04, 0x04 };// 查询电量(数组中没有放入校验码,和数据,共3个字节) Byte RECEIVE_CONNECT_PRESSURE[] = { 0xaa, 0x85, 0x02, 0x03, 0x01, 0x01, 0x00, 0x01 };// 连接血压计 Byte RECEIVE_START_PRESSURE[] = { 0xaa, 0x85, 0x02, 0x03, 0x01, 0x02, 0x00, 0x02 };// 开始测量 Byte RECEIVE_STOP_PRESSURE[] = { 0xaa, 0x85, 0x02, 0x03, 0x01, 0x03, 0x00, 0x03 };// 停止测量 Byte RECEIVE_CLOSE_PRESSURE[] = { 0xaa, 0x85, 0x02, 0x03, 0x01, 0x04, 0x00, 0x04 };// 关仪器 Byte RECEIVE_RESULT[] = { 0xaa, 0x85, 0x02, 0x0f, 0x01};// 接收结果 Byte error_state_a = 0x01;// 请检查血压计佩戴是否正确 Byte error_state_b = 0x02;// 血压计袖带过松或漏气 Byte error_state_c = 0x04;// 请保持安静 ,重新测量 Byte error_state_d = 0x07;// 血压计电量低,请充电 Byte error_state_e = 0x15;// 测量出错,请重新测量 EE21 @implementation ENBPDevice{ BOOL _connected; NSArray *_characteristics; } -(id)initWithPeripheral:(CBPeripheral *)peripheral WithCentralManager:(CBCentralManager *)centerManager WithDevice:(BLEDevice *)device{ self = [super initWithPeripheral:peripheral WithCentralManager:centerManager WithDevice:device]; if(self){ _connected = NO; } return self; } /** * 4.发现外围服务 * * @param peripheral 外围设备 * @param error 错误信息 */ - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{ if (error){ NSLog(@"didDiscoverServices : %@", [error localizedDescription]); // [self cleanup]; return; } for (CBService * service in _peripheral.services) { /** * 查找自己需要的UUID的服务 */ if ([[_device.advertisementData[@"kCBAdvDataServiceUUIDs"] firstObject] isEqual:service.UUID]) { /** * 查找外围设备特性 */ [_peripheral discoverCharacteristics:nil forService:service]; return; } } } /** * 5.发现外围设备特性 * * @param peripheral 外围设备 * @param service 外围设备服务 * @param error 错误信息 */ - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{ if (error){ NSLog(@"didDiscoverCharacteristicsForService error : %@", [error localizedDescription]); return; } _characteristics = service.characteristics; /** * 开启通知(外围设备发数据能收到) service.characteristics 里面两个FFF1(通知的) FFF2(写入的) */ [_peripheral setNotifyValue:YES forCharacteristic:[service.characteristics firstObject]]; [_peripheral writeValue:[NSData dataWithBytes:SEND_CONNECT_PRESSURE length:8] forCharacteristic:[service.characteristics lastObject] type:CBCharacteristicWriteWithoutResponse]; } /** * 6.发送数据到外围设备回调 * * @param peripheral 外围设备 * @param characteristic 外围设备特性 * @param error 错误信息 */ - (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { NSLog(@"%@---",error); } /** * 6.外围设备发送数据到蓝牙中心 * * @param peripheral 外围设备 * @param characteristic 外围设备特性 * @param error 错误信息 */ - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { // byte error_state_a = 0x01;// 请检查血压计佩戴是否正确 // byte error_state_b = 0x02;// 血压计袖带过松或漏气 // byte error_state_c = 0x04;// 请保持安静 ,重新测量 // byte error_state_d = 0x07;// 血压计电量低,请充电 // // byte error_state_e = 0x15;// 测量出错,请重新测量 EE21 NSData *data = characteristic.value; Byte * byte = (Byte *)[data bytes]; //判断是否是连接 { NSData * connectData = [NSData dataWithBytes:RECEIVE_CONNECT_PRESSURE length:8]; BOOL flag = NO; if (connectData.length == data.length) { for (int i=0; i= powerData.length) { for (NSInteger i = 0; i= resultData.length) { for (NSInteger i = 0; i