程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> 圖片-c#藍牙通信,程序報錯。

圖片-c#藍牙通信,程序報錯。

編輯:編程綜合問答
c#藍牙通信,程序報錯。

我是新手,C#才起步,就先拿著其他大神的程序練練手,跪請大神幫忙看一下是怎麼回事。圖片說明
代碼是:using InTheHand.Net;
using InTheHand.Net.Bluetooth;
using InTheHand.Windows.Forms;
using System;
using System.IO;
using System.Net;
using System.Threading;
using System.Windows.Forms;
namespace _12._2._2
{
public partial class Form1 : Form
{
BluetoothRadio radio = null;//藍牙適配器

string sendFileName = null;//發送文件名

BluetoothAddress sendAddress = null;//發送目的地址

ObexListener listener = null;//監聽器

string recDir = null;//接受文件存放目錄

Thread listenThread, sendThread;//發送/接收線程

public Form1()
{
InitializeComponent();
radio = BluetoothRadio.PrimaryRadio;//獲取當前PC的藍牙適配器

CheckForIllegalCrossThreadCalls = false;//不檢查跨線程調用

if (radio == null)//檢查該電腦藍牙是否可用

{
MessageBox.Show("這個電腦藍牙不可用!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
recDir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
labelRecDir.Text = recDir;

}

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void buttonSelectBluetooth_Click(object sender, EventArgs e)
    {
        SelectBluetoothDeviceDialog dialog = new SelectBluetoothDeviceDialog();
        dialog.ShowRemembered = true;//顯示已經記住的藍牙設備  
        dialog.ShowAuthenticated = true;//顯示認證過的藍牙設備  
        dialog.ShowUnknown = true;//顯示位置藍牙設備  
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            sendAddress = dialog.SelectedDevice.DeviceAddress;//獲取選擇的遠程藍牙地址  
            labelAddress.Text = "地址:" + sendAddress.ToString() + "    設備名:" + dialog.SelectedDevice.DeviceName;
        }  
    }

    private void buttonSelectFile_Click(object sender, EventArgs e)
    {
        OpenFileDialog dialog = new OpenFileDialog();
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            sendFileName = dialog.FileName;//設置文件名  
            labelPath.Text =Path.GetFileName(sendFileName);
        }  
    }

    private void buttonSend_Click(object sender, EventArgs e)
    {
        sendThread = new Thread(sendFile);//開啟發送文件線程  
        sendThread.Start();  
    }
    private void sendFile()//發送文件方法  
    {
        Cursor.Current = Cursors.WaitCursor;
        System.Uri uri = new Uri("obex://" + dialog.SelectedDevice.DeviceAddress.ToString() + "/" + System.IO.Path.GetFileName(dialog.FileName));
        ObexWebResponse response = null;
        try
        {
            ObexWebRequest request = new ObexWebRequest(uri);
            buttonSend.Enabled = false;
            request.ReadFile(sendFileName);//發送文件  
            labelInfo.Text = "開始發送!";
            response = request.GetResponse() as ObexWebResponse;//獲取回應  
            labelInfo.Text = "發送完成!";
        }
        catch (System.Exception ex)
        {
            MessageBox.Show("發送失敗!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            labelInfo.Text = "發送失敗!";
        }
        finally
        {
            if (response != null)
            {
                response.Close();
                buttonSend.Enabled = true;
            }
        }
    }
    private void buttonselectRecDir_Click(object sender, EventArgs e)//選擇接受目錄  
    {

        }

    private void buttonListen_Click(object sender, EventArgs e)
    {
        if (listener == null || !listener.IsListening)
        {
            radio.Mode = RadioMode.Discoverable;//設置本地藍牙可被檢測  
            listener = new ObexListener(ObexTransport.Bluetooth);//創建監聽  
            listener.Start();
            if (listener.IsListening)
            {
                buttonListen.Text = "停止";
                labelRecInfo.Text = "開始監聽";
                listenThread = new Thread(receiveFile);//開啟監聽線程  
                listenThread.Start();
            }
        }
        else
        {
            listener.Stop();
            buttonListen.Text = "監聽";
            labelRecInfo.Text = "停止監聽";
        }  
    }
    private void receiveFile()//收文件方法  
    {
        ObexListenerContext context = null;
        ObexListenerRequest request = null;
        while (listener.IsListening)
        {
            context = listener.GetContext();//獲取監聽上下文  
            if (context == null)
            {
                break;
            }
            request = context.Request;//獲取請求  
            string uriString = Uri.UnescapeDataString(request.RawUrl);//將uri轉換成字符串  
            string recFileName = recDir + uriString;
            request.WriteFile(recFileName);//接收文件  
            labelRecInfo.Text = "收到文件" + uriString.TrimStart(new char[] { '/' });
        }
    }  
    private void labelRecDir_Click(object sender, EventArgs e)
    {

    }
    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        if (sendThread != null)
        {
            sendThread.Abort();
        }
        if (listenThread != null)
        {
            listenThread.Abort();
        }
        if (listener != null && listener.IsListening)
        {
            listener.Stop();
        }
    }  
    }  
}

最佳回答:


http://www.tuicool.com/articles/iYjYn2

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved