概念 CBPeripheral 蓝牙外设,比如蓝牙手环、蓝牙心跳监视器、蓝牙打印机。 而实际的数据是从蓝牙外设(CBPeripheral),也就是蓝牙手环等这类设备中获取,所以CBPeripheral就相当于是服务器,与他们有些不同的是,蓝牙数据传输是服务器(CBPeripheral)一直在广播发送数据 它可以既广播发送数据,又获取其他设备的数据,但是它只能扮演一种角色,如果iOS 设备A 通过蓝牙主动连接了 设备B,那么设备A是CBCentral,设备B是CBPeripheral;但是如果是设备B连接了设备 A,那么设备B就是CBCentral,设备A是CBPeripheral。 YES]; } 连接某个外设成功后,查找其具有的服务 - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral
CoreBluetooth,初始化中心管理者CBCentralManager并设置CBCentralManagerDelegate 监听CBCentralManager的状态,当是On的时候进行外设搜索 发现外设CBPeripheral // 外设 var peripheral: CBPeripheral? // 外设数组 var peripherals: [CBPeripheral]! / 连接外设的结果 // 1 连接成功 func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral // 2 连接失败 func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral // 3 丢失连接 func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral
*)retrievePeripheralsWithIdentifiers:(NSArray<NSUUID *> *)identifiers; //根据服务id获取所有连接的设备 - (NSArray<CBPeripheral peripheral; //连接外设失败 - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral NSError *)error; //断开外设连接 - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral peripheralManagerIsReadyToUpdateSubscribers:(CBPeripheralManager *)peripheral; 四、中心设备与外设对象CBCentral与CBPeripheral *)peripheral didDiscoverServices:(nullable NSError *)error; //在服务中发现子服务回调的方法 - (void)peripheral:(CBPeripheral
corebluetooth 重要的类与协议 CBCentral:中心设备,发起连接的是CBCentral CBCentralManager:中心设备管理者,类似定位中的CLLocationManager CBPeripheral :外围设备,被连接的设备是CBPeripheral CBService:外围设备的服务,每个外围设备都会提供很多服务 CBCharacteristic:外围设备的特征,包含在每个服务里 CBDescriptor CBCharacteristic的信息或属性 CBCentralManagerDelegate:CBCentralManager对象遵守的协议,用于监视外围设备的发现、连接和检索 CBPeripheralDelegate:CBPeripheral
@property (nonatomic, strong) NSMutableArray *peripherals; // 扫描到的设备 @property (nonatomic, strong) CBPeripheral *cbPeripheral; // 外设状态 @property (nonatomic, assign) CBManagerState peripheralState; @end // 蓝牙4.0设备名 广告信息 @param RSSI 信号强度 */ - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral param error 错误信息 */ - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral @param error 错误信息 */ - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral
接收搜索到打印机的回调 extension ViewController: BaseManagerDelegate { func discoverPeripheral(_ peripheral: CBPeripheral var currentPeripheral: CBPeripheral? var writeCharacteristic: CBCharacteristic! // 2 public func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral () } func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral 蓝牙外围设备代理方法 extension BaseManager: CBPeripheralDelegate { //发现服务 func peripheral(_ peripheral: CBPeripheral
central { } // 扫描到设备 - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral } } } } // 连接成功 - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral } } } // 发现特征的回调, 发现characteristics,由发现服务调用(上一步),获取读和写的characteristics - (void)peripheral:(CBPeripheral 看是否需要执行特定指令,用于判断是否连接成功 [self makeConnectWrite]; } } } //读数据的回调 - (void)peripheral:(CBPeripheral ; // 根据文档,对于数据进行解密或者其他处理,然后通过指定的判断逻辑,判断是否指令是否执行成功 } } //是否写入成功的回调 - (void)peripheral:(CBPeripheral
UIView * _searchGameView; //设备中心管理对象 CBCentralManager * _centerManger; //要连接的外设 CBPeripheral @YES}]; } } //连接外设成功的回调 -(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral options:@{CBConnectPeripheralOptionNotifyOnConnectionKey:@YES}]; } //发现服务后回调的方法 -(void)peripheral:(CBPeripheral forService:service]; break; } } } //开发服务中的特征值后回调的方法 -(void)peripheral:(CBPeripheral _centerWriteChara = characteristic; } } } //所监听的特征值更新时回调的方法 - (void)peripheral:(CBPeripheral
property (nonatomic, strong) CBCentralManager*centralManager; //中心管理者 @property (nonatomic, strong) CBPeripheral 就会自动回调下面的协议方法 /** 发现符合要求的外设,回调 */ - (void)centralManager:(CBCentralManager*)central didDiscoverPeripheral:(CBPeripheral 如果连接成功就会回调这个协议方法 /** 连接成功 */ - (void)centralManager:(CBCentralManager*)central didConnectPeripheral:(CBPeripheral 外设连接失败 /** 连接失败的回调 */ - (void)centralManager:(CBCentralManager*)central didFailToConnectPeripheral:(CBPeripheral //丢失连接 掉线 /** 断开连接 */ - (void)centralManager:(CBCentralManager*)central didDisconnectPeripheral:(CBPeripheral
scanForPeripheralsWithServiceUUIDs:nil options:nil didDiscoverPeripheral:^(CBCentralManager *central, CBPeripheral scanForPeripheralsWithServiceUUIDs:nil options:nil didDiscoverPeripheral:^(CBCentralManager *central, CBPeripheral 这里给出了三种方式,前两种方式都需要先设置好搜索到蓝牙外设之后的回调, 即: manager.discoverPeripheralBlcok = ^(CBCentralManager *central, CBPeripheral self.deviceArray.count; i++) { NSDictionary *dict = [self.deviceArray objectAtIndex:i]; CBPeripheral servicesOptions:nil characteristicsOptions:nil completeBlock:^(HLOptionStage stage, CBPeripheral
eventWaitHandle.WaitOne(); } public override void DiscoveredPeripheral(CBCentralManager central, CBPeripheral private BluetoothDevice(CBPeripheral peripheral) { _peripheral = peripheral; } public static implicit operator BluetoothDevice(CBPeripheral peripheral) { null : new BluetoothDevice(peripheral); } public static implicit operator CBPeripheral
//把数据写到Characteristic中//写数据-(void)writeCharacteristic:(CBPeripheral *)peripheral characteristic
Swift版)如下: // 第7、8步: // 发现特征的回调(委托)方法(假设在这之前已经「成功连接」、「发现服务」) func peripheral(peripheral: CBPeripheral } } // 第9步: // 最终,蓝牙发过来的数据,我们会在这个回调方法中拿到 func peripheral(peripheral: CBPeripheral // 也就是上面的第8、9两步合在一个方法中了 [_baby notify:peripheral characteristic:_dataOutCharacteristic block:^(CBPeripheral
手机每成功连接一个设备(某事件被触发),这个方法都会被调用(回调) - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral } // 成功连接一个蓝牙设备的回调(官方框架) - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral MyCnetralManager.m文件 // 成功连接一个蓝牙设备的回调(官方框架) - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral MyCnetralManager.m文件 // 成功连接一个蓝牙设备的回调(官方框架) - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral
babyDelegate{ //设置扫描到设备的委托 [baby setBlockOnDiscoverToPeripherals:^(CBCentralManager *central, CBPeripheral
特点:提供CBCentralManager和CBPeripheral类,用于设备管理和数据传输。支持多设备连接和数据更新通知。需要处理异步回调,开发复杂度较高9。
(assign, nonatomic) BOOL IsreqGetCurrentSysUsering; /** 蓝牙打印机的连接信息 */ @property(nonatomic,strong)CBPeripheral