国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

iOS藍牙4.0開發例子

2019-11-14 20:00:10
字體:
來源:轉載
供稿:網友

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

  


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 灵川县| 舟山市| 加查县| 宣恩县| 子洲县| 固阳县| 新干县| 大庆市| 师宗县| 水城县| 山东省| 页游| 大英县| 邹平县| 大厂| 北川| 惠来县| 迁西县| 慈溪市| 兴仁县| 左贡县| 阜康市| 桐乡市| 肃宁县| 买车| 桦甸市| 嘉义县| 焉耆| 泽库县| 同心县| 济源市| 福建省| 东乡| 浦北县| 涿鹿县| 杭锦后旗| 深圳市| 汝城县| 安西县| 库尔勒市| 陵川县|