iOS 蓝牙开发实现文件传输
生活随笔
收集整理的這篇文章主要介紹了
iOS 蓝牙开发实现文件传输
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
??這是一篇舊文,三年前就寫過了,一直沒有時間分享出來,最近簡單整理了下,希望能幫到有需要的人。
??由于我這里沒有相關的藍牙設備,主要用了兩個手機,一個作為主設備,一個做為從設備。另外進行藍牙開發有一個調試利器。
主設備和從設備我分別創建了一個管理類。
主設備主要進行的操作如下:
- 開始掃描設備
- 停止掃描設備
- 連接設備
- 斷開連接設備
- 發送數據
具體源碼如下:
#import <Foundation/Foundation.h> #import <CoreBluetooth/CoreBluetooth.h>NS_ASSUME_NONNULL_BEGIN@interface JKBlueToothCenterHelper : NSObject /// 設備管理者狀態block @property (nonatomic, copy) void(^btStatusBlock)(NSInteger status,NSString *message); /// 設備連接狀態的block @property (nonatomic, copy) void(^btConnectStatusBlock)(CBPeripheral *peripheral, NSError * _Nullable error); /// 斷開鏈接block @property (nonatomic, copy) void(^btDisconnectStatusBlock)(CBPeripheral *peripheral, NSError * _Nullable error); /// 掃描到的設備列表block @property (nonatomic,copy) void(^btScanDevicesBlock)(NSMutableArray <CBPeripheral *>*devices);/// 接收到數據的block @property (nonatomic,copy) void(^receivedDataBlock)(NSData *data, NSError *error);/**開始掃描設備@param services 掃描的服務@param options 掃描的選項配置@param containDefaultService 是否包含默認的服務@return JKBlueToothCenterHelper 對象*/ - (instancetype)initWithScanServices:(nullable NSArray<CBUUID *> *)services options:(nullable NSDictionary <NSString *, id> *)options containDefaultService:(BOOL)containDefaultService;/** 掃描設備*/ - (void)scanDevice;/**停止掃描設備*/ - (void)stopScanDevice;/**連接設備@param peripheral 設備@param options 配置信息*/ - (void)connectToDevice:(CBPeripheral *)peripheral options:(NSDictionary <NSString *, id> *)options;/**斷開連接設備@param peripheral 設備*/ - (void)disconnectToDevice:(CBPeripheral *)peripheral;/**設備管理者向設備發送數據@param data 二進制數據@param peripheral 接受數據的設備@param CBCharacteristic 特征@param type 請求類型*/ - (void)sendData:(NSData *)data toPeripheral:(CBPeripheral *)peripheral forCharacteristic:(CBCharacteristic *)CBCharacteristic type:(CBCharacteristicWriteType)type complete:(void(^)(NSError *error))completeBlock;@endNS_ASSUME_NONNULL_END typedef void(^JKBTCenterSendCompleteBlock)(NSError *error);@interface JKBlueToothCenterHelper()<CBCentralManagerDelegate,CBPeripheralDelegate>@property (nonatomic, strong) CBCentralManager *centerManager; ///< 管理者 @property (nonatomic, strong) NSMutableArray *scanServices; ///< 掃描的服務 @property (nonatomic, strong) NSDictionary <NSString *, id> *scanOptions; ///< 掃描的配置 @property (nonatomic,strong) NSMutableArray *scannedDevices; ///< s掃描到的設備 @property (strong , nonatomic) CBPeripheral * discoveredPeripheral;//周邊設備 @property (nonatomic,copy) JKBTCenterSendCompleteBlock sendCompleteBlock;@end@implementation JKBlueToothCenterHelper - (instancetype)initWithScanServices:(nullable NSArray<CBUUID *> *)services options:(nullable NSDictionary <NSString *, id> *)options containDefaultService:(BOOL)containDefaultService{self = [super init];if (self) {[self.scanServices addObjectsFromArray:services];if (containDefaultService) {[self setupDefalutScanService];}self.scanOptions = options;[self centerManager];}return self; }- (void)setupDefalutScanService{CBUUID *uuid = [CBUUID UUIDWithString:DEFAULT_SERVICE_UUID];[self.scanServices addObject:uuid];}- (void)scanDevice{[self.scannedDevices removeAllObjects];[self.centerManager scanForPeripheralsWithServices:self.scanServices options:self.scanOptions]; }- (void)stopScanDevice{[self.centerManager stopScan]; }- (void)connectToDevice:(CBPeripheral *)peripheral options:(NSDictionary <NSString *, id> *)options{self.discoveredPeripheral = peripheral;self.discoveredPeripheral.delegate = self;[self.centerManager connectPeripheral:peripheral options:options]; }- (void)disconnectToDevice:(CBPeripheral *)peripheral{[self.centerManager cancelPeripheralConnection:peripheral]; }- (void)sendData:(NSData *)data toPeripheral:(CBPeripheral *)peripheral forCharacteristic:(CBCharacteristic *)CBCharacteristic type:(CBCharacteristicWriteType)type complete:(void(^)(NSError *error))completeBlock{self.sendCompleteBlock = completeBlock;[peripheral writeValue:data forCharacteristic:CBCharacteristic type:type]; }#pragma mark - - - - CBCentralManagerDelegate - - - - - (void)centralManagerDidUpdateState:(CBCentralManager *)central{if (@available(iOS 10.0, *)) {switch (central.state) {case CBManagerStatePoweredOn://藍牙打開{[self.centerManager scanForPeripheralsWithServices:self.scanServices options:self.scanOptions];}break;case CBManagerStatePoweredOff://藍牙關閉了{if (self.btStatusBlock) {self.btStatusBlock(CBManagerStatePoweredOff, @"藍牙已關閉,請打開藍牙");}}break;case CBManagerStateUnsupported://不支持{if (self.btStatusBlock) {self.btStatusBlock(CBManagerStatePoweredOff, @"藍牙設備不支持!");}}break;default:break;}} else {// Fallback on earlier versionsswitch (central.state) {case 5://藍牙打開{[self.centerManager scanForPeripheralsWithServices:self.scanServices options:self.scanOptions];}break;case 4://藍牙關閉了{if (self.btStatusBlock) {self.btStatusBlock(4, @"藍牙已關閉,請打開藍牙");}}break;case 2://不支持{if (self.btStatusBlock) {self.btStatusBlock(2, @"藍牙設備不支持!");}}break;default:break;}}}- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI{if (!peripheral) {return;}[peripheral discoverServices:self.scanServices];if (![self.scannedDevices containsObject:peripheral]) {[self.scannedDevices addObject:peripheral];if (self.btScanDevicesBlock) {self.btScanDevicesBlock(self.scannedDevices);}}}- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(nonnull CBPeripheral *)peripheral error:(nullable NSError *)error{if (self.btConnectStatusBlock) {self.btConnectStatusBlock(peripheral, error);} }- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{if (@available(iOS 9.0, *)) {if (self.centerManager.isScanning) {[self stopScanDevice];}} else {// Fallback on earlier versions[self stopScanDevice];}self.discoveredPeripheral = peripheral;[peripheral setDelegate:self];[peripheral discoverServices:nil];if (self.btConnectStatusBlock) {self.btConnectStatusBlock(peripheral, nil);} }- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(nonnull CBPeripheral *)peripheral error:(nullable NSError *)error{if (self.btDisconnectStatusBlock) {self.btDisconnectStatusBlock(peripheral, error);} }//- (void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary<NSString *, id> *)dict{ // //}#pragma mark - - - - CBPeripheralDelegate - - - - - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(nullable NSError *)error{peripheral.delegate = self;NSArray *services = peripheral.services;for (CBService *service in services) {[peripheral discoverCharacteristics:nil forService:service];} }- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(nullable NSError *)error{for (CBCharacteristic *characteristic in service.characteristics) {if ([[NSString stringWithFormat:@"%@",characteristic.UUID] isEqualToString: DEFAULT_CHARACTERISTIC_UUID]) {[self.discoveredPeripheral setNotifyValue:YES forCharacteristic:characteristic];}}}// 寫入成功 - (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error {if (self.sendCompleteBlock) {self.sendCompleteBlock(error);} }-(void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {if (error) {//NSLog(@"訂閱失敗");//NSLog(@"%@",error);}if (characteristic.isNotifying) {//NSLog(@"訂閱成功");} else {//NSLog(@"取消訂閱");} }- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{[peripheral readValueForCharacteristic:characteristic];NSData *data = characteristic.value;if (self.receivedDataBlock) {self.receivedDataBlock(data,error);} }#pragma mark - - - - lazyLoad - - - - - (CBCentralManager *)centerManager{if (!_centerManager) {dispatch_queue_t queue = dispatch_queue_create("com.btCenterManager.queue", 0);CBCentralManager *centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:queue options:nil];centralManager.delegate = self;_centerManager = centralManager;}return _centerManager; }- (NSMutableArray *)scannedDevices{if (!_scannedDevices) {_scannedDevices = [NSMutableArray new];}return _scannedDevices; }- (NSMutableArray *)scanServices{if (!_scanServices) {_scanServices = [NSMutableArray new];}return _scanServices; }@end從設備進行的操作如下:
- 添加服務
- 開始廣播
- 停止廣播
- 發送數據
具體源碼如下:
代碼可以直接復制直接使用。由于我這邊是私有庫,就不開放給大家了。另外文件傳輸時,參考我之前寫的一篇文章《iOS藍牙開發之數據傳輸精華篇》
里面有講到數據拼接的一個工具 ,pod集成如下:
總結
以上是生活随笔為你收集整理的iOS 蓝牙开发实现文件传输的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Django-session的存放位置
- 下一篇: Settings【学习笔记05】