IOS Bluetooth Bluetooth learning

Source: Internet
Author: User

1 establishing a central role

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

2 Scanning Peripherals (Discover)

3 Connecting peripherals (Connect)

12345678910111213 - (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:[NSDictionarydictionaryWithObject:[NSNumbernumberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]];}

  

4 Scanning for services and features in peripherals (Discover)

1234567891011 - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral           NSLog(@"Did connect to peripheral: %@", peripheral);     _testPeripheral = peripheral;           [peripheral setDelegate:self];  <br>//查找服务    [peripheral discoverServices:nil];             }
123456789101112131415161718192021222324252627282930 - (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:nilwithPeripheral:nilerror: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);  <br>//查找特征            [peripheral discoverCharacteristics:nilforService: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]);                [selferror];        return;    }        NSLog(@"服务:%@",service.UUID);    for(CBCharacteristic *characteristic in service.characteristics)    {       //发现特征            if([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"xxxxxxx"]]) {                NSLog(@"监听:%@",characteristic);<br>//监听特征                [self.peripheral setNotifyValue:YESforCharacteristic:characteristic];            }            }}

5 Data interaction with peripherals (read and write)  

Read

12345678910111213 - (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;        [selferror];        return;    }    //    NSLog(@"收到的数据:%@",characteristic.value);    [selfdecodeData:characteristic.value];}

Write

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

IOS Bluetooth Bluetooth learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.