【问题标题】:tranfer file via bluetooth to iphone using 32feet使用 32 英尺通过蓝牙将文件传输到 iphone
【发布时间】:2018-08-18 00:59:42
【问题描述】:

我正在尝试使用 32 英尺蓝牙将文件传输到我的 iphone,但似乎无法通过 ObexWebResponse。

我已经阅读了很多关于此的帖子,但似乎没有一个解决方案适合我。

我得到的错误是
// 连接失败
// 请求的地址在其上下文“address:Guid”中无效

    private BluetoothClient _bluetoothClient;
    private BluetoothComponent _bluetoothComponent;
    private List<BluetoothDeviceInfo> _inRangeBluetoothDevices;
    private BluetoothDeviceInfo _hlkBoardDevice;
    private EventHandler<BluetoothWin32AuthenticationEventArgs> _bluetoothAuthenticatorHandler;
    private BluetoothWin32Authentication _bluetoothAuthenticator;

    public BTooth() {
        _bluetoothClient = new BluetoothClient();
        _bluetoothComponent = new BluetoothComponent(_bluetoothClient);
        _inRangeBluetoothDevices = new List<BluetoothDeviceInfo>();
        _bluetoothAuthenticatorHandler = new EventHandler<BluetoothWin32AuthenticationEventArgs>(_bluetoothAutenticator_handlePairingRequest);
        _bluetoothAuthenticator = new BluetoothWin32Authentication(_bluetoothAuthenticatorHandler);

        _bluetoothComponent.DiscoverDevicesProgress += _bluetoothComponent_DiscoverDevicesProgress;
        _bluetoothComponent.DiscoverDevicesComplete += _bluetoothComponent_DiscoverDevicesComplete;

        ConnectAsync();

    }

    public void ConnectAsync() {
        _inRangeBluetoothDevices.Clear();
        _hlkBoardDevice = null;
        _bluetoothComponent.DiscoverDevicesAsync(255, true, true, true, false, null);
    }

    private void PairWithBoard() {
        Console.WriteLine("Pairing...");

        bool pairResult = BluetoothSecurity.PairRequest(_hlkBoardDevice.DeviceAddress, null);

        if (pairResult) {
            Console.WriteLine("Success");
            Console.WriteLine($"Authenticated equals {_hlkBoardDevice.Authenticated}");
        } else {
            Console.WriteLine("Fail"); // Instantly fails
        }
    }

    private void _bluetoothComponent_DiscoverDevicesProgress(object sender, DiscoverDevicesEventArgs e) { _inRangeBluetoothDevices.AddRange(e.Devices); }

    private void _bluetoothComponent_DiscoverDevicesComplete(object sender, DiscoverDevicesEventArgs e) {
        for (int i = 0; i < _inRangeBluetoothDevices.Count; ++i) {
            if (_inRangeBluetoothDevices[i].DeviceName == "Uranus") {
                _hlkBoardDevice = _inRangeBluetoothDevices[i];
                PairWithBoard();
                TransferFile();
                return;
            }
        }
        // no devices found
    }

    private void _bluetoothAutenticator_handlePairingRequest(object sender, BluetoothWin32AuthenticationEventArgs e) {
        e.Confirm = true; // Never reach this line
    }

    // not working
    // transfers a file to the phone
    public void TransferFile() {
        string file = "E:\\test.txt",
            filename = System.IO.Path.GetFileName(file);
        string deviceAddr = _hlkBoardDevice.DeviceAddress.ToString();
        BluetoothAddress addr = BluetoothAddress.Parse(deviceAddr);

       _bluetoothClient.Connect(BluetoothAddress.Parse(deviceAddr), BluetoothService.SerialPort);

       Uri u = new Uri($"obex://{deviceAddr}/{file}");
       ObexWebRequest owr = new ObexWebRequest(u);

       owr.ReadFile(file);
       // error:
       // Connect failed
       // The requested address is not valid in its context ...
       var response = (ObexWebResponse)owr.GetResponse();

       Console.WriteLine("Response Code: {0} (0x{0:X})", response.StatusCode);

       response.Close();

    }

配对和身份验证工作正常,我可以让 BluetoothService.Handsfree 为我拨打电话,但文件传输失败。不知道实际错误是什么,我尝试了几乎所有可用的服务,但没有成功。

你能帮我弄清楚发生了什么吗?这是我第一次尝试使用蓝牙服务,所以我还有很多东西要学。

【问题讨论】:

  • 它是否适用于较短的电缆?你用什么波特率?设置为较低的波特率可能会解决问题。
  • 这是蓝牙。不涉及电缆或电线
  • 那么它在靠近的时候有效吗?普通 Wifi 可以在大约 2- - 25 英尺范围内正常工作。您正在推动它在 32 英尺处使用任何放大功能工作。一些 Wifi 设备以更高的功率发送,但这将在一个方向上工作,而不是在另一个方向上工作(除非两端都使用放大器)。
  • 距离没有区别
  • 我认为你需要修改这一行:Uri u = new Uri($"obex://{deviceAddr}/{file}");

标签: c# bluetooth file-transfer 32feet


【解决方案1】:

是否可以通过蓝牙将文件从 iPhone 传输到 Windows 桌面?

但是,如果您需要从 Android 设备传输媒体文件(图像、视频等),您可以使用 32Feet 库提供的 ObexListener 类来实现此目的,然后您可以简单地调用_obexListener.GetContext() 方法将阻塞并等待传入​​的连接。

收到新连接后,可以将收到的文件保存到本地存储,如下例所示:

ObexListener _listener = new ObexListener();
_listener.Start();

// This method will block and wait for incoming connections
ObexListenerContext _context = _listener.GetContext();

// Once new connection is received, you can save the file to local storage
_context.Request.WriteFile(@"c:\sample.jpg");

注意:在 Windows 上使用 OBEX 时,请确保禁用 “蓝牙 OBEX 服务” Windows 服务,以免它处理传入的 OBEX 请求而不是所需的应用程序。

【讨论】:

    【解决方案2】:

    我离开了一段时间。并开始尝试使用 xamiren,但后来不得不创建一个虚拟 Mac,这样我就可以让苹果商店在我的手机上加载软件。从那里 xamerin '应该' 运作良好,但它是另一个领域,还有更多需要解决的问题。

    【讨论】:

      猜你喜欢
      • 2012-09-02
      • 2014-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-07
      • 2012-02-07
      • 2013-11-10
      相关资源
      最近更新 更多