This project has moved. For the latest updates, please go here.

No Connection could be made because the target machine actively refuse it.

Topics: Bluetooth - Microsoft, Bluetooth - Other, Bluetooth - Windows Phone 8
Jun 23, 2014 at 9:08 AM
Hello,

I have developed windows mobile 6.1 application which search Bluetooth printer and print document on Bluetooth printer.

First time print functionality is working perfectly fine but when I print the document again, then It shows error "No Connection could be made because the target machine actively refuse it" on btClient.Connect(new BluetoothEndPoint(btAddress, BluetoothService.SerialPort)) then I need to restart the printer and then after it will print.

Is there any solution for this??

Below is my print code.
private void btPrint_Click(object sender, EventArgs e)
        {            
            // Activate BT
            BluetoothRadio.PrimaryRadio.Mode = RadioMode.Connectable;
            System.Threading.Thread.Sleep(1000);
            // Connect  
            BluetoothAddress btAddress;
            btAddress = BluetoothAddress.Parse("0022583165F7");            
            BluetoothClient btClient = new BluetoothClient();
            try
            {
                btClient.Connect(new BluetoothEndPoint(btAddress, BluetoothService.SerialPort));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            // Send data
            string CPCLStr1 =
                "! 0 200 200 210 1" + Environment.NewLine +
                "ML 25" + Environment.NewLine +
                "TEXT 7 0 10 20" + Environment.NewLine +
                "Just" + Environment.NewLine +
                "Testing" + Environment.NewLine +
                "ENDML" + Environment.NewLine +
                "FORM" + Environment.NewLine +
                "PRINT" + Environment.NewLine;

            // Convert CPCL String to byte array
            byte[] CPCLbytes1 = ASCIIEncoding.ASCII.GetBytes(CPCLStr1);

            NetworkStream ns = btClient.GetStream();
            ns.Write(CPCLbytes1, 0, CPCLbytes1.Length);

            btClient.Close();
        }