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.