ios 蓝牙(GameKit CoreBluetooth)
利用GameKit框架实现ios设备的蓝牙通讯,导入框架:#import <GameKit/GameKit.h> , 注意: 此框架只能用于ios设置间蓝牙通讯
如今苹果开放了接口来实现不仅限于在苹果设备间的蓝牙通讯,需要导入框架:#import <CoreBluetooth/CoreBluetooth.h>
首先,利用GameKit框架实现ios设备蓝牙通讯
基本属性和方法:
属性:
- 是否可见:visible
- 蓝牙连接类型:connectionTypesMask
方法:
- 显示蓝牙控制器:- (void)show;
- 隐藏蓝牙控制器:- (void)dismiss;
- 蓝牙连接成功会调用:- (void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *)session
- 取消蓝牙连接:- (void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker
- 接收到数据会调用:- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context
- 发送数据给特定的对端蓝牙设备:self.session sendData:<#(NSData *)#> toPeers:<#(NSArray *)#> withDataMode:<#(GKSendDataMode)#> error:<#(NSError *__autoreleasing *)#>
- 发送数据给所有蓝牙设备:self.session sendDataToAllPeers:<#(NSData *)#> withDataMode:<#(GKSendDataMode)#> error:<#(NSError *__autoreleasing *)#>
实现:
1 #import "WYSViewController.h" 2 #import <GameKit/GameKit.h> 3 4 @interface WYSViewController () <GKPeerPickerControllerDelegate> 5 6 // 会话 7 @property (nonatomic,strong) GKSession *session; 8 9 @end 10 11 @implementation WYSViewController 12 13 - (void)viewDidLoad 14 { 15 [super viewDidLoad]; 16 17 } 18 19 - (void)didReceiveMemoryWarning 20 { 21 [super didReceiveMemoryWarning]; 22 // Dispose of any resources that can be recreated. 23 } 24 25 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 26 { 27 28 // 创建蓝牙控制器 29 GKPeerPickerController *peerPk = [[GKPeerPickerController alloc] init]; 30 31 // 代理 32 peerPk.delegate = self; 33 34 // 显示蓝牙控制器 35 [peerPk show]; 36 } 37 38 #pragma mark - 蓝牙代理方法 39 // 蓝牙连接成功会调用 40 - (void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *)session 41 { 42 // 保存回话 43 self.session = session; 44 45 // 设置接收者 46 [self.session setDataReceiveHandler:self withContext:nil]; 47 48 49 // 关闭控制器 50 [picker dismiss]; 51 } 52 53 // 取消连接调用 54 - (void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker 55 { 56 57 } 58 59 // 接收到数据会调用 60 - (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context 61 { 62 NSLog(@"%@",data); 63 } 64 65 66 - (IBAction)sendData 67 { 68 // 发送数据去某些蓝牙设备 69 // NSData * :要发送的数据 70 // NSArray * : 蓝牙设备 71 // GKSendDataMode:枚举模式 72 // self.session sendData:<#(NSData *)#> toPeers:<#(NSArray *)#> withDataMode:<#(GKSendDataMode)#> error:<#(NSError *__autoreleasing *)#> 73 74 // 发送数据去所有蓝牙设备 75 // self.session sendDataToAllPeers:<#(NSData *)#> withDataMode:<#(GKSendDataMode)#> error:<#(NSError *__autoreleasing *)#> 76 }
利用CoreBluetooth实现蓝牙通讯:
基本的思路:创建中心设备-->扫描外设-->连接外设-->扫描外设的服务和特征-->利用特征和外设进行数据交互-->断开连接
基本方法和属性:
属性:
- 中心管理设备类:CBCentralManager
- 中心管理设备代理:id<CBCentralManagerDelegate> delegate
- 中心管理设备状态:CBCentralManagerState state
- 外设类:CBPeripheral
- 服务类:CBService
- 特征类:CBCharacteristic
方法:
- 扫描外设:- (void)scanForPeripheralsWithServices:(NSArray *)serviceUUIDs options:(NSDictionary *)options;
- 停止扫描:- (void)stopScan;
- 连接外设:- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
- 外设连接成功调用:- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
- 外设连接失败调用:- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
- 扫描到服务就会调用:- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
- 扫描到特征就会调用:- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
具体实现:
1 #import "WYSViewController.h" 2 #import <CoreBluetooth/CoreBluetooth.h> 3 4 @interface WYSViewController () <CBCentralManagerDelegate,CBPeripheralDelegate> 5 6 // 中心设备 7 @property (nonatomic,strong) CBCentralManager *cbMgr; 8 9 // 全部的外部设备 10 @property (nonatomic,strong) NSMutableArray *Peripherals; 11 12 @end 13 14 @implementation WYSViewController 15 16 // 懒加载 17 - (NSMutableArray *)Peripherals 18 { 19 if (!_Peripherals){ 20 21 _Peripherals = [NSMutableArray array]; 22 } 23 24 return _Peripherals; 25 } 26 27 - (void)viewDidLoad 28 { 29 [super viewDidLoad]; 30 31 32 // 创建中心设备 33 CBCentralManager *cbMgr = [[CBCentralManager alloc] init]; 34 self.cbMgr = cbMgr; 35 36 // 代理 37 cbMgr.delegate = self; 38 39 // 扫描外设 40 [cbMgr scanForPeripheralsWithServices:nil options:nil]; 41 } 42 43 44 #pragma mark - CBCentralManager代理方法 45 - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI 46 { 47 // 扫描到得外设 48 if (![self.Peripherals containsObject:peripheral]){ 49 50 // 外设代理 51 peripheral.delegate = self; 52 53 [self.Peripherals addObject:peripheral]; 54 } 55 } 56 57 // 连接外设 58 - (IBAction)peripheralConnectSuccess 59 { 60 for (CBPeripheral *peripheral in self.Peripherals) { 61 62 // 连接外设 63 [self.cbMgr connectPeripheral:peripheral options:nil]; 64 } 65 } 66 67 68 // 外设连接成功会调用 69 - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral 70 { 71 // 扫描服务 72 [peripheral discoverServices:nil]; 73 } 74 75 // 外设连接失败 76 77 - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error 78 { 79 80 } 81 82 #pragma mark - Peripheral代理 83 // 扫描到服务就会调用 84 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error 85 { 86 NSArray *services = peripheral.services; 87 for (CBService *service in services) { 88 89 // 某服务存在 90 if ([service.UUID.UUIDString isEqualToString:@"xxx"]){ 91 92 [peripheral discoverCharacteristics:nil forService:service]; 93 } 94 } 95 } 96 97 // 扫描到特征就会调用 98 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error 99 { 100 NSArray *characters = service.characteristics; 101 102 for (CBCharacteristic *character in characters) { 103 104 if ([character.UUID.UUIDString isEqualToString:@"xxx"]){ 105 106 // 进行设置 107 } 108 } 109 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。