美文网首页
iOS蓝牙4.0开发

iOS蓝牙4.0开发

作者: xh_0129 | 来源:发表于2016-10-11 06:22 被阅读0次

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;        }                          } }12345678910111213141516171819202122- (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];}

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

相关文章

  • iOS开发蓝牙4.0初识

    iOS开发蓝牙4.0初识转载 2015-09-20 15:26:44标签:ios开发蓝牙ios开发蓝牙4.0ios...

  • iOS 蓝牙4.0开发

    iOS 蓝牙4.0开发

  • iOS蓝牙4.0,收发数据设计

    iOS蓝牙开发,现在常规使用的是CoreBlueTooth.framework,即蓝牙4.0开发框架。 1.CBC...

  • iOS蓝牙4.0,后台监听来电

    在上一篇文章《iOS蓝牙4.0收发数据设计》,有简单的介绍收发数据设计。在iOS蓝牙4.0开发过程中,肯定还有许多...

  • iOS学习-蓝牙

    一、Core Bluetooth iOS中使用Core Bluetooth这个框架来进行蓝牙的开发支持蓝牙4.0,...

  • iOS蓝牙4.0基础开发

    1.蓝牙开发基础 蓝牙4.0是低电量模式所以也叫4.0BLE。本文将使用iOS系统提供的CoreBluetooth...

  • iOS 蓝牙BLE4.0开发

    蓝牙开发,现在普遍的都是BLE4.0低功耗蓝牙,CoreBluetooth是iOS 开发I比较推荐的一种开发方法...

  • iOS BlueTooth HID(Human Interfac

    1.HID做什么用的? 在iOS蓝牙开发当中,现在的主流是使用蓝牙4.0,使用CoreBluetooth来进行开发...

  • iOS 蓝牙4.0开发

    iOS 蓝牙4.0开发 背景: 1.iOS的蓝牙不能用来传输文件。 2.iOS与iOS设备之间进行数据通信,使用g...

  • iOS 蓝牙4.0开发

    iOS 蓝牙4.0开发 背景: 1.iOS的蓝牙不能用来传输文件。2.iOS与iOS设备之间进行数据通信,使用ga...

网友评论

      本文标题:iOS蓝牙4.0开发

      本文链接:https://www.haomeiwen.com/subject/jowvyttx.html