With the development of iOS projects, many apps need to be connected to the device through Bluetooth.
Bluetooth development attention:
Define central devices and peripheral devices first and comply with Bluetooth protocols
@interface ViewController()<CBCentralManagerDelegate,CBPeripheralDelegate> @property (strong, nonatomic) CBCentralManager *manager; @property (nonatomic, strong) CBPeripheral *peripheral; @property (nonatomic, weak)NSTimer * connentTimer; @end
Implement the delegate method again
1. Determine the Bluetooth status. If successful, scan the specified UUID device (if you do not specify the UUID, the background cannot be continuously connected)
2. When the specified device is found, connect the device
3. When the specified peripheral device is connected successfully, write a timer and read RSSI once per second
4. When monitoring loses connection with peripherals, re-establish the connection
5. When reading the RSSI value, print out its value
//Bluetooth Status- (void)centralManagerDidUpdateState:(CBCentralManager *)central { NSString * state = nil; switch ([central state]) { case CBCentralManagerStateUnsupported: state = @"The platform/hardware doesn't support Bluetooth Low Energy."; break; //The application is not authorized to use Bluetooth case CBCentralManagerStateUnauthorized: state = @"The app is not authorized to use Bluetooth Low Energy."; break; //Bluetooth has not been turned on yet case CBCentralManagerStatePoweredOff: state = @"Bluetooth is currently powered off."; break; //Connection is successful case CBCentralManagerStatePoweredOn: [ scanForPeripheralsWithServices:nil options:nil]; state = @"work"; break; case CBCentralManagerStateUnknown: default: ; } NSLog(@"Central manager state: %@", state); } //Find the device- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI { // Each Bluetooth device has its own unique identifier, and confirm the device it wants to connect to based on the identifier. if ([ isEqual:]) { = peripheral; //Data connection timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(connentPeripheral) userInfo:@"timer" repeats:YES]; [ fire]; } } - (void)connentPeripheral { //Connect peripherals = self; [ connectPeripheral:_peripheral options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]]; } //Called after the connection is successful- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral { NSLog(@"Did connect to peripheral: %@,%@", peripheral,); [peripheral setDelegate:self]; // Find services [peripheral discoverServices:nil]; [ invalidate]; // Monitor whether the equipment is disconnected// [self createWorkDataSourceWithTimeInterval:1]; } //When the monitor loses the connection to the peripheral device, re-establish the connection//This method must be implemented because Bluetooth will interrupt the connection, which just triggers this method to rebuild the connection. Rebuilding the connection may cause RSSI to be read for several seconds. - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error { [ connectPeripheral:peripheral options:nil]; } - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error { NSLog(@"%@",); } //The returned Bluetooth service notification is implemented through the proxy- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error { if (error) { NSLog(@"Discovered services for %@ with error: %@", , [error localizedDescription]); return; } for (CBService *service in ) { // NSLog(@"Service found with UUID: %@", ); //Discovery Service if ([ isEqual:[CBUUID UUIDWithString:@"180D"]])//heart rate { //Looking for feature values in a service [peripheral discoverCharacteristics:nil forService:service]; } } } //The returned Bluetooth characteristic value notification is implemented through the proxy- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error { if (error) { NSLog(@"Discovered characteristics for %@ with error: %@", , [error localizedDescription]); return; } for (CBCharacteristic * characteristic in ) { NSLog(@"characteristic:%@",characteristic); if( [ isEqual:[CBUUID UUIDWithString:@"2A37"]]) { [self notification: characteristicUUID: peripheral:peripheral on:YES]; // [ setNotifyValue:YES forCharacteristic:characteristic]; } } } // Process data sent by Bluetooth- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { } -(void) notification:(CBUUID *) serviceUUID characteristicUUID:(CBUUID *)characteristicUUID peripheral:(CBPeripheral *)p on:(BOOL)on { CBService *service = [self getServiceFromUUID:serviceUUID p:p]; if (!service) { // if ( == NULL) return; // zach ios6 addedche // NSLog(@"Could not find service with UUID on peripheral with UUID \n"); return; } CBCharacteristic *characteristic = [self getCharacteristicFromUUID:characteristicUUID service:service]; if (!characteristic) { // if ( == NULL) return; // zach ios6 added // NSLog(@"Could not find characteristic with UUID on service with UUID on peripheral with UUID\n"); return; } [p setNotifyValue:on forCharacteristic:characteristic]; } -(CBService *) getServiceFromUUID:(CBUUID *)UUID p:(CBPeripheral *)p { for (CBService* s in ) { if ([ isEqual:UUID]) return s; } return nil; //Service not found on this peripheral } -(CBCharacteristic *) getCharacteristicFromUUID:(CBUUID *)UUID service:(CBService*)service { for (CBCharacteristic* c in ) { if ([ isEqual:UUID]) return c; } return nil; //Characteristic not found on this service }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.