For iOS, I hope you can step on Bluetooth 4.0.

Source: Internet
Author: User

For iOS, I hope you can step on Bluetooth 4.0.

1. create these three parameters first

@property (nonatomic,strong)CBCentralManager * manager;@property (nonatomic,strong)CBPeripheral     * peripheral;@property (nonatomic,strong)CBCharacteristic *writeDataCharacteristic

2. initialize CBCentralManager

-(CBCentralManager *)manager{    if (!_manager )    {        _manager  = [[CBCentralManager alloc]initWithDelegate:self queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) options:nil];    }    return _manager;}

3. start scanning for Bluetooth

   [self.manager scanForPeripheralsWithServices:nil options:@{CBCentralManagerRestoredStateScanOptionsKey:@(YES)}];

 

4. This method is called when the Bluetooth status changes.

-(Void) centralManagerDidUpdateState :( CBCentralManager *) central {switch (central. state) {case CBCentralManagerStateUnknown: // NSLog (@ ">>> CBCentralManagerStateUnknown"); {dispatch_async (dispatch_get_main_queue (), ^ {ALERTVIEW (@ "device not supported") ;}) ;}break; case CBCentralManagerStateResetting: // resetting status {dispatch_async (dispatch_get_main_queue (), ^ {ALERTVIEW (@ "resetting status") ;}) ;}nslog (@ ">>> CBCentralManagerStateResetting"); break; case CBCentralManagerStateUnsupported: // status not supported by the device {dispatch_async (dispatch_get_main_queue (), ^ {ALERTVIEW (@ "status not supported by the device ");});} NSLog (@ ">>> CBCentralManagerStateUnsupported"); break; case CBCentralManagerStateUnauthorized: // device unauthorized status {dispatch_async (dispatch_get_main_queue (), ^ {ALERTVIEW (@ "unauthorized device status") ;}) ;}nslog (@ ">>> CBCentralManagerStateUnauthorized"); break; case CBCentralManagerStatePoweredOff: // device closing status {dispatch_async (dispatch_get_main_queue (), ^ {ALERTVIEW (@ "device closing status") ;}} NSLog (@ ">>> CBCentralManagerStatePoweredOff "); break; case CBCentralManagerStatePoweredOn: NSLog (@ ">>> CBCentralManagerStatePoweredOn"); // starts scanning peripheral devices [self. manager scanForPeripheralsWithServices: nil options: @ {CBCentralManagerRestoredStateScanOptionsKey: @ (YES)}]; break; default: break ;}}

5. Connect the peripheral device

-(Void) connect :( CBPeripheral *) peripheral {// connect to the peripheral device [self. manager connectPeripheral: peripheral options: nil];}

6. Connect to the device and call the successful method, and then start scanning.

// Connect to Peripherals-success // this method is called when the services and features in the peripheral are scanned to connect to the peripheral device-(void) centralManager :( CBCentralManager *) central didConnectPeripheral :( CBPeripheral *) peripheral {NSLog (@ ">>> connect to the device named (% @)-successful", peripheral. name); // The configured peripheral delegate CBPeripheralDelegate // @ interface ViewController: UIViewController [peripheral setDelegate: self]; // After scanning the peripheral Services, the method-(void) is displayed) peripheral :( CBPeripheral *) peripheral didDiscoverServices :( NSError *) error {[peripheral discoverServices: nil];} // connect to Peripherals-failure-(void) centralManager :( CBCentralManager *) central didFailToConnectPeripheral :( CBPeripheral *) peripheral error :( NSError *) error {NSLog (@ ">>> connect to the device named (% @)-failed, cause: % @", [peripheral name], [error localizedDescription]);}

7. Discover services and device services

// Discovery Service-(void) centralManager :( CBCentralManager *) central didDiscoverPeripheral :( CBPeripheral *) peripheral advertisementData :( NSDictionary *) advertisementData :( NSNumber *) RSSI {NSLog (@ "Discovered services for % @", peripheral. name); if (! [Self. cecearray containsObject: peripheral]) {[self. performancearray addObject: peripheral]; NSLog (@ "% @", peripheral); dispatch_async (dispatch_get_main_queue (), ^ {[_ tableView reloadData];});} /*** it is found that the service of the peripheral device will come to this method (services of peripheral are directly added after scanning to the Service) */-(void) peripheral :( CBPeripheral *) peripheral didDiscoverServices :( NSError *) error {NSLog (@ "discover peripheral services"); for (CBService * serivce in peripheral. services) {NSLog (@ "===%@ ------ % @ ++", serivce. UUID. UUIDString, self. peripheral. identifier); if ([serivce. UUID. UUIDString isEqualToString: identifier]) {// characteristicuids: You can specify the feature to be scanned (pass nil to scan all features) [peripheral discoverCharacteristics: nil forService: serivce];}

8. Find the service of the device and then scan the feature

-(Void) peripheral :( CBPeripheral *) peripheral didDiscoverCharacteristicsForService :( CBService *) service error :( NSError *) error {NSLog (@ "discovering peripheral device features "); for (CBCharacteristic * characteristic in service. characteristics) {NSLog (@ "===%@ ------ +", characteristic. UUID. UUIDString); if ([characteristic. UUID. UUIDString isEqualToString: Signature]) {// obtain features and interact with peripheral devices. [self policycharacteristic: peripheral characteristic: characteristic];} for (CBCharacteristic * characteristic in service. characteristics) {NSLog (@ "===%@ ------ +", characteristic. UUID. UUIDString); if ([characteristic. UUID. UUIDString isEqualToString: UUIDSTR_ISSC_TRANS_RX]) {// obtain the feature, and interact with the peripheral device to save and write the feature self. writeDataCharacteristic = characteristic ;}}}

 

9. Write the notification in the last step.

// Set notification-(void) Authorization :( CBPeripheral *) peripheral characteristic :( CBCharacteristic *) characteristic {// set notification. The data notification will enter: notification method [peripheral setpolicyvalue: YES forCharacteristic: characteristic]; [self. manager stopScan];} // notification cancellation-(void) initialize :( CBPeripheral *) peripheral characteristic :( CBCharacteristic *) characteristic {[peripheral setpolicyvalue: NO forCharacteristic: characteristic];}

 

10. Writing data is only one step away.

// Write data-(void) writeCharacteristic :( CBPeripheral *) peripheral characteristic :( CBCharacteristic *) characteristic value :( NSData *) value {// only characteristic. properties can write if (characteristic. properties & CBCharacteristicPropertyWrite) {/* the best type parameter can be CBCharacteristicWriteWithResponse or type: response. The difference is whether there is feedback */[peripheral writeValue: value forCharacteristic: characterist Ic type: CBCharacteristicWriteWithResponse];} else {NSLog (@ "this field cannot be written! ");}}

11. I hope to help people and let others fall short.

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.