iOS 蓝牙(CoreBluetooth)
来源:互联网 发布:服装之星v8数据库补丁 编辑:程序博客网 时间:2024/06/04 18:15
iOS中提供了4个框架用于实现蓝牙连接
GameKit.framework(用法简单)
只能用于iOS设备之间的连接,多用于游戏(比如五子棋对战),从iOS7开始过期MultipeerConnectivity.framework
只能用于iOS设备之间的连接,从iOS7开始引入ExternalAccessory.framework
可用于第三方蓝牙设备交互,但是蓝牙设备必须经过苹果MFi认证(国内较少)CoreBluetooth.framework(时下热门)
可用于第三方蓝牙设备交互,必须要支持蓝牙4.0
硬件至少是4s,系统至少是iOS6
蓝牙4.0以低功耗著称,一般也叫BLE(BluetoothLowEnergy)
目前应用比较多的案例:运动手坏、嵌入式设备、智能家居
实现步骤
- 导入框架 CoreBluetooth.framework
- 建立中央管理者
- 扫描周边设备
- 链接扫描到的设备
- 扫描服务
- 扫描特征
- 根据需求进行数据的一个处理
代码实现
#import "ViewController.h"#import <CoreBluetooth/CoreBluetooth.h>@interface ViewController () <CBCentralManagerDelegate, CBPeripheralDelegate>@property (strong, nonatomic) CBCentralManager *centralManager; // 中央管理者@property (strong, nonatomic) NSMutableArray *peripheralArray;; // 扫描到的外设@end@implementation ViewController- (NSMutableArray *)peripheralArray { if (_peripheralArray == nil) { _peripheralArray = [NSMutableArray array]; } return _peripheralArray;}- (void)viewDidLoad { [super viewDidLoad]; // 1.建立中央管理者 // queue:传空,代表的就是在主队列 self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; //2.扫描周边设备 // Services:服务的UUID,是一个数据.如果传nil,默认就会扫描全部所有的服务 [self.centralManager scanForPeripheralsWithServices:nil options:nil];}#pragma mark - CBCentralManager代理方法- (void)centralManagerDidUpdateState:(CBCentralManager *)central { NSLog(@"state: %ld",(long)central.state);}/** 当发现外围设备时,会调用这个方法 @param central 控制中心 @param peripheral 外围设备 @param advertisementData 相关数据 @param RSSI 信号的强度 */- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI { // 3.记录扫描到的设备 if (![self.peripheralArray containsObject:peripheral]) { [self.peripheralArray addObject:peripheral]; } // 伪步骤.用一个列表显示咋们检测到的外设备. // 4.链接扫描到的设备 [self.centralManager connectPeripheral:peripheral options:nil]; // 5.设置外围设备的一个代理 peripheral.delegate = self;}#pragma mark - 连接到设备之后,会调这方法- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral { // 6.扫描服务 [peripheral discoverServices:nil];}#pragma mark - 外设的代理方法.当发现到读物的时候会调用- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error { // 7. 获取制定的服务,根据这个服务来查找特征 //services:外设的所有服务,会保存在一个servicse中 for (CBService *service in peripheral.services) { if ([service.UUID.UUIDString isEqualToString:@"123"]) { // UUID一直的话,就开始扫描 [peripheral discoverCharacteristics:nil forService:service]; } }}- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(nullable NSError *)error { // 8.获取制定特征 for (CBCharacteristic *characteristic in service.characteristics) { if ([characteristic.UUID.UUIDString isEqualToString:@"789"]) { // 9.根据需求进行数据的一个处理 // 如果获取到了指定的特征,就可以进行数据交换了 [peripheral readValueForCharacteristic:characteristic]; } }}#pragma mark - 断开- (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; // 10.最后断开连接 [self.centralManager stopScan];}
阅读全文
0 0
- iOS蓝牙CoreBluetooth介绍
- ios蓝牙开发(CoreBluetooth)
- iOS蓝牙开发--CoreBluetooth
- iOS 蓝牙 Corebluetooth
- iOS 蓝牙入门CoreBluetooth
- iOS 蓝牙(CoreBluetooth)
- ios corebluetooth蓝牙4.0事例
- iOS蓝牙框架CoreBluetooth Framework
- iOS--蓝牙 ble4.0 CoreBluetooth
- ios 蓝牙4.0开发(CoreBlueTooth)
- iOS蓝牙框架CoreBluetooth应用
- ios蓝牙框架CoreBluetooth使用
- iOS开发 CoreBluetooth 蓝牙4.0
- iOS 学习小菜鸟之蓝牙corebluetooth
- IOS开发-使用蓝牙(CoreBluetooth.f…
- iOS- 蓝牙集成GameKit,MultipeerConnectivity,CoreBluetooth
- iOS - 蓝牙库CoreBluetooth核心内容之central
- iOS蓝牙4.0 CoreBluetooth框架开发
- 锁定竖屏
- thinkPhp3.2 field和getField区别
- 多线程运用场景
- LeetCode:M-3 Sum
- freertos第三课,关于串口用于可视化调试的问题
- iOS 蓝牙(CoreBluetooth)
- HttpClient 4.5.2版本设置连接超时时间-CloseableHttpClient设置Timeout
- java8-bifunction源码
- android 耳机接口设备无法识别问题
- 深入java 序列化和反序列化基本用法
- 面向对象7大设计原则
- 企业Docker的使用流程以及常用命令
- JDBC
- win10+cuda8.0+matlab2014a+caffe配置经历记录