iO蓝牙开发 - BLE(蓝牙4.0)
来源:互联网 发布:淘宝直播可以用电脑吗 编辑:程序博客网 时间:2024/05/21 17:34
设备:
- 中心设备(CBCentralManager):iOS系统的手机等设备
- 外围设备(CBPeripheral):手环等第三方设备
外围设备:
- 服务: 外围设备下的子信息, 每个服务有一个UUID标示.
- 特征:服务下的子信息, 每个特征也有一个UUID标示.特征是外围设备的最小单位,每一个特征信息代表设备的一个信息或者数据, 以手环为例, 每一个特征可能代表手环记录的步数或者电量等信息.
实现步骤:
- 创建中心设备(CBCentralManager)
- 中心设备开始扫描(scanForPeripherals)
- 扫描到外围设备之后, 自动调用中心设备的代理方法(didDiscoverPeripheral)
- 如果设备过多, 可以将扫描到的外围设备添加到数组
- 开始连接, 从数组中过滤出自己想要的设备, 进行连接(connectPeripheral)
- 连接上之后, 自动调用中心设备的代理方法(didConnectPeripheral), 在代理中, 进行查找外围设备的服务(peripheral.discoverServices)
- 查找到服务之后, 自动调用外围设备的代理(didDiscoverServices), 可通过UUID,查找具体的服务,查找服务(discoverCharacteristics)
- 查找到特征之后, 自动调用外围设备的代理(didDiscoverCharacteristics), 通过UUID找到自己想要的特征, 读取特征(readValueForCharacteristic)
- 读取到特征之后, 自动调用外设的代理方法(didUpdateValueForCharacteristic),在这里打印或者解析自己想要的特征值.
代码:
#import "ViewController.h"#import <CoreBluetooth/CoreBluetooth.h>//4个字节Bytes 转 intunsigned int TCcbytesValueToInt(Byte *bytesValue) { unsigned int intV; intV = (unsigned int ) ( ((bytesValue[3] & 0xff)<<24) |((bytesValue[2] & 0xff)<<16) |((bytesValue[1] & 0xff)<<8) |(bytesValue[0] & 0xff)); return intV;}@interface ViewController () <CBCentralManagerDelegate, CBPeripheralDelegate>// 扫描设备- (IBAction)scanPeripheral:(id)sender;// 连接蓝牙- (IBAction)startConnect:(id)sender;// 断开蓝牙- (IBAction)disConnect:(id)sender;// 开始震动- (IBAction)startShake:(id)sender;// 停止震动- (IBAction)stopShake:(id)sender;@property (weak, nonatomic) IBOutlet UILabel *label;// 中心设备@property (nonatomic, strong) CBCentralManager *manager;// 外围设备集合@property (nonatomic, strong) NSMutableArray *peripherals;@end@implementation ViewController- (NSMutableArray *)peripherals{ if (!_peripherals) { self.peripherals = [NSMutableArray array]; } return _peripherals;}- (void)viewDidLoad { [super viewDidLoad]; self.manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];}// 扫描设备- (IBAction)scanPeripheral:(id)sender { NSLog(@"扫描设备..."); // 扫描设备 [self.manager scanForPeripheralsWithServices:nil options:nil];}// 开始连接- (IBAction)startConnect:(id)sender { for (CBPeripheral *peripheral in self.peripherals) { NSLog(@"----->>>>>> peripheral == %@", peripheral); if ([peripheral.name isEqualToString:@"MI"]) { NSLog(@"这是公司的小米手环设备!"); // 中心设备 连接 外围设备 [self.manager connectPeripheral:peripheral options:nil]; } }}// 断开连接- (IBAction)disConnect:(id)sender {}// 开始震动- (IBAction)startShake:(id)sender {}// 停止震动- (IBAction)stopShake:(id)sender {}#pragma mark -当前蓝牙主设备状态- (void)centralManagerDidUpdateState:(CBCentralManager *)central{ switch (central.state) { case CBCentralManagerStateUnknown: NSLog(@">>>CBCentralManagerStateUnknown"); break; case CBCentralManagerStateResetting: NSLog(@">>>CBCentralManagerStateResetting"); break; case CBCentralManagerStateUnsupported: NSLog(@">>>CBCentralManagerStateUnsupported"); break; case CBCentralManagerStateUnauthorized: NSLog(@">>>CBCentralManagerStateUnauthorized"); break; case CBCentralManagerStatePoweredOff: NSLog(@">>>CBCentralManagerStatePoweredOff"); break; default: break; }}// 发现设备会进入方法- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{ // 添加外围设备 if (![self.peripherals containsObject:peripheral]) { // 设置外设的代理 peripheral.delegate = self; [self.peripherals addObject:peripheral]; NSLog(@"扫描连接外设:%@ ",peripheral); self.label.text = [NSString stringWithFormat:@"%@", peripheral]; }}/** * 连接到某个外设的时候调用 */- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{ [central stopScan]; NSLog(@"连接上了"); // 查找外设中的所有服务 [peripheral discoverServices:nil];}#pragma mark - CBPeripheralDelegate/** * 外设已经查找到服务 */- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{ // 遍历所有的服务 for (CBService *service in peripheral.services) { NSLog(@"服务 = %@", service); // 过滤掉不想要的服务 if ([service.UUID.UUIDString isEqual:@"FEE0"]) { // 找到想要的服务 // 扫描服务下面的特征 [peripheral discoverCharacteristics:nil forService:service]; } }}- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error { // 遍历所有的特征 for (CBCharacteristic *characteristic in service.characteristics) { NSLog(@"特征 = %@", characteristic); // 过滤掉不想要的特征 if ([characteristic.UUID.UUIDString isEqual:@"FF0C"]) { // 找到想要的特征 NSLog(@"电量: %@", characteristic); [peripheral readValueForCharacteristic:characteristic]; } }}#pragma mark 设备信息处理//扫描到具体的值- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error{ if (error) { NSLog(@"扫描外设的特征失败!%@-> %@",peripheral.name, [error localizedDescription]); self.title = @"find value error."; return; } NSLog(@"%@ %@",characteristic.UUID.UUIDString,characteristic.value); Byte *bufferBytes = (Byte *)characteristic.value.bytes; int buterys = TCcbytesValueToInt(bufferBytes)&0xff; NSLog(@"电池:%d%%",buterys);}@end
0 0
- iO蓝牙开发 - BLE(蓝牙4.0)
- 蓝牙小试牛刀之蓝牙4.0BLE开发
- iOS蓝牙4.0(BLE)-开发
- 4.0蓝牙 ble开发记录
- iOS蓝牙4.0(BLE)-开发
- iOS:蓝牙BLE 4.0 开发
- Android蓝牙4.0Ble开发
- iOS蓝牙Ble开发
- Android Ble蓝牙开发
- android BLE蓝牙开发
- ios开发蓝牙 BLE
- iOS蓝牙BLE开发
- 蓝牙Ble开发
- android 蓝牙BLE 开发
- Android:BLE蓝牙开发
- android ble蓝牙开发
- android ble蓝牙开发略解-Android 蓝牙4.0开发
- iOS开发之蓝牙4.0 BLE开发
- iOS蓝牙开发 - GameKit框架
- [HAOI2007]理想正方形-单调队列学习笔记
- pat 1088
- caffe faster-rcnn make -j8 报错 ,CentOS
- 简易:ListView滑动隐藏标题栏
- iO蓝牙开发 - BLE(蓝牙4.0)
- Unity中使用鼠标控制相机的视口方向以及移动
- div 滚动条在最下面
- C++——树形DP模板——骑士【浙江省选2008】
- 微信商户平台企业付款到用户找不到没选项开通不了怎么开通
- 【java】 ClassLoader
- 【TVOS】TVOS与android的关系
- 排序算法之简单插入法排序(Java)
- Post请求测试—Postman插件安装