Example #1
0
        public bool Connect(UInt64 bluetoothAddress)
        {
            if (_connected)
            {
                Disconnect();
            }

            Program.Debug.WriteLine("Connecting to: " + bluetoothAddress);

            //_connected = true;
            //return true;

            _bluetoothAddress = bluetoothAddress;

            int    iErr = 0;
            ushort outmtu = 0, intrCid = 0, ctrlCid = 0;

            _connected = false;

            if (0 != _bluetoothAddress)
            {
                UInt64 ba = _bluetoothAddress;

                Program.Debug.WriteLine("Connect CTRL: " + bluetoothAddress);
                if (0 == (iErr = L2CAPAPI.L2CAPConnect(ref ba, L2CAP_PSM_HIDP_CTRL, 48, ref ctrlCid, ref outmtu)))
                {
                    Program.Debug.WriteLine("Connect INTR: " + bluetoothAddress);
                    if (0 == (iErr = L2CAPAPI.L2CAPConnect(ref ba, L2CAP_PSM_HIDP_INTR, 48, ref intrCid, ref outmtu)))
                    {
                        _connected = true;
                    }
                    else
                    {
                        Program.Debug.WriteError("Connect INTR failed", iErr);
                    }
                }
                else
                {
                    Program.Debug.WriteError("Connect CTRL failed", iErr);
                }
            }
            else
            {
                if (!_connected)
                {
                    _connected = false;

                    UInt64 ba = 0;
                    if (0 == (iErr = L2CAPAPI.L2CAPListen(L2CAP_PSM_HIDP_CTRL, 48)))
                    {
                        if (0 == (iErr = L2CAPAPI.L2CAPListen(L2CAP_PSM_HIDP_INTR, 48)))
                        {
                            if (0 == (iErr = L2CAPAPI.L2CAPAccept(L2CAP_PSM_HIDP_CTRL, ref ba, ref ctrlCid, ref outmtu)))
                            {
                                if (0 == (iErr = L2CAPAPI.L2CAPAccept(L2CAP_PSM_HIDP_INTR, ref ba, ref intrCid, ref outmtu)))
                                {
                                    _connected        = true;
                                    _bluetoothAddress = ba;
                                }
                                else
                                {
                                    Program.Debug.WriteError("Accept INTR failed", iErr);
                                }
                            }
                            else
                            {
                                Program.Debug.WriteError("Accept CTRL failed", iErr);
                            }
                        }
                        else
                        {
                            Program.Debug.WriteError("Listen CTRL failed", iErr);
                        }
                    }
                    else
                    {
                        Program.Debug.WriteError("Listen INTR failed", iErr);
                    }

                    if (!_connected)
                    {
                        Program.Debug.WriteLine("Disconnecting: " + bluetoothAddress);
                        iErr = L2CAPAPI.L2CAPClosePSM(L2CAP_PSM_HIDP_INTR);
                        iErr = L2CAPAPI.L2CAPClosePSM(L2CAP_PSM_HIDP_CTRL);
                    }
                }
            }

            if (_connected)
            {
                Program.Debug.WriteLine("Connected. Launching listeners: " + bluetoothAddress);
                // write our stuff here
                //unsafe
                //{
                //    L2CAPAPI.btFLOWSPEC flowSpec = new L2CAPAPI.btFLOWSPEC();
                //    flowSpec.token_rate = 900;
                //    flowSpec.token_bucket_size = 20;
                //    flowSpec.peak_bandwidth = 900;
                //    flowSpec.latency = 10;
                //    flowSpec.delay_variation = 10;
                //    flowSpec.service_type = 1;

                //    iErr = L2CAPAPI.L2CAPConfigReq(_cidIntr, 48, 0xFF, &flowSpec, 0, null);
                //}

                ThreadStart listenThreadIntr = new ThreadStart(ListenOnIntr);
                ThreadStart listenThreadCtrl = new ThreadStart(ListenOnCtrl);

                _cidIntr = intrCid;
                _cidCtrl = ctrlCid;

                _intrThread = new Thread(listenThreadIntr);
                _intrThread.IsBackground = true;
                _ctrlThread = new Thread(listenThreadCtrl);
                _ctrlThread.IsBackground = true;

                _intrThread.Start();
                _ctrlThread.Start();

                _state = 1;

                Program.Debug.WriteLine("Firing connected event");
                if (null != Connected)
                {
                    Connected(this, null);
                }
            }
            return(_connected);
        }