读书笔记---蓝牙(CoreBluetooth)

在CoreBluetooth框架中,有两个主要的角色:外设
摘要由CSDN通过智能技术生成

在CoreBluetooth框架中,有两个主要的角色:外设和中心,整个框架都是围绕这两个主要角色设计的.


中心角色变成实现过程

1.设置CBCentralManager

2.发现并连接外设

3.发现服务

4.发现特征

5.预定特征通知

6.读取数据




#import <UIKit/UIKit.h>
#import <CoreBluetooth/CoreBluetooth.h>


#define TRANSFER_SERVICE_UUID           @"D63D44E5-E798-4EA5-A1C0-3F9EEEC2CDEB"
#define TRANSFER_CHARACTERISTIC_UUID    @"1652CAD2-6B0D-4D34-96A0-75058E606A98"


@interface ViewController : UIViewController <CBCentralManagerDelegate, CBPeripheralDelegate>

@property (strong, nonatomic) CBCentralManager      *centralManager;//CBCentralManager 是用来管理BLE的中心设备
@property (strong, nonatomic) CBPeripheral          *discoveredPeripheral;
@property (strong, nonatomic) NSMutableData         *data;

@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicatorView;
@property (weak, nonatomic) IBOutlet UILabel *scanLabel;

@property (weak, nonatomic) IBOutlet UITextView *textView;

@end




@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 设置CBCentralManager
    _centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
    
    // 保存接收数据
    _data = [[NSMutableData alloc] init];
}

- (void)viewWillDisappear:(BOOL)animated
{
    
    [self.centralManager stopScan];
    
    [self.activityIndicatorView stopAnimating];
    
    NSLog(@"扫描停止");
    
    [super viewWillDisappear:animated];
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - Central Methods

//设置成功回调方法
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
    if (central.state != CBCentralManagerStatePoweredOn) {
        return;
    }
    //开始扫描
    [self scan];
    
}


/** 通过制定的128位的UUID,扫描外设
 */
- (void)scan
{
    //扫描
    [self.centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]]
                                                options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];
    [self.activityIndicatorView startAnimating];
    self.scanLabel.hidden = NO;
    
    NSLog(@"Scanning started");
}

/** 停止扫描
 */
- (void)stop
{
    [self.centralManager stopScan];
    
    [self.activityIndicatorView stopAnimating];
    self.scanLabel.hidden = YES;
    self.textView.text = @"";
  
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的Objective-C代码示例,展示如何实现一个iOS设备同时连接多个设备,并支持不同设备之间的通信功能。需要注意的是,此代码仅作为示例,实际应用中需要根据具体需求进行修改和优化。 ```objective-c // 导入CoreBluetooth框架 #import <CoreBluetooth/CoreBluetooth.h> // 实现CBCentralManagerDelegate协议 @interface ViewController () <CBCentralManagerDelegate> @property (nonatomic, strong) CBCentralManager *centralManager; @property (nonatomic, strong) NSMutableArray *connectedPeripherals; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 初始化CBCentralManager self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; // 初始化已连接的设备列表 self.connectedPeripherals = [NSMutableArray array]; } // 扫描设备 - (void)scanForPeripherals { NSDictionary *options = @{CBCentralManagerScanOptionAllowDuplicatesKey: @YES}; [self.centralManager scanForPeripheralsWithServices:nil options:options]; } // 连接指定的设备 - (void)connectPeripheral:(CBPeripheral *)peripheral { [self.centralManager connectPeripheral:peripheral options:nil]; } // 发送数据到指定的设备 - (void)sendData:(NSData *)data toPeripheral:(CBPeripheral *)peripheral { CBCharacteristic *characteristic = // 获取用于通信的特征 [peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse]; } #pragma mark - CBCentralManagerDelegate // 状态改变 - (void)centralManagerDidUpdateState:(CBCentralManager *)central { if (central.state == CBManagerStatePoweredOn) { // 已打开,开始扫描设备 [self scanForPeripherals]; } else { // 未打开,提示用户打开 } } // 扫描到设备 - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI { // 将扫描到的设备添加到列表中 [self.connectedPeripherals addObject:peripheral]; // 连接设备 [self connectPeripheral:peripheral]; } // 设备已连接 - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral { // 发现设备的服务和特征 peripheral.delegate = self; [peripheral discoverServices:nil]; } #pragma mark - CBPeripheralDelegate // 发现设备的服务 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error { if (error) { // 发现服务失败,提示用户 return; } // 遍历所有服务,发现服务的特征 for (CBService *service in peripheral.services) { [peripheral discoverCharacteristics:nil forService:service]; } } // 发现服务的特征 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error { if (error) { // 发现特征失败,提示用户 return; } // 遍历所有特征,找到用于通信的特征 for (CBCharacteristic *characteristic in service.characteristics) { if (characteristic.properties & CBCharacteristicPropertyWrite) { // 连接成功,可以开始进行通信了 // ... } } } @end ``` 以上代码实现了一个简单的iOS设备同时连接多个设备,并支持不同设备之间的通信功能。在实际应用中,还需要根据具体需求进行进一步的优化和修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值