1. 程式人生 > >iOS藍芽開發

iOS藍芽開發

原作者:===>https://blog.csdn.net/swibyn/article/details/20531593

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

}




大概就這個個流程,例子中的引數設定,及其其他的一些代理請自己研究。