Connect two devices under Core bluetooth

Source: Internet
Author: User

First, peripheral managers-post ads

If two phones are connected on a ble basis, one of the phones needs to be used as a peripheral, and the peripherals need to broadcast the data they need to publish to receive and process the central device.

Implementing the peripheral broadcast data and handling the traffic limits in the sending process are as follows:

1. Initialize the peripheral manager

2. Setting up the agent for the peripheral manager

3. Automatically invoke the agent method of the Peripheral manager status-determine if the peripheral manager's device is turned on

    • 3.1. Initializing features
    • 3.2. Initializing the service
    • 3.3. Adding features to the service array
    • 3.4. Add the service to the peripheral manager-complete Peripheral manager

4. When a central device subscribes to a feature in a peripheral, the following methods are called:

-(void) Peripheralmanager: (Cbperipheralmanager *) Peripheral Central: (Cbcentral *) Central Didsubscribetocharacteristic: (cbcharacteristic *) characteristic;

In this method, you get the data that you want to send and send it.

    • 4.1. Get the text in TextView as the data sent
    • 4.2. Calibrate the location of the sending data, since the first byte development is sent, so set to 0
    • 4.3. Start sending data (with data, location in data)

5. When the central device cancels the feature in the peripheral, call the method:

-(void) Peripheralmanager: (Cbperipheralmanager *) Peripheral Central: (Cbcentral *) Central Didunsubscribefromcharacteristic: (cbcharacteristic *) characteristic;

6. Send data

    • 6.1. Make an end callout, if the callout has a value, it means the data is sent to the end. Determines whether the send succeeds, and if successful, assigns the label a value of no for the next send. If the send is unsuccessful, wait for the next send, or exit.
    • 6.2. If the callout does not have a value, the position of the data is directly returned if it is greater than the length of the data (proof of delivery is complete)
    • 6.3. If the location of the data is not greater than the length of the data, set the Send status to Yes to start looping the data content.
    • 6.4. Calculate the size of the data that will be sent
    • If the data sent is greater than 20 bytes, set the data size to 20 bytes
    • Convert all text data to NSData data, intercept, 20 byte size
    • 6.5. Sending intercepted data for broadcast
    • 6.6. If the data sent is nil, return directly
    • 6.7. Convert the sent binary data to a string and output the string contents.
    • 6.8. The data location is added (in order to intercept a long text into 20 bytes per paragraph to send)
    • 6.9. Determine if the data location is greater than or equal to the data length (determine if the send is complete)

6.9.1. Set the end callout status to Yes, send the end callout, if the end callout has a value (on behalf of the data sent to complete), in order to send the data again, reset the end callout status to No
7. When the subscription content is updated, call the following method:

-(void) Peripheralmanagerisreadytoupdatesubscribers: (Cbperipheralmanager *) peripheral;

If the data is updated, send the data again.
8. When the data in the TextView is changed, the broadcast should be stopped

9. When TextView starts editing, add the Finish button to the right of the navigation bar and click this button to destroy the keyboard.

10. Processing start/End broadcast switch

    • 10.1. If you start the broadcast
    • 10.2. Let the peripheral manager broadcast data with XX service
    • 10.3. End the broadcast

Second, the central manager-receive data

1. Initialize the center manager and set up agents and queues

2. Initializing an array of received data

3. Agency methods for central managers

    • 1. Switching status of the central device
      • 1. If the center device is open, return directly
      • 2. If the central device status is turned on, start scanning peripherals
    • 2. Scan the peripherals with XX service, do not filter processing (charge electricity)
    • 3. When a peripheral with an XX service is found, call the proxy method that discovers the peripheral
      • 1. Determine if the signal strength is beyond a reasonable range (-15 ~-35)
      • 2. Determine if the peripheral data is found for this peripheral device, if it is returned directly
      • 3. If not, let the peripheral properties hold the discovered peripheral and connect


4. If the link peripheral fails, call the following proxy method
-(void) Centralmanager: (Cbcentralmanager *) Central didfailtoconnectperipheral: (cbperipheral *) Peripheral Error: ( Nullable nserror *) error;

1. Device and error description for output link failure
2. Clear Linked Data

    • 1. If the peripheral status is not linked, return directly
    • 2. If the service of the peripheral is not empty, traverse the service, if the feature of the service is not empty, traverse the characteristics of the service, if the UUID of the feature is the UUID of the feature in the peripheral, judge whether the feature is in the broadcast, if it is broadcast, stop the broadcast, return.
    • 3. If the peripheral service is empty, cancel the peripheral link directly.

5. If you are connected to a peripheral, call the following proxy method:
-(void) Centralmanager: (Cbcentralmanager *) Central didconnectperipheral: (cbperipheral *) peripheral;

    • 1. Stop the Peripheral scan
    • 2. Clear the data array
    • 3. Setting up the peripheral agent
    • 4. Search for specific XX services via peripherals

4. Proxy methods for peripherals

1. If the XX service is found, the proxy method of the discovery service is invoked, as follows:
-(void) Peripheral: (cbperipheral *) Peripheral diddiscoverservices: (Nullable nserror *) error;

    • 1. Determine if there is an error, output a description of the error, and empty the connected data, return.
    • 2. If there is no error, the specific XX feature is found through the service.

2. If the XX feature is found, call the following proxy method:
-(void) Peripheral: (cbperipheral *) Peripheral Diddiscovercharacteristicsforservice: (Cbservice *) Service error: ( Nullable nserror *) error;

    • 1. Determine if there is an error, output an error description, and empty the connected data, return.
    • 2. Iterate through the features of the service, find the XX features we need, and when found, let the peripheral set the feature's notification value to Yes in order to get notification of the feature.
    • 3. Setup is complete, waiting for data to come in.

3. When updating the features of the peripheral, call the following method:
-(void) Peripheral: (cbperipheral *) Peripheral didupdatevalueforcharacteristic: (cbcharacteristic *) characteristic Error: (Nullable nserror *) error;

1. If there is an error, the output error is described and returned.
2. If there is no error, convert the accepted feature data to a string and decode the UIF-8.
3. Determine if the full data is accepted (judging if the data contains the end tag (EOM))

    • 1. If included, the display data is in the empty TextView.
    • 2. Peripheral settings The notification of this feature is no (unsubscribe from this notification)
    • 3. The center manager disconnects from the peripheral.
    • 4. If the full data is not accepted

1. Stitching the data of this feature into the data properties for saving (splicing)
2. Output of the stitching data obtained
4. Call the following method when the subscription status changes (subscription/unsubscribe occurs):
-(void) Peripheral: (cbperipheral *) Peripheral didupdatenotificationstateforcharacteristic: (CBCharacteristic *) Characteristic error: (Nullable nserror *) error;

    • 1. If an error occurs, the output error description
    • 2. If the feature does not match, return directly
    • 3. If the feature is notifying the state, the output specific which feature is sending the notification.
    • 4. If the feature is not in the notification state, leave the hub device disconnected from the peripheral.
    • 5. Once disconnected from the peripheral, call the following proxy method:

-(void) Centralmanager: (Cbcentralmanager *) Central diddisconnectperipheral: (cbperipheral *) Peripheral Error: ( Nullable nserror *) error;

1. Set the locally discovered peripheral to nil.
2. Restart the scan peripherals.

Connect two devices under Core bluetooth

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.