1

I am trying to transfer a file to my iphone using 32feet bluetooth, but cannot seem to get past the ObexWebResponse.

I have read many post on this but none of the solutions seem to work for me.

The Error i get is
// Connect failed
// The requested address is not valid in its context "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();

    }

The pairing and authentication works just fine, and I can get the BluetoothService.Handsfree to make a call for me but the transferring of the file fails. Not knowing what the actual error is, I tried almost every service available with no luck.

Can you help me figure out what is going on? This is my first attempt working with Bluetooth services so I still have a ton to learn.

Mike
  • 623
  • 6
  • 26
  • Does it work with a shorter cable? What baud rate are you using? Setting to a lower baud rate may solve issue. – jdweng Apr 03 '18 at 15:35
  • It’s bluetooth. No cables or wires are involved – Mike Apr 03 '18 at 17:14
  • So does it work when it is closer? Normal Wifi works well up to around 2- - 25 feet. You are pushing it to work at 32 feet with any amplification. Some Wifi devices send with higher power, but that will work in one direction but not other direction (unless both ends use an amplifier). – jdweng Apr 03 '18 at 17:17
  • distance makes no difference – Mike Apr 03 '18 at 17:55
  • I think you need to modify this line : Uri u = new Uri($"obex://{deviceAddr}/{file}"); – jdweng Apr 03 '18 at 18:18

2 Answers2

0

Is it possible to transfer a file from iPhone to Windows desktop via Bluetooth?

However, in case you need to transfer media files (images, videos, etc) from Android device, you can use ObexListener class provided by 32Feet library for this purpose, and then you can simply call _obexListener.GetContext() method that will block and wait for incoming connections.

Once a new connection is received, you can save the received file to local storage, as shown in the below example:

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");

NOTE: When working with OBEX on Windows, make sure to disable the "Bluetooth OBEX Service" Windows service, in order not to let it handle the incoming OBEX requests instead of the desired application.

0

I walked away from this for a while. and started Trying to use xamiren but then had to create a virtual Mac so that I could have the apple store to just load software on my phone. From there xamerin 'should' work well but its another field and tons more to firgure out.

Mike
  • 623
  • 6
  • 26