iOS蓝牙4.0开发
2016-03-15 13:52
253 查看
<pre name="code" class="objc">//蓝牙系统库 #import <CoreBluetooth/CoreBluetooth.h> //必须要由UUID来唯一标示对应的service和characteristic #define kServiceUUID @"5C476471-1109-4EBE-A826-45B4F9D74FB9" #define kCharacteristicHeartRateUUID @"82C7AC0F-6113-4EC9-92D1-5EEF44571398" #define kCharacteristicBodyLocationUUID @"537B5FD6-1889-4041-9C35-F6949D1CA034" @interface ViewController ()<CBCentralManagerDelegate,CBPeripheralDelegate> @property (nonatomic,strong)CBCentralManager * <span style="font-family: Arial, Helvetica, sans-serif;">manager; </span> @property (nonatomic,strong)CBPeripheral * peripheral; @end
1.建立中心角色
#import <CoreBluetooth/CoreBluetooth.h> - (void)viewDidLoad { [super viewDidLoad]; //初始化蓝牙 central manager _centralManager = [[CBCentralManager alloc]initWithDelegate:self queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) options:nil]; }
2.扫描外设(discover)
[manager scanForPeripheralsWithServices:nil options:options];
3.连接外设
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI { if([peripheral.name isEqualToString:BLE_SERVICE_NAME]){ [self connect:peripheral]; } s); } -(BOOL)connect:(CBPeripheral *)peripheral{ self.manager.delegate = self; [self.manager connectPeripheral:peripheral options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]]; }
4.扫描外设中的服务和特征(discover)
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral { NSLog(@"Did connect to peripheral: %@", peripheral); _testPeripheral = peripheral; [peripheral setDelegate:self]; <br>//查找服务 [peripheral discoverServices:nil]; }
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error { NSLog(@"didDiscoverServices"); if (error) { NSLog(@"Discovered services for %@ with error: %@", peripheral.name, [error localizedDescription]); if ([self.delegate respondsToSelector:@selector(DidNotifyFailConnectService:withPeripheral:error:)]) [self.delegate DidNotifyFailConnectService:nil withPeripheral:nil error:nil]; return; } for (CBService *service in peripheral.services) { //发现服务 if ([service.UUID isEqual:[CBUUID UUIDWithString:UUIDSTR_ISSC_PROPRIETARY_SERVICE]]) { NSLog(@"Service found with UUID: %@", service.UUID); <br>//查找特征 [peripheral discoverCharacteristics:nil forService:service]; break; } } }
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error { if (error) { NSLog(@"Discovered characteristics for %@ with error: %@", service.UUID, [error localizedDescription]); [self error]; return; } NSLog(@"服务:%@",service.UUID); for (CBCharacteristic *characteristic in service.characteristics) { //发现特征 if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"xxxxxxx"]]) { NSLog(@"监听:%@",characteristic);<br>//监听特征 [self.peripheral setNotifyValue:YES forCharacteristic:characteristic]; } } }
5.与外设做数据交互
读
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { if (error) { NSLog(@"Error updating value for characteristic %@ error: %@", characteristic.UUID, [error localizedDescription]); self.error_b = BluetoothError_System; [self error]; return; } // NSLog(@"收到的数据:%@",characteristic.value); [self decodeData:characteristic.value]; }写
NSData *d2 = [[PBABluetoothDecode sharedManager] HexStringToNSData:@"0x02"]; [self.peripheral writeValue:d2 forCharacteristic:characteristic type:CBCharacteristicWriteWithoutResponse];
相关文章推荐
- iOS学习之block(一)
- iOS - coreData
- iOS 打开系统打电话 发短信 邮件 浏览器 地图
- iOS开发中那些高效常用的宏
- iOS NSRuntime机制
- 利用runtime懒人实现iOS 防止按钮连续点击 连续响应事件,可以自己设置时间间隔,拖进工程中立即生效
- IOS Cocoapods安装及使用
- ios 毛玻璃效果
- iOS开发小技巧
- iOS 友盟分享
- iOS 字典与字符串的相互转换
- iOS Tabbar和Naviagtion
- iOS cell分割线长度设置
- iOS8 添加毛玻璃效果
- iOS 文件读写
- iOS创建单例的两种方法
- iOS中关于界面的亮度的应用
- iOS 获取沙盒目录
- iOS正则表达式,手机号、邮箱验证
- ios 开发中 developer tools access 总是要输入密码问题的解决