IOS Bluetooth 4.0 development example: ios Bluetooth 4.0

Source: Internet
Author: User

IOS Bluetooth 4.0 development example: ios Bluetooth 4.0

1. Create a central role

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

2 scan peripherals (discover)

[manager scanForPeripheralsWithServices:nil options:options]; 

3 connect to peripherals)

- (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. Scan for services and features (discover) in peripherals)

-(Void) centralManager :( CBCentralManager *) central principal :( CBPeripheral *) peripheral {NSLog (@ "Did connect to peripheral: % @", peripheral); _ testPeripheral = peripheral; [peripheral setDelegate: self];
// Find the service [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 (didpolicyfailconnectservice: withPeripheral: error :)]) [self. delegate DidNotifyFailConnectService: nil withPeripheral: nil error: nil]; return ;}for (CBService * service in peripheral. services) {// discover the service if ([service. UUID isEqual: [CBUUID UUIDWithString: UUIDSTR_ISSC_PROPRIETARY_SERVICE]) {NSLog (@ "Service found with UUID: % @", service. UUID );
// Search for features [peripheral discoverCharacteristics: nil forService: service]; break ;}}}
-(Void) peripheral :( CBPeripheral *) peripheral failed :( CBService *) service error :( NSError *) error {if (error) {NSLog (@ "Discovered characteristics for % @ with error: % @ ", service. UUID, [error localizedDescription]); [self error]; return;} NSLog (@ "service: % @", service. UUID); for (CBCharacteristic * characteristic in service. characteristics) {// discovers the feature if ([characteristic. UUID isEqual: [CBUUID UUIDWithString: @ "xxxxxxx"]) {NSLog (@ "listener: % @", characteristic );
// Listener features [self. peripheral setpolicyvalue: YES forCharacteristic: characteristic] ;}}

5. Data Interaction with peripherals (read and write)  

Read

-(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 = warn therror_system; [self error]; return;} // NSLog (@ "received data: % @", characteristic. value); [self decodeData: characteristic. value];}

Write

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

  


How to send data when developing ios Bluetooth 40?

Recently, I am working on this project. The Penguin is in the name. You can join me and talk about it.
 
Ios Bluetooth development?

If you select IOS, you should learn more:
Is it because the teacher has development experience in this area, rather than a self-taught teacher giving classes to the students? |;
Determine whether there are real online appstore applications;
Whether professional ios teaching devices are available ~;
Whether to pay after employment;
Try 3G 3G 3G 3G, which is a good result.; kT

Related Article

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.