繁体   English   中英

在 C# 应用程序中通过蓝牙发送文件

[英]send file via bluetooth in c# application

我想自动检测我电脑周围的所有设备并向他们发送文件

我正在使用 brecham 和 inthehand dll,

这是我的代码:

        BluetoothClient bc = new BluetoothClient();
        BluetoothDeviceInfo[] info = null;
        info = bc.DiscoverDevices(999);
        foreach (BluetoothDeviceInfo device in info)
        {
            lstDevices.Items.Add(device.DeviceName + device.DeviceAddress);
            device.Update();
            device.Refresh();
            device.SetServiceState(BluetoothService.ObexObjectPush, true);

            if (!device.Authenticated)
            {
                // Use pin "0000" for authentication
                if (!BluetoothSecurity.PairRequest(device.DeviceAddress, "0000")){
                    MessageBox.Show("Request failed");
                }

            }

            var file = @"C:\1.jpg";
            var uri = new Uri("obex://" + info[1].DeviceAddress + "/" + file);
            var request = new ObexWebRequest(uri);
            request.ReadFile(file);
            var response = (ObexWebResponse)request.GetResponse();
            MessageBox.Show(response.StatusCode.ToString());
            //check response.StatusCode
            response.Close();
        }

但我收到消息“请求失败!” 请问有人可以纠正我吗?

有人有想法吗?

通过对代码的一些改动解决了问题:

        if (!BluetoothRadio.IsSupported)
            MessageBox.Show("No Bluetooth device detected.");
        if (BluetoothRadio.PrimaryRadio.Mode == RadioMode.PowerOff)
            BluetoothRadio.PrimaryRadio.Mode = RadioMode.Connectable;
        MessageBox.Show(BluetoothRadio.PrimaryRadio.Name.ToString());
        MessageBox.Show(BluetoothRadio.PrimaryRadio.Mode.ToString());
        BluetoothClient bc = new BluetoothClient();
        BluetoothDeviceInfo[] info = null;
        info = bc.DiscoverDevices(999);
        foreach (BluetoothDeviceInfo device in info)
        {
            lstDevices.Items.Add(device.DeviceName + " - " + device.DeviceAddress);
            device.Update();
            device.Refresh();
            device.SetServiceState(BluetoothService.ObexObjectPush, true);
            if (!device.Authenticated){
                // Use pin "0000" for authentication
                if (!BluetoothSecurity.PairRequest(device.DeviceAddress, "0000")){
                    MessageBox.Show("Request failed");
                }
            }
            var file = @"d:\1.jpg";
            var uri = new Uri("obex://" + device.DeviceAddress + "/" + file);
            var request = new ObexWebRequest(uri);
            request.ReadFile(file);
            var response = (ObexWebResponse)request.GetResponse();
            MessageBox.Show(response.StatusCode.ToString());
            // check response.StatusCode
            response.Close();
        }

希望它对你和所有需要它的人有用:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM