iOS開發 - Core Bluetooth藍芽開發

來源:互聯網
上載者:User

iOS開發 - Core Bluetooth藍芽開發
Core Bluetooth

Core Bluetooth測試比較麻煩,正常情況下,得至少有2台真實的藍芽4.0裝置

如何讓iOS模擬器也能測試藍芽4.0程式?
買一個CSR藍芽4.0 USB適配器,插在Mac上
在終端輸入sudo nvram bluetoothHostControllerSwitchBehavior=”never”
重啟Mac
用Xcode 4.6調試代碼,將程式跑在iOS 6.1的模擬器上
(蘋果把iOS 7.0模擬器對BLE的支援移除掉了)

Core Bluetooth的使用情境
運動手環、智能家居、嵌入式裝置等等(金融刷卡器、心電測量器)

Core Bluetooth的核心結構圖

Core Bluetooth的基本常識

每個藍芽4.0裝置都是通過服務(Service)和特徵(Characteristic)來展示自己的
一個裝置必然包含一個或多個服務,每個服務下面又包含若干個特徵

特徵是與外界互動的最小單位
比如說,一台藍芽4.0裝置,用特徵A來描述自己的出廠資訊,用特徵B來收發資料

服務和特徵都是用UUID來唯一標識的,通過UUID就能區別不同的服務和特徵

裝置裡面各個服務(service)和特徵(characteristic)的功能,均由藍牙裝置硬體廠商提供,比如哪些是用來互動(讀寫),哪些可擷取模組資訊(唯讀)等

Core Bluetooth的開發步驟

建立中心裝置
掃描外設(Discover Peripheral)
串連外設(Connect Peripheral)
掃描外設中的服務和特徵(Discover Services And Characteristics)
利用特徵與外設做資料互動(Explore And Interact)
中斷連線(Disconnect)

藍芽的現狀

絕大多數智能手機支援藍芽 4.0(BLE)

藍芽晶片發展迅速,在效能和效率方面都有很大提高,且不斷變得更小更便宜

iBeacon + 藍芽,前景一片光明
應用之一:室內導航
Estimote公司為iBeacon提供基站
3個iBeacon基站的預購價格為99美元(約合人民幣610元)
Estimote公司推出的iBeacon基站的最遠傳輸距離為50m,但是他們推薦在10m範圍內的使用效果最好

一塊紐扣電池就能為一個iBeacon基站提供長達 2 年的使用壽命,而且是在裝置不斷對外發射訊號的情況下

Core Bluetooth開發執行個體
#import ViewController.h#import @interface ViewController ()/** *  外設 */@property (nonatomic, strong) NSMutableArray *peripherals;/** *  中心管理者 */@property (nonatomic, strong) CBCentralManager *mgr;@end@implementation ViewController- (NSMutableArray *)peripherals{    if (!_peripherals) {        _peripherals = [NSMutableArray array];    }    return _peripherals;}- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    // 1.建立中心裝置    CBCentralManager *mgr = [[CBCentralManager alloc] init];    self.mgr = mgr;    // 設定代理    mgr.delegate = self;    // 2.利用中心裝置掃描外部裝置    /*     如果指定數組代表只掃描指定的裝置     */    [mgr scanForPeripheralsWithServices:nil options:nil];}#pragma mark - CBCentralManagerDelegate- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{    // 儲存掃描到得外部裝置    // 判斷如果數組中不包含當前掃描到得外部設定才儲存    if (![self.peripherals containsObject:peripheral]) {        peripheral.delegate = self;        [self.peripherals addObject:peripheral];    }}/** *  類比點擊, 然後串連所有的外設 */- (void)start{    for (CBPeripheral *peripheral in self.peripherals) {        /**         *  串連外設         */        [self.mgr connectPeripheral:peripheral options:nil];    }}/** *  串連外設成功調用 */- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{    // 掃描外設中得服務    [peripheral discoverServices:nil];}/** *  串連外設失敗調用 */- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{}#pragma makr - CBPeripheralDelegate/** *  只要掃描到服務就會調用 * *  @param peripheral 服務所在的外設 */- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{    // 擷取外設中所有掃描到得服務    NSArray *services = peripheral.services;    for (CBService *service in services) {        // 拿到需要的服務        if ([service.UUID.UUIDString isEqualToString:@123])        {            // 從需要的服務中尋找需要的特徵            // 從peripheral中得service中掃描特徵            [peripheral discoverCharacteristics:nil forService:service];        }    }}/** *  只要掃描到特徵就會調用 * *  @param peripheral 特徵所屬的外設 *  @param service    特徵所屬的服務 */- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{    // 拿到服務中所有的特診    NSArray *characteristics =  service.characteristics;    // 遍曆特徵, 拿到需要的特徵處理    for (CBCharacteristic * characteristic in characteristics) {        if ([characteristic.UUID.UUIDString isEqualToString:@8888]) {            NSLog(@設定鬧鐘);        }    }}@end

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.