示例#1
0
        private void detector_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                if (_bluetoothStream != null)
                    _bluetoothStream.Close();

                _client = new BluetoothClient();

                BluetoothDeviceInfo gadgeteerDevice = null;

                while (gadgeteerDevice == null)
                {
                    gadgeteerDevice = _client.DiscoverDevices().Where(d => d.DeviceName == "Gadgeteer")
                        .FirstOrDefault();
                    UpdateStatus("Still looking...");
                }

                if (gadgeteerDevice != null)
                {
                    _client.SetPin(gadgeteerDevice.DeviceAddress, "1234");
                    _client.Connect(gadgeteerDevice.DeviceAddress, BluetoothService.SerialPort);
                    _bluetoothStream = _client.GetStream();

                    e.Result = true;
                }
                else
                {
                    e.Result = false;
                }
            }
            catch (Exception)
            {
                e.Result = false;
            }
        }
示例#2
0
        public void SerialOpen(String portName, BaudRates baud)
        {
            if (portName.StartsWith("COM")){
                BasicPortSettings portSettings = new BasicPortSettings();
                portSettings.BaudRate = baud;
                mPort = new Port(portName+":", portSettings);
                mPort.RThreshold = 1;
                mPort.SThreshold = 1;	// send 1 byte at a time
                mPort.InputLen = 0;
                mPort.Open();
                mPort.DataReceived +=new Port.CommEvent(mPort_DataReceived);
            }else{

                try{
                    BluetoothAddress address = BluetoothAddress.Parse(portName);
                    bluetoothClient = new BluetoothClient();
                    bluetoothClient.SetPin(address, "0000");
                    BluetoothEndPoint btep = new BluetoothEndPoint(address, BluetoothService.SerialPort, 1);
                    bluetoothClient.Connect(btep);
                    stream = bluetoothClient.GetStream();
                    if (stream == null){
                        bluetoothClient.Close();
                        bluetoothClient = null;
                    }else{
                        if (stream.CanTimeout){
                            stream.WriteTimeout = 2;
                            stream.ReadTimeout = 2;
                        }
                    }
                }catch(System.IO.IOException){
                    bluetoothClient = null;
                }

            }
        }
示例#3
0
        private void bt_pair()
        {
            BluetoothAddress BtAdress = null;
            BluetoothClient _blueToothClient;
            bool _beginConnect;
            int c = 0;
            string selDev = null;

            try
            {
                _blueToothClient = new BluetoothClient();
            }
            catch { MessageBox.Show("Bluetooth модуль не подключен!"); return; }

            button7.BeginInvoke((MethodInvoker)(() => { button7.Text = "Wait..."; button7.Enabled = false; }));
            comboBox6.BeginInvoke((MethodInvoker)(() => selDev = comboBox6.SelectedItem.ToString()));

            var devices = _blueToothClient.DiscoverDevices();

            while (BtAdress == null)
            {

                foreach (var device in devices.Where(device => device.DeviceName == selDev))
                {
                    BtAdress = device.DeviceAddress;
                    Console.WriteLine("Device found, Address:" + BtAdress.ToString());
                }

                if (BtAdress != null)
                    break;

                if (c > 2)
                {
                    MessageBox.Show("Невозможно подключиться к устройству!");
                    button7.BeginInvoke((MethodInvoker)(() => { button7.Text = "Open"; button7.Enabled = true; }));
                    return;
                }

                devices = _blueToothClient.DiscoverDevices();

                c++;
            }

            BluetoothDeviceInfo _bluetoothDevice = null;

            try
            {
                 _bluetoothDevice = new BluetoothDeviceInfo(BtAdress);
            }
            catch (System.ArgumentNullException) { }

            if (BluetoothSecurity.PairRequest(_bluetoothDevice.DeviceAddress, "1111"))
            {
                Console.WriteLine("Pair request result: :D");

                if (_bluetoothDevice.Authenticated)
                {
                    Console.WriteLine("Authenticated result: Cool :D");

                    _blueToothClient.SetPin("1111");

                    _blueToothClient.BeginConnect(_bluetoothDevice.DeviceAddress, BluetoothService.SerialPort, null, _bluetoothDevice);
                    _beginConnect = true;

                    bt_serial(BtAdress.ToString()); // Open Serial port for Bluetooth

                    if (btSerialPort != null)
                    {
                        button7.BeginInvoke((MethodInvoker)(() => button7.Text = "Open"));
                        button6.BeginInvoke((MethodInvoker)(() => button6.Enabled = true));
                        groupBox6.BeginInvoke((MethodInvoker)(() => groupBox6.Enabled = true));
                    }
                }
                else
                {
                    Console.WriteLine("Authenticated: So sad :(");

                    bt_pair();
                }
            }
            else
            {
                Console.WriteLine("PairRequest: Sad :(");

                MessageBox.Show("Невозможно подключиться к устройству!");
                button7.BeginInvoke((MethodInvoker)(() => { button7.Text = "Open"; button7.Enabled = true; }));
                return;
            }
        }
示例#4
0
        public void Open(String url)
        {
            try{

            var parsed_url = ParseUrl(url);
            Logger.trace("BluetoothStream", "Open " + parsed_url[URL_ADDR] + " serviceid " + parsed_url[URL_SVC] + " pin " + parsed_url[URL_PIN]);

            try{
                BluetoothRadio.PrimaryRadio.Mode = RadioMode.Discoverable;
            }catch(Exception e){
                Logger.error("BluetoothStream", "set_Mode", e);
            }

            BluetoothAddress address = BluetoothAddress.Parse(parsed_url[URL_ADDR]);

            bluetoothClient = new BluetoothClient();

            try{
                if (parsed_url[URL_PIN] != null)
                {
                    bluetoothClient.SetPin(address, parsed_url[URL_PIN]);
                }
            }catch(Exception){
                Logger.error("BluetoothStream", "SetPin");
            }

            #if xxBLUETOOTH_USE_PAIRREQUEST
            // advice from some user - but was useless. other user reported "unable to connecte because of this"
            if (parsed_url[URL_PIN] != null)
            try{
                for(var i = 0; i < 3; i++){
                    var res = BluetoothSecurity.PairRequest(address, parsed_url[URL_PIN]);
                    if (res)
                        break;
                    Logger.error("BluetoothStream", "PairRequest failed, retry "+i);
                }
            }catch(Exception){
                Logger.error("BluetoothStream", "PairRequest");
            }
            #endif

            BluetoothEndPoint btep;

            // force serviceid for some popular china BT adapters
            if (parsed_url[URL_SVC] == null && try_with_service) {
                parsed_url[URL_SVC] = "1";
            }

            if (parsed_url[URL_SVC] != null)
                btep = new BluetoothEndPoint(address, BluetoothService.SerialPort, int.Parse(parsed_url[URL_SVC]));
            else
                btep = new BluetoothEndPoint(address, BluetoothService.SerialPort);
            bluetoothClient.Connect(btep);
            stream = bluetoothClient.GetStream();
            if (stream == null){
                bluetoothClient.Close();
                bluetoothClient = null;
            }else{
                if (stream.CanTimeout){
                    stream.WriteTimeout = 2;
                    stream.ReadTimeout = 2;
                }
            }
            }catch(Exception e){
            if (bluetoothClient != null)
            {
                bluetoothClient.Close();
                bluetoothClient = null;
            }
            if (!try_with_service){
                try_with_service = true;
                Open(url);
                return;
            }
            throw;
            }
        }