iOS藍芽4.0開發例子

來源:互聯網
上載者:User

標籤:des   blog   io   os   ar   for   strong   資料   sp   

1建立中心角色

#import <CoreBluetooth/CoreBluetooth.h>  CBCentralManager *manager;  manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];  

2掃描外設(discover)

[manager scanForPeripheralsWithServices:nil options:options]; 

3串連外設(connect)

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI  {        if([peripheral.name  isEqualToString:BLE_SERVICE_NAME]){                [self connect:peripheral];        }s);  }        -(BOOL)connect:(CBPeripheral *)peripheral{        self.manager.delegate = self;        [self.manager connectPeripheral:peripheral                                options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]];}

  

4掃描外設中的服務和特徵(discover)

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral  {            NSLog(@"Did connect to peripheral: %@", peripheral);      _testPeripheral = peripheral;            [peripheral setDelegate:self];  
//尋找服務 [peripheral discoverServices:nil]; }
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error  {              NSLog(@"didDiscoverServices");            if (error)      {          NSLog(@"Discovered services for %@ with error: %@", peripheral.name, [error localizedDescription]);                    if ([self.delegate respondsToSelector:@selector(DidNotifyFailConnectService:withPeripheral:error:)])              [self.delegate DidNotifyFailConnectService:nil withPeripheral:nil error:nil];                    return;      }              for (CBService *service in peripheral.services)      {           //探索服務        if ([service.UUID isEqual:[CBUUID UUIDWithString:UUIDSTR_ISSC_PROPRIETARY_SERVICE]])          {              NSLog(@"Service found with UUID: %@", service.UUID);  
//尋找特徵 [peripheral discoverCharacteristics:nil forService:service]; break; } } }
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{        if (error)    {        NSLog(@"Discovered characteristics for %@ with error: %@", service.UUID, [error localizedDescription]);                [self error];        return;    }        NSLog(@"服務:%@",service.UUID);    for (CBCharacteristic *characteristic in service.characteristics)    {       //發現特徵            if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"xxxxxxx"]]) {                NSLog(@"監聽:%@",characteristic);
//監聽特徵 [self.peripheral setNotifyValue:YES forCharacteristic:characteristic]; } }}

5與外設做資料互動(讀 與 寫)  

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{    if (error)    {        NSLog(@"Error updating value for characteristic %@ error: %@", characteristic.UUID, [error localizedDescription]);        self.error_b = BluetoothError_System;        [self error];        return;    }    //    NSLog(@"收到的資料:%@",characteristic.value);    [self decodeData:characteristic.value];}

NSData *d2 = [[PBABluetoothDecode sharedManager] HexStringToNSData:@"0x02"];                [self.peripheral writeValue:d2 forCharacteristic:characteristic type:CBCharacteristicWriteWithoutResponse];

  

iOS藍芽4.0開發例子

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.