iOS 蓝牙使用小结 bluetooth

demo下载 http://download.csdn.NET/detail/swibyn/9717588
直接看代码 http://blog.csdn.Net/swibyn/article/details/53785249
首先推荐去看官方文档哦

现将建立蓝牙工程的要点总结一下,因为工程主要涉及中心模式,因此只总结中心模式的用法
1,引入CoreBluetooth.framework
2,实现蓝牙协议,如:
.h文件以下
@protocolCBCentralManagerDelegate;
@protocolCBPeripheralDelegate;

@interface ViewController :UIViewController <CBCentralManagerDelegate,CBPeripheralDelegate>

.m文件以下
#import "CoreBluetooth/CoreBluetooth.h"
另外还有代理部分请自行添加

3,下面是使蓝牙动起来的过程
3.1建立CBCentralManager实例
    self.cbCentralMgr = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
设置代理,好比:
    self.cbCentralMgr.delegate =self;

建立数组管理外设
    self.peripheralArray = [NSMutableArrayarray];

3.2扫描周围的蓝牙
实际上周围的蓝牙若是可被发现,则会一直往外发送广告消息,中心设备就是经过接收这些消息来发现周围的蓝牙的

    [self.cbCentralMgr scanForPeripheralsWithServices:nil options:nil];

3.3发现一个蓝牙设备
也就是收到了一个周围的蓝牙发来的广告信息,这是CBCentralManager会通知代理来处理
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
}
若是周围的蓝牙有多个,则这个方法会被调用屡次,你能够经过tableView或其余的控件把这些周围的蓝牙的信息打印出来
3.4链接一个蓝牙
[self.cbCentralMgr connectPeripheral:peripheral options:nil];
一个中心设备能够同时链接多个周围的蓝牙设备
当链接上某个蓝牙以后,CBCentralManager会通知代理处理

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
}

由于在后面咱们要从外设蓝牙那边再获取一些信息,并与之通信,这些过程会有一些事件可能要处理,因此要给这个外设设置代理,好比:
peripheral.delegate =self;
3.5查询蓝牙服务
[peripheral discoverServices:nil];
返回的蓝牙服务通知经过代理实现
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{

   for (CBService* service in peripheral.services){
        
    }
}
3.6查询服务所带的特征值
[peripheral discoverCharacteristics:nil forService:service];
返回的蓝牙特征值通知经过代理实现
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
    
   for (CBCharacteristic * characteristic in service.characteristics) {
    }
}
3.7给蓝牙发数据
[peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
这时还会触发一个代理事件
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
}
3.8处理蓝牙发过来的数据
一种方法是主动读取数据,不过更好的办法是设置事件通知。
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
这样当有数据时会自动触发代理事件
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
}

3.9 retrievePeripheralsWithIdentifiers使用例子
-(IBAction) Retrieve:(id)Sender
{
    [self.tvLogsetText:@""];
    NSMutableArray * Identifiers = [NSMutableArray array];
   for (CBPeripheral * peripheral in self.peripheralArray) {
        [Identifiers addObject: peripheral.identifier];
    }

    [self addLog:@"[self.cbCentralMgr retrievePeripheralsWithIdentifiers:self.PeripheralIdentifiers]"];
    self.retrievePeripherals = [self.cbCentralMgr retrievePeripheralsWithIdentifiers:Identifiers];
   for (CBPeripheral* peripheral in self.retrievePeripherals) {
        [self addLog:[NSString stringWithFormat:@"%@ name:%@",peripheral,peripheral.name]];
    }
    [self.tableViewPeripheral reloadData];
}

3.10 retrieveConnectedPeripheralsWithServices使用例子

-(IBAction) Retrieve:(id)Sender
{
    [self.tvLog setText:@""];
    NSMutableArray * services = [NSMutableArray array];
   for (CBPeripheral * peripheral in self.peripheralArray) {
       if (peripheral.isConnected) {
           for (CBService *service in peripheral.services) {
                [services addObject:service.UUID];
            }
        }
    }
    
    [self addLog:@"[self.cbCentralMgr retrieveConnectedPeripheralsWithServices:peripheral.services]"];
    self.retrievePeripherals = [self.cbCentralMgr retrieveConnectedPeripheralsWithServices:services];
   for (CBPeripheral* peripheral in self.retrievePeripherals) {
        [self addLog:[NSString stringWithFormat:@"%@ name:%@",peripheral,peripheral.name]];
    }
    [self.tableViewPeripheral reloadData];
}
相关文章
相关标签/搜索