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];
}
相關文章
相關標籤/搜索