Esempio n. 1
0
        /// <summary>
        /// Uses the MDAPI interfaces to find the belt clip radio, join devices,
        /// and determine the virtual port to use.
        /// </summary>
        /// <param name="strPortName">the name of the port we read from the registry</param>
        /// <param name="strZBCName">the name of the belt clip radio</param>
        /// <returns>true if we got a virtual port; false, otherwise</returns>
        //  Revision History
        //  MM/DD/YY Who Version Issue#     Description
        //  -------- --- ------- ---------- -------------------------------------------
        //  08/29/08 AF                     Created
        //  09/04/08 AF                     Removed call to BT_SetDeviceInfoFolder().
        //
        public bool SetUpBluetooth(ref string strPortName, string strZBCName)
        {
            BT_ERROR result      = BT_ERROR.SUCCESS;
            bool     blnContinue = true;
            ushort   usCount     = 0;
            bool     blnResult   = false;

            //Initialize API - must be called prior to any other function call
            if (BToothCe.BT_Init())
            {
                //Set pin so that input dialog box won't be displayed
                BToothCe.BT_SetPin("0000");

                while (blnContinue && (2 > usCount))
                {
                    blnContinue = false;

                    // In a normal operation, we always try to map the device first
                    // assuming it's been paired.
                    result = BToothCe.BT_MapSerialPortEx(ref strPortName, BT_DEVICE.ZB);

                    if (result == BT_ERROR.SUCCESS || result == BT_ERROR.PORT_ALREADY_MAPPED)
                    {
                        blnResult = true;
                        m_Logger.WriteDetail(Logger.LoggingLevel.ZigBeeProtocol,
                                             "Bluetooth Setup: Port successfully mapped");
                    }
                    else if (result == BT_ERROR.SERVICE_NOT_SUPPORTED)
                    {
                        // critical - should never happen
                        m_Logger.WriteDetail(Logger.LoggingLevel.ZigBeeProtocol,
                                             "Bluetooth service NOT supported");
                    }
                    else if (result == BT_ERROR.OUT_OF_PORTS)
                    {
                        // critical - reboot the device (PDA) and try again
                        m_Logger.WriteDetail(Logger.LoggingLevel.ZigBeeProtocol,
                                             "Bluetooth Setup: No available ports found. Reboot the handheld device");
                    }
                    else if (result == BT_ERROR.FAILED_CREATE_PORT)
                    {
                        // unknown error - should never happen (but I've seen it)
                        m_Logger.WriteDetail(Logger.LoggingLevel.ZigBeeProtocol,
                                             "Bluetooth Setup: Failed to create port");
                    }
                    else if (result == BT_ERROR.DEVICE_NOT_PAIRED || result == BT_ERROR.DEVICE_NOT_FOUND)
                    {
                        MyBTDEV[] devs = new MyBTDEV[1];
                        devs[0] = new MyBTDEV();
                        byte[] abyDevs     = new byte[MyBTDEV.Length];
                        int    numElements = 1;

                        // Scan for available BT devices.
                        // NOTE: A time consuming function; so running in the main thread
                        // can lock up the interface until the function returns
                        BToothCe.BT_GetAvailableBTDevices(abyDevs, ref numElements, strZBCName);
                        if (1 == numElements)
                        {
                            //Retrieve the device information so that we can pair
                            devs = MyBTDEV.ToMyBTDEVArray(abyDevs, numElements);

                            uint uiLowAddress = (uint)(devs[0].addr);
                            uint uiHiAddress  = (uint)(devs[0].addr >> 32);
                            // Try to pair the device
                            if (BT_ERROR.SUCCESS == BToothCe.BT_PairDeviceEx(uiHiAddress, uiLowAddress))
                            {
                                blnContinue = true;
                                m_Logger.WriteDetail(Logger.LoggingLevel.ZigBeeProtocol,
                                                     "Bluetooth Setup: Devices successfully paired");
                            }

                            usCount++;
                        }
                        else
                        {
                            m_Logger.WriteDetail(Logger.LoggingLevel.ZigBeeProtocol,
                                                 "Could not discover the selected Belt Clip Radio");
                        }
                    }
                }

                m_blnUsingBluetooth = true;
            }
            else
            {
                BToothCe.BT_Deinit();
                m_Logger.WriteDetail(Logger.LoggingLevel.ZigBeeProtocol,
                                     "Bluetooth initialization failed!");
            }

            return(blnResult);
        }