'Sending message through Bluetooth with xamarin android to uwp for Windows 10 iot core

I want to send text from my xamarin Android app to my uwp app on an raspberry pi 3. also I want to use the softkeyboard from my Android phone as remote keyboard for the scoreboard system.

The uwp app is a scoreboard system that I made 2 years ago and still using it. Now I want to develop an app on Android which I can use as a remote control for the scoreboard system.

The Bluetooth pairing and connection is ok, but I am struggling with sending strings to the uwp app.

I tried several examples over the internet including this one on this site.

Bluetooth connection between Xamarin-Android and UWP

I tried this code but some code was not recognized by Visual Studio like this

uint messageLength = reader.RReadUint ();

also I tried this example and that came really close

https://www.hackster.io/patricia2/bluetooth-remote-control-android-for-windows-iot-devices-ed502d

But the Android part is made in Android Studio java and I want to make an app with xamarin android. But I used the uwp part of this code, and then the Android code of the xamarin Android chats sample.

https://docs.microsoft.com/en-us/samples/xamarin/monodroid-samples/bluetoothchat/

but somehow it is not working

this I have to send a message in Android

void SendMessage(String message)
    {

        if (message.Length > 0)
        {
            var bytes = Encoding.ASCII.GetBytes(message);
            write(bytes)
        }
    }

public void Write(byte[] buffer)
        {
            try
            {
                outStream.Write(buffer, 0, buffer.Length);
            }
            catch (Java.IO.IOException e)
            {
                Log.Error(TAG, "Exception during write", e);
            }
        }

and this is what I use now to receive the message in uwp on a raspberry pi with windows 10 iot core.

while (true)
        {
            try
            {
                uint readLength = await reader.LoadAsync(sizeof(uint));

                if (readLength < sizeof(uint))
                {
                    remoteDisconnection = true;
                    break;
                }

                var currentLength = reader.ReadUInt32();

                readLength = await reader.LoadAsync(currentLength);

                if (readLength < currentLength)
                {
                    remoteDisconnection = true;
                    break;
                }
                string message = reader.ReadString(currentLength);

                Debug.Write("Received: " + message);
            }
            // Catch exception HRESULT_FROM_WIN32(ERROR_OPERATION_ABORTED).
            catch (Exception ex) when ((uint)ex.HResult == 0x800703E3)
            {
                Debug.Write("Client Disconnected Successfully");
                break;
            }
        }

and on this line I get an out of memory error

readLength = await reader.LoadAsync(currentLength);

in the Android sample they use this code to send a message and it works but how do I send it in xamarin android?

ByteBuffer bb = ByteBuffer.allocate(4 + command.Length).putInt(command.Length).put(command.GetBytes());

                mmOutStream.WriteByte(bb.array());
                mmOutStream.Flush();

I hope I explained it a little bit so you understand what i need, and i hope someone can help me please.

is it also possible to when the Bluetooth connection is there, that you can use the softkeyboard on the Android app to type something in the uwp app



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source