iOS蓝牙——test

来源:互联网 发布:台湾大学知乎 编辑:程序博客网 时间:2024/06/07 15:31

#import "ViewController.h"

#import <CoreBluetooth/CoreBluetooth.h>


@interface ViewController ()<CBCentralManagerDelegate,CBPeripheralDelegate>


@end


@implementation ViewController{

    //设备的集合

    NSMutableArray *array;

    //服务的集合

    NSMutableArray *array_service;

    //蓝牙管理者

    CBCentralManager *blueToothManage;

    //发现的设备

    CBPeripheral *myPeripheral;

}


- (void)viewDidLoad {

    [superviewDidLoad];

    array=[NSMutableArrayarrayWithCapacity:5];

    array_service=[NSMutableArrayarrayWithCapacity:5];

    // Do any additional setup after loading the view, typically from a nib.

    dispatch_queue_t myQueue=dispatch_queue_create("com.lanya",DISPATCH_QUEUE_SERIAL);

    blueToothManage=[[CBCentralManageralloc]initWithDelegate:selfqueue:myQueue];

  

    UIView *myview=[[UIViewalloc]initWithFrame:CGRectMake(0,30, 300,150)];

    myview.backgroundColor=[UIColorblueColor];

    UITapGestureRecognizer *ui=[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(dianji)];

    myview.userInteractionEnabled=YES;

    [myview addGestureRecognizer:ui];

    [self.viewaddSubview:myview];

}

-(void)dianji{

    myPeripheral.delegate=self;

    [blueToothManageconnectPeripheral:myPeripheraloptions:nil];

};

- (void)centralManagerDidUpdateState:(CBCentralManager *)central{

    switch (central.state) {

        case1:

            NSLog(@"不支持");

            break;

            case2:

             NSLog(@"未授权");

            break;

        case3:

            NSLog(@"该设备蓝牙未授权,请检查系统设置");

            break;

        case4:

            NSLog(@"该设备尚未打开蓝牙,请在设置中打开");

            break;

        case5:

            NSLog(@"蓝牙已经成功开启,请稍后再试");

              [blueToothManagescanForPeripheralsWithServices:niloptions:nil];

            break;

        default:

            break;

    }

};

//发现服务的回调

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(nullableNSError *)error{

  //array_service=  [peripheral services];

    

    NSLog(@"服务数量:%lu",[peripheralservices].count);

    

};

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{

NSLog(@"%@",@"已经建立连接");

    //停止扫描

    [blueToothManagestopScan];

    [myPeripheraldiscoverServices:nil];

};

- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI{

    NSLog(@"%@",@"发现设备");

     NSLog(@"name:%@",peripheral);

     NSLog(@"dicoveredPeripherals:%@", peripheral.name);

    if([peripheral.nameisEqualToString:@"BT05"]){

        myPeripheral=peripheral;

        NSLog(@"找到BT05");

    }

    

    if(![arraycontainsObject:peripheral]){

        

   

        [arrayaddObject:peripheral];

        

    

    NSLog(@"dicoveredPeripherals:%@", peripheral.name);

        NSLog(@"数据的长度:%lu",(unsignedlong)array.count);

    }

}


@end

0 0
原创粉丝点击