iOS蓝牙4.0开发例子

转载自http://www.cnblogs.com/visen-0/p/4013119.html

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值