使用Xamarin android将消息槽蓝牙发送到适用于Windows 10 IoT核心版的uwp

时间:2019-09-27 06:11:24

标签: uwp bluetooth xamarin.android raspberry-pi3 windows-10-iot-core

我想将文本从我的xamarin android应用程序发送到树莓派3上的uwp应用程序。我还想将android手机中的softkeboard用作Scorebord系统的远程键盘。

uwp应用程序是我两年前制作并仍在使用的Scorebord系统。现在,我想在android上开发一个应用程序,我可以将其用作scorebord systeem的遥控器。

蓝牙配对和连接还可以,但是我正在努力向uwp应用发送字符串。

我在互联网上尝试了几个示例,包括此站点上的一个示例。

Bluetooth connection between Xamarin-Android and UWP

我尝试了这段代码,但是像这样的Visual Studio无法识别某些代码

uint messageLength = reader.RReadUint ();

我也尝试过这个例子,并且真的很接近

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

但是android部分是在android studio java中制作的,我想用xamarin android制作一个应用程序。但是我使用了这段代码的uwp部分,然后使用了xamarin android chat示例的android代码。

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

但不知何故

这我必须在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);
            }
        }

这就是我现在用来在具有Windows 10 IoT核心的树莓派上通过uwp接收消息的原因。

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;
            }
        }

在这一行上,我遇到内存不足错误

readLength = await reader.LoadAsync(currentLength);

在android示例中,他们使用此代码发送消息,并且可以正常工作,但是如何在xamarin android中发送消息?

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

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

我希望我能稍微解释一下,以便您了解我的需求,希望有人可以帮助我。

当蓝牙连接到位时,您还可以使用Andoroid应用程序上的软键盘在uwp应用程序中键入某些内容

0 个答案:

没有答案
相关问题