iOS 蓝牙4.0开发

作者: starfox寒流 | 来源:发表于2015-04-14 11:31 被阅读8646次

    iOS 蓝牙4.0开发

    背景:

    1.iOS的蓝牙不能用来传输文件。
    2.iOS与iOS设备之间进行数据通信,使用gameKit.framework
    3.iOS与其他非iOS设备进行数据通信,使用coreBluetooth.framework

    iOS中蓝牙的实现方案

    iOS中提供了4个框架用于实现蓝牙连接
    GameKit.framework(用法简单)
    只能用于iOS设备之间的连接,多用于游戏(比如五子棋对战),从iOS7开始过期

    MultipeerConnectivity.framework
    只能用于iOS设备之间的连接,从iOS7开始引入,主要用于文件共享(仅限于沙盒的文件)

    ExternalAccessory.framework
    可用于第三方蓝牙设备交互,但是蓝牙设备必须经过苹果MFi认证(国内较少)

    CoreBluetooth.framework(时下热门)
    可用于第三方蓝牙设备交互,必须要支持蓝牙4.0
    硬件至少是4s,系统至少是iOS6
    蓝牙4.0以低功耗著称,一般也叫BLE(BluetoothLowEnergy)
    目前应用比较多的案例:运动手坏、嵌入式设备、智能家居

    下面具体介绍使用CoreBluetooth.framework的代码步骤:

    
    //蓝牙系统库
    
    #import <CoreBluetooth/CoreBluetooth.h>
    
    //必须要由UUID来唯一标示对应的service和characteristic
    
    #define kServiceUUID @"5C476471-1109-4EBE-A826-45B4F9D74FB9"
    
    #define kCharacteristicHeartRateUUID @"82C7AC0F-6113-4EC9-92D1-5EEF44571398"
    
    #define kCharacteristicBodyLocationUUID @"537B5FD6-1889-4041-9C35-F6949D1CA034"
    
    
    
    @interface ViewController ()<CBCentralManagerDelegate,CBPeripheralDelegate>
    
    
    
    @property (nonatomic,strong)CBCentralManager * centralManager;
    
    @property (nonatomic,strong)CBPeripheral     * peripheral;
    
    @end
    
    

    1.创建中心角色

    #import <CoreBluetooth/CoreBluetooth.h> 
    - (void)viewDidLoad
    
    {
    
        [super viewDidLoad];
    
        //初始化蓝牙 central manager
    
        _centralManager = [[CBCentralManager alloc]initWithDelegate:self queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) options:nil];    
    
    }
    
    
    
    

    2.扫描外设

    [manager scanForPeripheralsWithServices:nil options:@{CBCentralManagerRestoredStateScanOptionsKey:@(YES)}]; 
    

    3.连接外设

    - (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.扫描外设中的服务和特征

    - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral 
    { 
           
        NSLog(@"Did connect to peripheral: %@", peripheral); 
        _testPeripheral = peripheral; 
           
        [peripheral setDelegate:self];  <br>//查找服务
        [peripheral discoverServices:nil]; 
           
           
    }
    

    发现服务:

    - (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; 
            } 
               
               
        } 
    }
    

    发现服务中的特征:

    - (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.与外设进行数据交互
    读取数据:

    - (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];
    }
    

    写数据:

    NSData *d2 = [[PBABluetoothDecode sharedManager] HexStringToNSData:@"0x02"];
                    [self.peripheral writeValue:d2 forCharacteristic:characteristic type:CBCharacteristicWriteWithoutResponse];
    
    

    相关文章

      网友评论

      • manbuzhe:能给一个demo吗 990195279@qq.com
      • 南方小金豆:附近有多个设备,怎么连接离自己最近的一个设备
      • 我是小文:学习了 谢谢
      • 7696d2968875:很不错,准备写蓝牙呢,能发一个demo参考一下吗?非常感谢。yangfanfu1210@163.com
      • 迷恋代码:可以给我发一份Demo吗,拜托了
        492262370@qq.com
      • mark666:谢谢你回答了我的问题,我想再问一下,如果我想看某个框架的参考,在哪里找到?
        starfox寒流:@mark666 当然是官方的开发者文档啦~~ :smiley:
      • mark666:CoreBluetooth.framework
        用这个框架是不是,外设必须是4.0?低于4.0就不能连接?
        starfox寒流:@mark666 是的。https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/AboutCoreBluetooth/Introduction.html#//apple_ref/doc/uid/TP40013257
      • 7f56a349199c:DecodeData这是什么?
      • 小K哥哥:最后一行代码:PBABluetoothDecode 什么东西?
      • 煜寒了:@ThinkerDown 楼主,搜索到得外设中uuid是唯一的吗? 假如我这次连接之后,下次怎么做到自动连接这个设备,刷新一下鉴权码就可以直接操作呢
      • 煜寒了:楼主 获取mac地址是私有api,那开发的时候就不能用mac地址只能使用uuid了是吧,而且uuid现在用也是被废弃了,使用identifier也是一样的吧
      • starfox寒流:@lufir 应该是由蓝牙设备提供
      • HYY:多谢分享,想问下怎样获取UUID的service和characteristic

      本文标题:iOS 蓝牙4.0开发

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