使用32feet通过蓝牙传输文件到iphone

时间:2018-04-03 15:16:26

标签: c# bluetooth file-transfer 32feet

我正在尝试使用32feet蓝牙将文件传输到我的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为我打电话,但传输文件失败。不知道实际的错误是什么,我几乎尝试了所有可用的服务而没有运气。

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

2 个答案:

答案 0 :(得分:0)

是否可以通过蓝牙将文件从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请求而不是所需的应用程序。 / p>

答案 1 :(得分:0)

我离开了一段时间。并开始尝试使用xamiren,但随后必须创建一个虚拟Mac,以便我可以拥有苹果商店来在手机上加载软件。从那里“萨默林”应该“很好”地工作,但是它的另一个领域和更多领域需要进一步研究。