public override Hashtable Search()
        {
            Hashtable bt_devices = new Hashtable();

            bt_devices.Clear();
            long device_index = 0;

            //start inquiry
            WidcommAPI.StartInquiry(this.wdStack);

            //bt_devices[0] = "Inq. Result: " + SensorLib.Widcomm.StartInquiry(Widcomm.wdStack).ToString();
            //bt_devices[1] = "Stack Status: " + SensorLib.Widcomm.GetStackStatus(Widcomm.wdStack).ToString();

            DateTime dt_start = DateTime.Now;

            //InquiryCompleteEvent returns the NEXT free device_index

            while (!WidcommAPI.InquiryCompleteEvent(this.wdStack, out device_index) && DateTime.Now < dt_start.AddSeconds(20))
            {
                System.Windows.Forms.Application.DoEvents();
            }

            //bt_devices[0] = "Found:" + device_index.ToString();

            for (int i = 0; i < device_index; i++)
            {
                long          vl = 0;
                System.IntPtr l  = WidcommAPI.DeviceResponded(this.wdStack, out vl, i);
                bt_devices[vl] = Marshal.PtrToStringUni(l);
            }

            return(bt_devices);
        }
Esempio n. 2
0
 public WidcommBluetoothStream(CircularBuffer buffer, CircularBuffer sbuffer, byte[] address, string pin)
     : base(buffer, sbuffer, address, pin)
 {
     wdStack[0] = WidcommAPI.CreateWidcommStack();
     if (wdStack[0] == IntPtr.Zero)
     {
         throw new Exception("Cannot create stack");
     }
 }
 public override bool Initialize()
 {
     //create Widcomm stack object
     this.wdStack = WidcommAPI.CreateWidcommStack();
     //set the stack to auto reconnect = true
     if (this.wdStack != null)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
        public int OpenSPP(long bt_address)
        {
            int comPort = 0;

            WidcommAPI.SppCreateConnection(this.wdStack, 0, bt_address);

            int retry = 0;

            while (retry < 5)
            {
                System.Threading.Thread.Sleep(500);
                comPort = WidcommAPI.SppComPort(this.wdStack);
                retry++;
            }

            return(comPort);
        }
        public override bool Dispose()
        {
            try
            {
                if (m_bDisposed)
                {
                    return(true);
                }

                if (this.wdStack != IntPtr.Zero)
                {
                    WidcommAPI.DeleteWidcommStack(this.wdStack);
                }
                this.wdStack = IntPtr.Zero;

                m_bDisposed = true;
                GC.SuppressFinalize(this);
                return(true);
            }
            catch
            {
                return(false);
            }
        }
        public int CloseSPP()
        {
            int result = WidcommAPI.SppRemoveConnection(this.wdStack);

            return(result);
        }
Esempio n. 7
0
        public bool Open()
        {
            try

            {
                lock (mylock)
                {
                    if (processingThread != null)
                    {
                        processingThread.Abort();
                    }

                    long bt_address = long.Parse(this._HexAddress, NumberStyles.HexNumber, CultureInfo.CurrentCulture.NumberFormat);
                    //Thread.Sleep(10000);
                    //int r = WidcommAPI.SppRemoveConnection(wdStack[0]);
                    //Thread.Sleep(3000);
                    int r = WidcommAPI.SppCreateConnection(wdStack[0], (byte)1, bt_address);
                    Thread.Sleep(3000);
                    if (r != 1)
                    {
                        WidcommAPI.SppRemoveConnection(wdStack[0]);
                        if (wdStack[0] != IntPtr.Zero)
                        {
                            if (WidcommAPI.DeleteWidcommStack(wdStack[0]) == false)
                            {
                                throw new Exception("Cannot delete stack");
                            }
                        }
                        NetworkStacks._BluetoothStack = null;
                    }

                    /* RegistryKey rk = Registry.LocalMachine.OpenSubKey("Software\\WIDCOMM\\BTConfig\\AutoConnect");
                     * string[] subkeys = rk.GetSubKeyNames();
                     * if (subkeys.Length > 0)
                     * {
                     *   for (int i = 0; (i < subkeys.Length); i++)
                     *   {
                     *       int com = Convert.ToInt32(subkeys[i]);
                     *       //if (WidcommBluetoothStack._PortUsed[com])
                     *         //  continue;
                     *       if (com == 11)
                     *       {
                     *           _COMPORT = "BTC1";
                     *           WidcommBluetoothStack._PortUsed[com] = true;
                     *       }
                     *       else if (com == 12)
                     *       {
                     *           _COMPORT = "BTC2";
                     *           WidcommBluetoothStack._PortUsed[com] = true;
                     *       }
                     *       else if (com == 13)
                     *       {
                     *           _COMPORT = "BTC3";
                     *           WidcommBluetoothStack._PortUsed[com] = true;
                     *       }
                     *
                     *       break;
                     *
                     *
                     *   }
                     *
                     *   if (_COMPORT.Length > 2)
                     *       break;
                     * }*/


                    int com = WidcommAPI.SppComPort(wdStack[0]);
                    //if (com == 11)
                    //  _COMPORT = "BTC1";
                    //else if (com == 12)
                    //  _COMPORT = "BTC2";
                    //else if (com == 13)
                    _COMPORT = "BTC3";

                    if ((_COMPORT.Length > 2) && ((r == 1) || (r == 3)))
                    {
                        spp[0] = new SerialPort(_COMPORT + ":", 38400, Parity.None, 8, StopBits.One);//new SerialPort(_COMPORT, 38400, Parity.None, 8, StopBits.One);
                        if (!spp[0].IsOpen)
                        {
                            spp[0].Open();
                        }
                        //spps.Add(this._HexAddress, spp);


                        this._Status = BluetoothStatus.Connected;
                        //start the processing thread
                        processingThread = new Thread(new ThreadStart(Process));
                        processingThread.Start();
                    }
                }
            }
            catch (Exception e)
            {
                spp[0].Close();
                spp[0].Dispose();

                WidcommAPI.SppRemoveConnection(wdStack[0]);
                if (wdStack[0] != IntPtr.Zero)
                {
                    if (WidcommAPI.DeleteWidcommStack(wdStack[0]) == false)
                    {
                        throw new Exception("Cannot delete stack");
                    }
                }
                NetworkStacks._BluetoothStack = null;

                CurrentWockets._LastError        = Wockets.Exceptions.ErrorCodes.CONNECTION_FAILED_TO_OPEN;
                CurrentWockets._LastErrorMessage = "MicrosoftBluetoothStream failed at Open() " + this._HexAddress;
                this._Status = BluetoothStatus.Disconnected;
                return(false);
            }
            return(true);
        }
Esempio n. 8
0
        public override void Process()
        {
            int sendTimer = 0;

            byte[] sendByte = new byte[1];
            sendByte[0] = 0xbb;
            byte[] singleReadBuffer = new byte[LOCAL_BUFFER_SIZE];

            while (this._Status == BluetoothStatus.Connected)
            {
                int bytesReceived = 0;

                try
                {
                    if (sendTimer > 100)
                    {
                        spp[0].Write(sendByte, 0, 1);
                        //WidcommAPI.SendData(wdStack, sendByte, 1);
                        sendTimer = 0;
                        Thread.Sleep(50);
                    }
                    sendTimer++;

                    int availableBytes = spp[0].BytesToRead;
                    if (availableBytes > 0)
                    {
                        bytesReceived = 0;
                        bytesReceived = spp[0].Read(singleReadBuffer, 0, LOCAL_BUFFER_SIZE);

                        // bytesReceived = WidcommAPI.GetData(wdStack, singleReadBuffer);



                        //if we will pass the end of buffer receive till the end then receive the rest

                        /*if ((tail + availableBytes) > this.buffer._Bytes.Length)
                         * {
                         *  bytesReceived = spp.Read(this.buffer._Bytes, tail, this.buffer._Bytes.Length - tail);
                         *  availableBytes -= bytesReceived;
                         *  tail = (tail + bytesReceived) % this.buffer._Bytes.Length;
                         * }
                         * bytesReceived += spp.Read(this.buffer._Bytes, tail, availableBytes);
                         * tail = (tail + bytesReceived) % this.buffer._Bytes.Length;*/
                    }

                    Thread.Sleep(30);

                    if (bytesReceived > 0)
                    {
                        timeoutIterationsCounter = 0;
                    }
                    else
                    {
                        timeoutIterationsCounter++;
                        if (timeoutIterationsCounter > iterationsToTimeout)
                        {
                            spp[0].Close();
                            spp[0].Dispose();

                            WidcommAPI.SppRemoveConnection(wdStack[0]);
                            if (wdStack[0] != IntPtr.Zero)
                            {
                                if (WidcommAPI.DeleteWidcommStack(wdStack[0]) == false)
                                {
                                    throw new Exception("Cannot delete stack");
                                }
                            }
                            NetworkStacks._BluetoothStack = null;
                            //Close();
                            CurrentWockets._LastError = Wockets.Exceptions.ErrorCodes.CONNECTION_TIMEOUT;
                            //this.errorMessage = "MicrosoftBluetoothStream failed at Process(). Disconnection timeout to " + this._HexAddress;
                            this._Status = BluetoothStatus.Disconnected;
                        }
                    }

                    int ii;
                    int mytail = this.buffer._Tail;
                    for (ii = 0; ii < bytesReceived; ii++)
                    {
                        // this.buffer._Timestamp[this.buffer._Tail] = timestamp;
                        this.buffer._Bytes[mytail++] = singleReadBuffer[ii];
                        mytail %= this.buffer._Bytes.Length;
                    }
                    this.buffer._Tail = mytail;
                }
                catch (Exception e)
                {
                    spp[0].Close();
                    spp[0].Dispose();

                    WidcommAPI.SppRemoveConnection(wdStack[0]);
                    if (wdStack[0] != IntPtr.Zero)
                    {
                        if (WidcommAPI.DeleteWidcommStack(wdStack[0]) == false)
                        {
                            throw new Exception("Cannot delete stack");
                        }
                    }
                    NetworkStacks._BluetoothStack = null;
                    this._Status = BluetoothStatus.Disconnected;
                    //Close();
                }
            }
        }