iOS基于蓝牙4.0开发代码示例_嘉兴APP开发_嘉兴APP设计制作开发_嘉兴APP制作_嘉兴手机APP制作开发_嘉兴APP软件开发_嘉兴最专业的APP开发公司-嘉兴雷鸟软件科技有限公司 
雷鸟科技,提供 嘉兴app制作嘉兴app开发嘉兴iOS开发嘉兴Android开发嘉兴app设计嘉兴手机app制作服务.  服务热线:13396739763 / 13136206268
您的位置:首页 > APP资讯 >

iOS基于蓝牙4.0开发代码示例

本文小编整理了关于手机APP蓝牙开发中各个阶段的代码示例:

1建立中心角色

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


2扫描外设

[manager scanForPeripheralsWithServices:nil options:options];  


3连接外设


- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI  
{  
  
    if(![_dicoveredPeripherals containsObject:peripheral])  
        [_dicoveredPeripherals addObject:peripheral];  
      
    NSLog(@"dicoveredPeripherals:%@", _dicoveredPeripherals);  
}   


//连接指定的设备  
-(BOOL)connect:(CBPeripheral *)peripheral  
{  
    NSLog(@"connect start");  
    _testPeripheral = nil;  
      
    [manager connectPeripheral:peripheral  
                       options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]];  
      
    //开一个定时器监控连接超时的情况  
    connectTimer = [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(connectTimeout:) userInfo:peripheral repeats:NO];  
  
    return (YES);  
}


4扫描外设中的服务和特征
连接成功后,系统会通过回调函数告诉我们,然后我们就在这个回调里去扫描设备下所有的服务和特征,代码如下
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral  
{  
    [connectTimer invalidate];//停止时钟  
      
    NSLog(@"Did connect to peripheral: %@", peripheral);  
    _testPeripheral = peripheral;  
      
    [peripheral setDelegate:self];  
    [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);  
            [peripheral discoverCharacteristics:nil forService:service];  
            isVPOS3356 = YES;  
            break;  
        }  
          
          
    }  
}


- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error   
{  
      
    if (error)   
    {  
        NSLog(@"Discovered characteristics for %@ with error: %@", service.UUID, [error localizedDescription]);  
          
        if ([self.delegate respondsToSelector:@selector(DidNotifyFailConnectChar:withPeripheral:error:)])  
            [self.delegate DidNotifyFailConnectChar:nil withPeripheral:nil error:nil];  
          
        return;  
    }  
      
      
    for (CBCharacteristic *characteristic in service.characteristics)  
    {  
        if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:UUIDSTR_ISSC_TRANS_TX]])  
        {  
            NSLog(@"Discovered read characteristics:%@ for service: %@", characteristic.UUID, service.UUID);  
              
            _readCharacteristic = characteristic;//保存读的特征  
              
            if ([self.delegate respondsToSelector:@selector(DidFoundReadChar:)])  
                [self.delegate DidFoundReadChar:characteristic];  
              
            break;  
        }  
    }  
  
      
    for (CBCharacteristic * characteristic in service.characteristics)  
    {  
          
          
        if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:UUIDSTR_ISSC_TRANS_RX]])  
        {  
  
            NSLog(@"Discovered write characteristics:%@ for service: %@", characteristic.UUID, service.UUID);  
            _writeCharacteristic = characteristic;//保存写的特征  
              
            if ([self.delegate respondsToSelector:@selector(DidFoundWriteChar:)])  
                [self.delegate DidFoundWriteChar:characteristic];  
              
            break;  
              
              
        }  
    }  
      
    if ([self.delegate respondsToSelector:@selector(DidFoundCharacteristic:withPeripheral:error:)])  
        [self.delegate DidFoundCharacteristic:nil withPeripheral:nil error:nil];  
      
}  


5与外设做数据交互
//写数据  
-(void)writeChar:(NSData *)data  
{  
    [_testPeripheral writeValue:data forCharacteristic:_writeCharacteristic type:CBCharacteristicWriteWithResponse];  
}  


6 断开连接
//主动断开设备  
-(void)disConnect  
{  
      
    if (_testPeripheral != nil)  
    {  
        NSLog(@"disConnect start");  
        [manager cancelPeripheralConnection:_testPeripheral];  
    }  
  
}  

 

 嘉兴雷鸟软件科技有限公司
 13396739763 13136206268 (节假日均可拨打) 0573-83698396 (周一至周六可拨打)


售前咨询QQ:点击这里给我发消息    点击这里给我发消息


技术支持QQ:点击这里给我发消息

所在地址: 浙江省嘉兴市城南路1539号创业大厦


网址:app.lei-niao.com

嘉兴APP公司

嘉兴APP公司

服务内容



浙公网安备 33040202000168号