zoukankan      html  css  js  c++  java
  • iOS蓝牙4.0开发例子

    1建立中心角色

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

    2扫描外设(discover)

    [manager scanForPeripheralsWithServices:nil options:options]; 

    3连接外设(connect)

    - (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)

    - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral  
    {  
          
        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]; 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);
    //监听特征 [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];
    

      

  • 相关阅读:
    【cocos2d-js官方文档】二、资源管理器Assets Manager
    【cocos2d-js官方文档】七、CCFileUtils
    【cocos2d-js官方文档】九、cc.loader
    【cocos2d-js官方文档】十二、对象缓冲池
    【cocos2d-js官方文档】十二、对象缓冲池
    【cocos2d-js官方文档】二十一、v3相对于v2版本的api变动
    【cocos2d-js教程】cocos2d-js 遮挡层(禁止触摸事件传递层)
    cocos2d-js中怎么删除一个精灵
    MS Chart 折线图——去除时间中的时、分、秒,按天统计【转】
    MS Chart 条状图【转】
  • 原文地址:https://www.cnblogs.com/visen-0/p/4013119.html
Copyright © 2011-2022 走看看