iOS開發筆記--Core Bluetooth開發

推薦閱讀文章:http://blog.csdn.net/pony_maggie/article/details/26740237框架

1、前言性能

CoreBluetooth框架的核心實際上是兩個東西,peripheral和central, 能夠理解成外設和中心。對應他們分別有一組相關的API和類,以下圖所示:spa

2、Core Bluetooth 的基本常識:
一、 每一個藍牙設備都是經過服務和特徵來展現本身
一個設備必然包含一個或多給服務,每一個服務下面又包含多個特徵
二、特徵是與外界交互的最小單位
好比說,一臺藍牙設備,用特徵A來描述本身的出場信息,用特徵B來描述本身的收發數據
三、服務和特徵都是用UUID來惟一標識的,經過UUID就能區別不一樣的服務和特徵
四、設備裏面的服務和特徵的功能,都是由藍牙設備硬件廠商提供,好比哪些用來交互(讀寫),哪些用來可獲取模塊信息(只讀)等。.net

 

3、Core Bluetooth的開發步驟:代理

一、創建中心設備
二、掃描外設(Discover Peripheral)
三、鏈接外設(connect Peripheral)
四、掃描外設中的服務於特徵(Discover Services and Characteristics)
五、利用特徵與外設作數據交互(Explore and interact)
六、斷開鏈接(Disconnect)blog

創建中心設備:ip

//1、建立中心設備開發

    CBCentralManager *mgr = [[CBCentralManager alloc] init];get

    //設置代理it

    mgr.delegate = self;

    //2、利用中心設備掃描外部設備

    /*

     若是指定數據NSArray 只能掃描指定的設備

     */

    [mgr scanForPeripheralsWithServices:nil options:nil];

    self.mgr = mgr;

#pragma mark  - CBCentralManagerDelegate

//實現代理,掃描到外設,保存外設

- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error

{

   //保留掃描到的外設

    if (![self.peripherals containsObject:peripheral])

    {

        [self.peripherals addObject:peripheral];

    }

}

/**

 *  模擬點擊鏈接全部的外設 鏈接全部外設

 */

- (void)start

{

    for (CBPeripheral *peripheral in self.peripherals) {

        peripheral.delegate = self;

        [self.mgr connectPeripheral:peripheral options:nil];

    }

}

 

/**

 *  鏈接外設成功

 *

 *  @param central

 *  @param peripheral

 */

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

{

    //1、掃描外設中的服務(掃描全部服務)

    [peripheral discoverServices:nil];

    

}

 

/**

 *  鏈接外設失敗

 *

 *  @param central

 *  @param peripheral

 *  @param error

 */

- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error

{

}

 

#pragma mark - CBPeripheralDelegate

/**

 *  只要掃描到服務就會調用

 *

 *  @param peripheral peripheral

 *  @param error      error

 */

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error

{

    //獲取外設中全部掃描的獲得的服務

    NSArray *services = peripheral.services;

    for (CBService *service in services)

    {

        //能夠過濾不須要的服務

        if ([service.UUID.UUIDString isEqualToString:@"989589595995"]) return;

        

        //從須要的服務中查找須要的特徵

        //peripheral中的service中掃描特徵

        [peripheral discoverCharacteristics:nil forService:service];

    }

    

}

 

/**

 *  只要掃描到特徵就會調用

 *

 *  @param peripheral 外設

 *  @param service    服務

 *  @param error      錯誤信息

 */

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error

{

    NSArray *characteristics = service.characteristics;

    //遍歷特徵,拿到須要的特徵處理

    

    for (CBCharacteristic *characteristic in characteristics)

    {

        if ([characteristic.UUID.UUIDString isEqualToString:@"9996996969"])

        {

            

        }

    }

}

 

4、藍牙的現狀

一、絕大多數只能手機支持藍牙4.0(BLE)

二、藍牙芯片發展迅速,在性能和效率方面都是有很大的提升,且不斷變得更小更便宜

三、iBeacon + 藍牙(BLE),前途一片光明

     應用之一:室內導航

    Estimote公司爲iBeacon提供基站

    3個Estimote公司爲iBeacon預購價格爲99美圓

    Estimote公司推出的iBeacon基站的最遠出書距離爲50m,最佳距離是10m之內

    一塊鈕釦電池就能爲一個iBeacon基站提供長達2年的使用壽命,並且是在設備不斷對外發射信號的狀況下;

相關文章
相關標籤/搜索