1.3.1 Bluetooth® Low Energy Provisioning Framework Integration and Usage

  1. Setting up iOS project:
    1. Create a new project:
      1. Open Xcode.
      2. Create a new project or open an existing one.
    2. Download the Framework:
      1. Download the MchpBleLib framework from the following link: MBD_Framework_v2.1.0
    3. Add the framework to the project:
      1. Copy the framework file to the app project folder.
      2. Find the Project Setting>General>Framework, Libraries, and Embedded Content.
      3. Add the copied framework file in the folder by clicking on “+”>Add Other…>Add Files….
    4. MBD app uses the same library for developing this feature.
      Figure 1-17. Adding Framework
      Figure 1-18. Framework Added in Project Target
  2. Initialize: Create the Bluetooth Low Energy Provisioning object.
    
    //import library
    import MchpBLELib
     
    var m_ZBPeripheral : ZBProvisionPeripheral?
    m_ZBPeripheral = ZBProvisionPeripheral.initialize()
  3. Features:
    1. Connection:
      1. Implement the delegate.
        m_ZBPeripheral?.ZBConnectionDelegate = self
         
        /// A protocol that provides the peripheral connection related information.
        public protocol ZBProvisionConnectionDelegate {
            /// Tells the delegate that discovered a peripheral while scanning for devices.
            /// - Parameters:
            ///   - peripheral: The discovered peripheral.
            ///   - deviceName: The name of peripheral.
            ///   - rssi: The current received signal strength indicator (RSSI) of the peripheral, in decibels.
            ///   - isConnectable: To indicates the peripheral is connectable or not.
            func didDiscoverPeripheral(peripheral: CBPeripheral,deviceName: String,rssi:NSNumber, isConnectable:Bool)
             
            /// Tells the delegate that  connected to a peripheral.
            func didConnected()
             
            /// Tells the delegate that  disconnected from a peripheral.
            func didDisconnected()
        }
         
        //scan for peripheral
        m_ZBPeripheral?.startScan()
         
        //stop scan
        m_ZBPeripheral?.stopScan()
         
        //connect peripheral
        m_ZBPeripheral?.connectPeripheral(peripheral)
         
        //disconnect current connected device
        m_ZBPeripheral?.disconnectPeripheral()
      2. Scan: to discover the nearby Bluetooth Low Energy Provisioning devices.
        Figure 1-19. Scanning Process
      3. Connection: to connect/disconnect the Bluetooth Low Energy Provisioning devices.
        Figure 1-20. Connection Process
    2. Device Information and Provision Control:
      1. Implement the delegate.
        m_ZBPeripheral?.ZBDelegate = self
         
        /// A protocol that provides updates for the provisioning services status.
        public protocol ZBProvisionResponseDelegate {
            //func didDisconnected()
             
            /// Tells the delegate that retrieving the device type.
            /// - Parameter type: Device type of peripheral.
            func updateDeviceType(type:UInt8)
             
            /// Tells the delegate that retrieving the device name.
            /// - Parameter name: The device name of peripheral.
            func updateDeviceName(name:String)
             
            /// Tells the delegate that retrieving the software version.
            /// - Parameter version: The software version of peripheral.
            ///
            /// The software version is up to 16 bytes.
            func updateSWVersion(version:Data)//up to 16 bytes
             
            /// Tells the delegate that retrieving the channel mapping.
            /// - Parameter channel: The channel mapping of peripheral.
            func updateChannelMap(channel:Data)//4 bytes
             
            /// Tells the delegate that retrieving the current channel.
            /// - Parameter channel: The current channel of peripheral.
            func updateCurrentChannel(channel:UInt8)
             
            /// Tells the delegate that retrieving the device state.
            /// - Parameter state: The device state of peripheral.
            func updateDeviceState(state:UInt8)
             
            /// Tells the delegate that retrieving the MAC ID.
            /// - Parameter id: The MAC ID of peripheral.
            func updateMACID(id:Data)//8bytes
             
            /// Tells the delegate that retrieving the light mode from peripheral.
            /// - Parameter mode: The light mode of peripheral.
            func updateLightMode(mode:UInt8)
             
            /// Tells the delegate that the channel update successfully.
            func didUpdateChannel()
             
            /// Tells the delegate that the device name update successfully.
            func didUpdateDeviceName()
             
            /// Tells the delegate that the update status of commission process.
            /// - Parameter status: The commission upddate status.
            func didCommission(status:UInt8)
             
            /// Tells the delegate that the commission process is started.
            func didStartCommission()
             
            /// Tells the delegate that retriving an error when sending the command.
            /// - Parameters:
            ///   - ID: The command ID.
            ///   - errorCode: The error code.
            ///
            ///   Please refer to the ZB_COMMISSION_COMMAND_ID and ZB_COMMISSION_COMMAND_ID2 for the command ID.
            ///   Please refer to the ERROR_CODE for error code definition.
            func errorResponse(ID:UInt8,errorCode:UInt8)
             
            /// Updates the stack type with the given status.
            /// - Parameter status: The status to update the stack type with.
            func updateStackType(status: UInt8)
         
            /// Updates the channel mask with the given data.
            /// - Parameter data: The data to update the channel mask with.
            func updateChannelMask(data: Data)
         
            /// Updates the PAN ID with the given data.
            /// - Parameter data: The data to update the PAN ID with.
            func updatePanID(data: Data)
         
            /// Updates the extended PAN ID with the given data.
            /// - Parameter data: The data to update the extended PAN ID with.
            func updateExtPanID(data: Data)
         
            /// Updates the network name with the given name.
            /// - Parameter name: The name to update the network name with.
            func updateNetworkName(name: String)
         
            /// Updates the network key with the given data.
            /// - Parameter data: The data to update the network key with.
            func updateNetworkKey(data: Data)
         
            /// Updates the mesh local prefix with the given data.
            /// - Parameter data: The data to update the mesh local prefix with.
            func updateMeshLocalPrefix(data: Data)
         
            /// Updates the user parameter with the given data.
            /// - Parameter data: The data to update the user parameter with.
            func updateUserParameter(data: Data)
         
            /// Delegate method that handles the response after the app sends the SetExtPanID data to the BLE device.
            /// This method is called when the BLE device sends back the set pan ID data response.
            /// The response status indicates whether the operation was successful.
            /// - Parameter status: A value indicating the response status (1 for failure, 0 for success).
            func setPanIdResponse(status: UInt8)
         
            /// Delegate method that handles the response after the app sends the SetExtPanID data to the BLE device.
            /// This method is called when the BLE device sends back the set ext pan ID data response.
            /// The response status indicates whether the operation was successful.
            /// - Parameter status: A value indicating the response status (1 for failure, 0 for success).
            func setExtPanIdResponse(status: UInt8)
         
            /// Delegate method that handles the response after the app sends the setCurrentChannel data to the BLE device.
            /// This method is called when the BLE device sends back the current channel data response.
            /// The response status indicates whether the operation was successful.
            /// - Parameter status: A value indicating the response status (1 for failure, 0 for success).
            func setCurrentChannelResponse(status: UInt8)
         
            /// Delegate method that handles the response after the app sends the SetChannelMask data to the BLE device.
            /// This method is called when the BLE device sends back the channel mask data response.
            /// The response status indicates whether the operation was successful.
            /// - Parameter status: A value indicating the response status (1 for failure, 0 for success).
            func setchannelMaskResponse(status: UInt8)
         
            /// Delegate method that handles the response after the app sends the SetUserParameter data to the BLE device.
            /// This method is called when the BLE device sends back the set user parameter data response.
            /// The response status indicates whether the operation was successful.
            /// - Parameter status: A value indicating the response status (1 for failure, 0 for success).
            func setUserParameterResponse(status: UInt8)
         
            /// Delegate method that handles the response after the app sends the SetNetworkName data to the BLE device.
            /// This method is called when the BLE device sends back the set network name data response.
            /// The response status indicates whether the operation was successful.
            /// - Parameter status: A value indicating the response status (1 for failure, 0 for success).
            func setNetworkNameResponse(status: UInt8)
         
            /// Delegate method that handles the response after the app sends the SetNetworkKey data to the BLE device.
            /// This method is called when the BLE device sends back the set network key data response.
            /// The response status indicates whether the operation was successful.
            /// - Parameter status: A value indicating the response status (1 for failure, 0 for success).
            func setNetworkKeyResponse(status: UInt8)
         
            /// Delegate method that handles the response after the app sends the SetMeshLocalPrefix data to the BLE device.
            /// This method is called when the BLE device sends back the set mesh local prefix data response.
            /// The response status indicates whether the operation was successful.
            /// - Parameter status: A value indicating the response status (1 for failure, 0 for success).
            func setMeshLocalPrefixResponse(status: UInt8)
         
        }
        
      2. Get device information. Refer to the Figure 1-21 and Figure 1-22 read the device information based on the stack type (Thread, MAC, Phy and Zigbee device).
        //fetch software version
        m_ZBPeripheral?.command_GetSwVersion()
         
        //fetch device state
        m_ZBPeripheral?.command_GetDeviceState()
         
        //fetch mac id
        m_ZBPeripheral?.command_GetMacID()
         
        //fetch stack type
        m_ZBPeripheral?.command_GetStackType()
         
        //fetch current channel
        m_ZBPeripheral?.command_GetCurrentChannel()
         
        //fetch device name
        m_ZBPeripheral?.command_GetDeviceName()
        Figure 1-21. Device Information
        Figure 1-22.  Device Information Based on Stack Type
      3. Commission and Provision for Thread, MAC, Phy and Zigbee device.
        //Set Channel Map - Zigbee Device Only
         
        //Initialize a channelMap array with the appropriate number of bytes (supported channel 11 - 26).
        //Set the bits in the channelMap array based on the state of the checkboxes.
        //Convert the channelMap array to a Data object.
        //Pass the Data object to the command_SetChannelMap method.
        m_ZBPeripheral?.command_SetChannelMap(channel: Data(channelMap))
         
        //Get Commission Status
        m_ZBPeripheral?.command_GetCommissionStatus()
         
        //Start Commission
        m_ZBPeripheral?.command_StartCommission()
         
        //Set Current Channel - Thread, MAC, PHY only
        m_ZBPeripheral?.command_SetCurrentChannel(id: dataFromHexString(hexString: hexValue))
         
        //Set Pan Id - Thread, MAC, PHY only
        m_ZBPeripheral?.command_SetPanID(id: dataFromHexString(hexString: hexString!)) // only 2 hexadecimal value
         
        //Set Current Mask - Thread, MAC, PHY only
        m_ZBPeripheral?.command_SetChannelMask(id: dataFromHexString(hexString: hexString!)) // only 4 hexadecimal value
         
        //Set User Parameters - Thread, MAC, PHY only
        m_ZBPeripheral?.command_SetUserParameter(id: dataFromHexString(hexString: hexString!)) // only 16 hexadecimal value
         
        //Set Ext Pan Id - Thread only
        m_ZBPeripheral?.command_SetExtPanID(id: dataFromHexString(hexString: hexString!)) // only 8 hexadecimal value
         
        //Set Network Name - Thread only
        m_ZBPeripheral?.command_SetNetworkName(name: str!) // Maximum 16 characters
         
        //Set Network Key - Thread only
        m_ZBPeripheral?.command_SetNetworkKey(id: dataFromHexString(hexString: hexString!)) // only 16 hexadecimal value
         
        //Set Mesh Local - Thread only
        m_ZBPeripheral?.command_SetMeshLocalPrefix(id: dataFromHexString(hexString: hexString!)) // only 8 hexadecimal value