Bluetooth Phone Center devices connect peripherals to discover their services, read their features, and then update them.

Source: Internet
Author: User

viewcontroller.m//ble1////Created by Shawn on 15-1-29.//Copyright (c) 2015 BILL-JC. All rights reserved.//#import "ViewController.h" #import <CoreBluetooth/CoreBluetooth.h> @interface Viewcontroller () <CBCentralManagerDelegate,CBPeripheralDelegate,CBPeripheralManagerDelegate> @property ( Nonatomic, strong) Cbcentralmanager *centralmanager; @property (nonatomic, strong) Nsmutablearray *peripherals;@    End@implementation viewcontroller-(void) viewdidload{[Super Viewdidload];    [Self scanperipherals]; } #pragma mark-Scan peripherals-(void) scanperipherals{Self.centralmanager = [[Cbcentralmanager alloc] Initwithdelegate:sel    f Queue:nil Options:nil]; if (self.centralManager.state = = Cbcentralmanagerstatepoweredon) {[Self.centralmanager Scanforperipheralswithservi            Ces:nil Options:nil]; }} #pragma mark-cbcentralmanagerdelegate method, callback with Bluetooth device status turned on-(void) Centralmanagerdidupdatestate: (Cbcentralmanager *) central{if (central.state = = CbcentralmanagerstatepOweredon) {[Self.centralmanager scanforperipheralswithservices:nil options:nil]; }} #pragma mark-connect the peripheral Bluetooth device.-(void) Centralmanager: (Cbcentralmanager *) Central diddiscoverperipheral: (Cbperipheral *) Peripheral Advertisementdata: (nsdictionary *) advertisementdata RSSI: (NSNumber *) rssi{[Self.centralmanager StopScan]    ; if (![        Self.peripherals Containsobject:peripheral]) {peripheral.delegate = self;        NSLog (@ "peripheral:%@", peripheral);        Self.peripherals = [@[]mutablecopy];        [Self.peripherals addobject:peripheral];//should be retained before connecting peripherals, otherwise the ARC compiler will release the peripheral object and cause it to fail to connect by adding an array that maintains a list of peripheral devices to preserve.    [Self.centralmanager connectperipheral:peripheral Options:nil];    }//know the identifier of the peripheral device, you can use the following method without scanning. It is a good practice to try to connect a known peripheral device every time you want to scan the device, and scan it to compare charges, and avoid it as much as possible. Nsarray *PERIPHERALS1 = [Self.centralmanager retrieveperipheralswithidentifiers:@[[cbuuid UUIDWithString:@ "    7bddc62c-d916-7e4b-4d09-285e11164936 "]]; #pragma mark-Get a list of services provided by peripherals, which is also a delegate method in Cbmanagerdelegate, discoveringService, if the connection is successful, you can know in the following ways that the next step is to discover the services provided by the perimeter.//You can get the list of services from the peripherals ' services property-(void) Centralmanager: (Cbcentralmanager *) Central didconnectperipheral: (cbperipheral *) peripheral{[peripheral discoverservices:nil];} #pragma mark-After the service has been discovered, a service typically has one or more features-(void) Peripheral: (cbperipheral *) Peripheral diddiscoverservices: ( Nserror *) error{[peripheral.services enumerateobjectsusingblock:^ (id obj, Nsuinteger idx, BOOL *stop) {CBSe        Rvice *service = obj; if ([Service. UUID isequal:[cbuuid uuidwithstring:@ "f000aa00-0451-4000-b000-000000000000"]) {//[peripheral DiscoverCharacter        Istics:nil Forservices:service]; }}];//[peripheral Discovercharacteristics:nil Forservices:service];} #pragma mark-Opens the notification function to get the sensor reading characteristics, returning the delegate method for which the attribute was found-(void) Peripheral: (Cbperipheral *) Peripheral Diddiscovercharacteristicsforservice: (cbservice *) Service error: (NSERROR *) error{[service.characteristics     enumerateobjectsusingblock:^ (id obj, Nsuinteger idx, BOOL *stop) {   cbcharacteristic *ch = obj; if ([Ch.            UUID isequal:[cbuuid uuidwithstring:@ "f000aa02-0451-4000-b000-000000000000"]) {uint8_t data = 0x01; [Peripheral Writevalue:[nsdata datawithbytes:&data length:1] forcharacteristic:ch type:        Cbcharacteristicwritewithresponse]; } if ([Ch.  UUID isequal:[cbuuid uuidwithstring:@ "f000aa01-0451-4000-b000-000000000000"]) {[Peripheral SetNotifyValue:YES        FORCHARACTERISTIC:CH]; }    }];} -(void) Peripheral: (cbperipheral *) Peripheral didupdatevalueforcharacteristic: (cbcharacteristic *) characteristic    Error: (Nserror *) error{Float temp = [self temperatureFromData:characteristic.value];    NSLog (@ "temperature:%f", temp);    }-(float) Temperaturefromdata: (NSData *) data{char scratchval[data.length];    int16_t ambtemp;    [Data getbytes:&scratchval length:data.length]; Ambtemp = ((scratchval[0] & 0xff) |    ((Scratchval[3] << 8) & 0xff00)); return (float) (float) ambtemp /(float) 128);} 1100999077786678@end

  

Bluetooth Phone Center devices connect peripherals to discover their services, read their features, and then update them.

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.