如何使用带有HC的SPP将数据从Android发送到Xamarin.Android中的Arduino-06

时间:2018-12-18 23:09:45

标签: c# bluetooth xamarin.android spp

我目前能够读取从Arduino发送到Android手机的数据,但是当我尝试发送数据时,在Arduino端却一无所获。

下面是我的BluetoothManager类,该类在主活动中被实例化并设置。然后,使用您要发送的数据调用.sendDataToDevice(String data)方法。我已经在互联网上搜索了很多,但是在查找与该代码的设置方式接近的示例时遇到了麻烦。我以为我可能在输出流中做错了,但不确定。任何帮助将不胜感激!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Bluetooth;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Java.IO;
using Java.Util;

namespace BluetoothTest
{
    class BluetoothManager
    {
        //Unique ID which helps us connect to any device 
        private const string UuidUniverseProfile = "00001101-0000-1000-8000-00805f9b34fb";
        //Represent bluetooth data coming from UART
        private BluetoothDevice result;
        //get input/output stream of this comunication
        private BluetoothSocket mSocket;
        //convert byte[] to readable strings
        private BufferedReader reader;
        private BufferedWriter writer;//----------------------
        private System.IO.Stream mStream;
        private InputStreamReader mReader;

        private System.IO.Stream oStream;
        private OutputStreamWriter oWriter;//-------------------



        public BluetoothManager()
        {
            reader = null;
            writer = null;//---------------------------
        }



        public UUID getUUIDFromString()
        {
            return UUID.FromString(UuidUniverseProfile);
        }



        private void close(IDisposable aConnectableObject)
        {
            if (aConnectableObject == null) return;
            try
            {
                aConnectableObject.Dispose();
            }
            catch(Exception)
            {
                throw;
            }
            aConnectableObject = null;
        }


        public String getDataFromDevice()
        {
            return reader.ReadLine();
        }

        public void sendDataToDevice(String data)//---------------
        {
            writer.Write(data.ToCharArray());//------------------------         
        }


        private void openDeviceConnection(BluetoothDevice btDevice)
        {
            try
            {
                //Getting socket from specific device
                mSocket = btDevice.CreateRfcommSocketToServiceRecord(getUUIDFromString());
                //blocking operation
                mSocket.Connect();
                //input stream
                mStream = mSocket.InputStream;
                //output stream
                //mSocket.OutputStream;
                oStream = mSocket.OutputStream;//----------------------------------

                oWriter = new OutputStreamWriter(oStream);//--------------------------
                writer = new BufferedWriter(oWriter);//-------------------------------

                mReader = new InputStreamReader(mStream);
                reader = new BufferedReader(mReader);
            }
            catch(IOException e)
            {
                //Close all
                close(mSocket);
                close(mStream);
                close(mReader);
                close(oStream);
                close(oWriter);//----------------------------------
                throw e;
            }
        }


        public void getAllPairedDevices()
        {
             //your android phone bluetooth device
            BluetoothAdapter btAdapter = BluetoothAdapter.DefaultAdapter;

            var devices = btAdapter.BondedDevices;
            if(devices != null && devices.Count > 0)
            {
                foreach (BluetoothDevice mDevice in devices)
                {
                    //search threw all paired devices
                    //mDevice.Name.Split(' ');
                    openDeviceConnection(mDevice);
                }
            }
        }
    }
}

0 个答案:

没有答案