Sending message trough bluetooth with xamarin android to uwp for windows 10 iot core

0

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 softkeboard from my android phone as remote keyboard for the scorebord system.

The uwp app is a scorebord sytem that i made 2 years ago and still using it. Now i want to develop an app on android wich i can use as a remote control for the scorebord systeem.

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 realy 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 recieve 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 andoroid app to type something in the uwp app

uwp
bluetooth
xamarin.android
raspberry-pi3
windows-10-iot-core
asked on Stack Overflow Sep 27, 2019 by A.Pols • edited Oct 1, 2019 by A.Pols

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0