美文网首页
蓝牙coreblueTooth

蓝牙coreblueTooth

作者: 曾柏超 | 来源:发表于2019-08-08 11:53 被阅读0次
    
    @interface ViewController : UIViewController<CBCentralManagerDelegate,CBPeripheralDelegate>
    {
        CBCentralManager *theManager;
        CBPeripheral *thePerpher;
        CBCharacteristic *theSakeCC;
    }
    
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        theManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil];
        self.connectBtn.enabled = NO;
    }
    
    //当前蓝牙主设备状态
    -(void)centralManagerDidUpdateState:(CBCentralManager *)central{
        if (central.state==CBCentralManagerStatePoweredOn) {
            self.title = @"蓝牙已就绪";
            self.connectBtn.enabled = YES;
        }else
        {
            self.title = @"蓝牙未准备好";
            [self.activeID stopAnimating];
            switch (central.state) {
                case CBCentralManagerStateUnknown:
                    NSLog(@">>>CBCentralManagerStateUnknown");
                    break;
                case CBCentralManagerStateResetting:
                    NSLog(@">>>CBCentralManagerStateResetting");
                    break;
                case CBCentralManagerStateUnsupported:
                    NSLog(@">>>CBCentralManagerStateUnsupported");
                    break;
                case CBCentralManagerStateUnauthorized:
                    NSLog(@">>>CBCentralManagerStateUnauthorized");
                    break;
                case CBCentralManagerStatePoweredOff:
                    NSLog(@">>>CBCentralManagerStatePoweredOff");
                    break;
                default:
                    break;
            }
        }
    }
    
    //开始连接action
    - (IBAction)startConnectAction:(id)sender {
        
        if (theManager.state==CBCentralManagerStatePoweredOn) {
            NSLog(@"主设备蓝牙状态正常,开始扫描外设...");
            self.title = @"扫描小米手环...";
            //nil表示扫描所有设备
            [theManager scanForPeripheralsWithServices:nil options:nil];
            [self.activeID startAnimating];
            self.connectBtn.enabled = NO;
            self.resultTextV.text = @"";
        }
    }
    
    #pragma mark 设备扫描与连接的代理
    
    //扫描到设备会进入方法
    -(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
        NSLog(@"扫描连接外设:%@ %@",peripheral.name,RSSI);
        [central connectPeripheral:peripheral options:nil];
        if ([peripheral.name hasPrefix:@"RBP"]) {
            thePerpher = peripheral;
            [central stopScan];
            [central connectPeripheral:peripheral options:nil];
            self.title = @"发现小米手环,开始连接...";
            self.resultTextV.text = [NSString stringWithFormat:@"发现手环:%@\n名称:%@\n",peripheral.identifier.UUIDString,peripheral.name];
        }
    }
    
    //连接到Peripherals-成功
    - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
    {
        self.title = @"连接成功,扫描信息...";
        NSLog(@"连接外设成功!%@",peripheral.name);
        [peripheral setDelegate:self];
        [peripheral discoverServices:nil];
        NSLog(@"开始扫描外设服务 %@...",peripheral.name);
    }
    
    
    //扫描到特征
    -(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
        if (error)
        {
            NSLog(@"扫描外设的特征失败!%@->%@-> %@",peripheral.name,service.UUID, [error localizedDescription]);
            self.title = @"find characteristics error.";
            [self.activeID stopAnimating];
            self.connectBtn.enabled = YES;
            return;
        }
        
        NSLog(@"扫描到外设服务特征有:%@->%@->%@",peripheral.name,service.UUID,service.characteristics);
        //获取Characteristic的值
        for (CBCharacteristic *c in service.characteristics){
           NSString* charUUID = [NSString stringWithFormat:@"%@", c.UUID];
            if ([thePerpher.name hasPrefix:@"RBP"]) {
                if ([charUUID isEqualToString:@"FFF1"]) { //通知通道 0x30
                    self.nofityDataC = c;
                    [peripheral setNotifyValue:YES forCharacteristic:self.nofityDataC];
                }
                if ([charUUID isEqualToString:@"FFF2"]) { //写通道 0xC
                    self.writeDataC = c;//ok
                }
                if (self.writeDataC && self.nofityDataC) {
                    NSData* data = [HiseeClassFormat intArrayToData:BT_RBP9804_ORDER_READY length:8];
                    [self writeDataToBt:data];
                }
            }
        }
    }
    
    - (void)writeDataToBt:(NSData*)data
    {
        if (thePerpher && self.writeDataC) {
            [thePerpher writeValue:data forCharacteristic:self.writeDataC type:CBCharacteristicWriteWithResponse];
            NSLog(@"向蓝牙写数据 = %@", data);
        }
    }
    
    - (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
    {
        if (error) {
            NSLog(@"写入错误 = %@, 写入特征uuid = %@", error, characteristic.UUID);
        } else {
            NSLog(@"特征值 = %@, 写入成功", characteristic.UUID);
        }
    }
    
    #pragma mark 设备信息处理
    //扫描到具体的值
    - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error
    {
        if (error) {
            NSLog(@"扫描外设的特征失败!%@-> %@",peripheral.name, [error localizedDescription]);
            self.title = @"find value error.";
            return;
        }
        NSUInteger dataLen = 0;
        NSData *data = characteristic.value;
        int* dataArray = [HiseeClassFormat dataToIntArray:data length:&dataLen];
        if (dataArray == NULL) {
            return;
        }
        NSString* dataStr = @"";
        for (int i = 0; i < data.length; i++) {
            NSString* num = [NSString stringWithFormat:@"%d,", dataArray[i]];
            dataStr = [dataStr stringByAppendingString:num];
        }
        NSLog(@"接收到蓝牙的数据 = %@", dataStr);
        if ([thePerpher.name hasPrefix:@"RBP"]) { //RBP9804
            HiseeBtDeviceBP88A* rbp9804 = [[HiseeBtDeviceBP88A alloc] initWithData:data];
            rbp9804.deviceName = thePerpher.name;
            if (dataLen > 7 && dataArray[3] == 3 && dataArray[7] == 1) { //首次链接成功
                NSData* sendData = [HiseeClassFormat intArrayToData:BT_RBP9804_ORDER_BEGINMEASURE length:8];
                [self writeDataToBt:sendData];
            }
        }
        [self.activeID stopAnimating];
        self.connectBtn.enabled = YES;
        self.title = @"信息扫描完成";
    }
    
    

    相关文章

      网友评论

          本文标题:蓝牙coreblueTooth

          本文链接:https://www.haomeiwen.com/subject/jbatjctx.html