Search code examples
c#bluetoothfile-transfer32feet

tranfer file via bluetooth to iphone using 32feet


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.


Solution

  • 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.