• iOS Bluetooth 蓝牙 学习


       一个蓝牙 第三方库 (github上面)  https://github.com/coolnameismy/BabyBluetooth    (名字:BabyBluetooth)

    ----------蓝牙大神博客(http://liuyanwei.jumppo.com/index.html)---------------

    1.http://liuyanwei.jumppo.com/2015/07/17/ios-BLE-1.html  (蓝牙开发相关基础知识)

    2.http://liuyanwei.jumppo.com/2015/08/14/ios-BLE-2.html  (蓝牙中心模式的iOS代码)

    3.http://liuyanwei.jumppo.com/2015/09/07/ios-BLE-3.html  (app作为外设被链接的实现)

    4.http://liuyanwei.jumppo.com/2015/09/11/ios-BLE-4.html   (BabyBluetooth蓝牙库介绍

    ----原生蓝牙调用 

    iOS蓝牙4.0开发例子

    1建立中心角色

    1
    2
    3
    #import <CoreBluetooth/CoreBluetooth.h> 
    CBCentralManager *manager; 
    manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; 

    2扫描外设(discover)

    [manager scanForPeripheralsWithServices:nil options:options]; 

    3连接外设(connect)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI 
    {
            if([peripheral.name  isEqualToString:BLE_SERVICE_NAME]){
                    [self connect:peripheral];
            }
    s); 
    }       
     
    -(BOOL)connect:(CBPeripheral *)peripheral{
            self.manager.delegate = self;
            [self.manager connectPeripheral:peripheral
                                    options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]];
    }

      

    4扫描外设中的服务和特征(discover)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral 
           
        NSLog(@"Did connect to peripheral: %@", peripheral); 
        _testPeripheral = peripheral; 
           
        [peripheral setDelegate:self];  <br>//查找服务
        [peripheral discoverServices:nil]; 
           
           
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error 
       
           
        NSLog(@"didDiscoverServices"); 
           
        if (error) 
        
            NSLog(@"Discovered services for %@ with error: %@", peripheral.name, [error localizedDescription]); 
               
            if ([self.delegate respondsToSelector:@selector(DidNotifyFailConnectService:withPeripheral:error:)]) 
                [self.delegate DidNotifyFailConnectService:nil withPeripheral:nil error:nil]; 
               
            return
        
           
       
        for (CBService *service in peripheral.services) 
        
             //发现服务
            if ([service.UUID isEqual:[CBUUID UUIDWithString:UUIDSTR_ISSC_PROPRIETARY_SERVICE]]) 
            
                NSLog(@"Service found with UUID: %@", service.UUID);  <br>//查找特征
                [peripheral discoverCharacteristics:nil forService:service]; 
                break
            
               
               
        
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
    {
         
        if (error)
        {
            NSLog(@"Discovered characteristics for %@ with error: %@", service.UUID, [error localizedDescription]);
             
            [self error];
            return;
        }
         
        NSLog(@"服务:%@",service.UUID);
        for (CBCharacteristic *characteristic in service.characteristics)
        {
           //发现特征
                if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"xxxxxxx"]]) {
                    NSLog(@"监听:%@",characteristic);<br>//监听特征
                    [self.peripheral setNotifyValue:YES forCharacteristic:characteristic];
                }
             
        }
    }

    5与外设做数据交互(读 与 写)  

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
    {
        if (error)
        {
            NSLog(@"Error updating value for characteristic %@ error: %@", characteristic.UUID, [error localizedDescription]);
            self.error_b = BluetoothError_System;
            [self error];
            return;
        }
         
    //    NSLog(@"收到的数据:%@",characteristic.value);
        [self decodeData:characteristic.value];
    }

    1
    2
    NSData *d2 = [[PBABluetoothDecode sharedManager] HexStringToNSData:@"0x02"];
                    [self.peripheral writeValue:d2 forCharacteristic:characteristic type:CBCharacteristicWriteWithoutResponse];
  • 相关阅读:
    base64URL处理
    智慧幼儿园方案:AI技术如何助力幼儿园智慧建设?
    TSINGSEE青犀视频开发webrtc项目如何实现Windows视频采集?
    国内采用HTTP协议传输视频都有什么优势?
    如何根据直播场景选择合适的直播协议?
    EasyPlayer-RTSP WebActiveX注册提示failed to create control 未指定错误
    luogu题解P2312解方程--暴力模+秦九韶
    【补充习题四】凑微分技巧与积分因子法解常微分方程
    【GMOJ7177】鱼跃龙门
    【洛谷P1791】人员雇佣
  • 原文地址:https://www.cnblogs.com/leilei123/p/4961816.html
Copyright © 2020-2023  润新知