使用C#通过蓝牙从Android手机向PC发送数据

时间:2017-01-22 21:22:33

标签: c# android bluetooth

我还没有在这里问过很多问题所以请原谅我,因为我还在学习发布的所有惯例。

我正在尝试构建一个Android移动应用程序,允许用户通过蓝牙触摸屏控制PC鼠标。计划是在PC上安装一个C#程序,它将识别蓝牙连接,从手机接收数据(x和y坐标)并将数据转换为鼠标移动(如果有人有任何其他建议,如何更好地翻译我也很想听听他们的数据)。这就是问题的起点。我是Android开发的新手,没有蓝牙经验。我已经构建了应用程序并可以从屏幕跟踪移动但我无法将手机连接到PC的蓝牙加密狗。我正在使用32英尺试着连接到手机。

我收到的错误是: " System.InvalidOperationException:不允许对未连接的套接字进行操作" - 我认为这是因为我的蓝牙客户端(PC)没有连接到蓝牙设备(手机),我认为这是由于这一行:

BC.BeginConnect(BTDevice.DeviceAddress, BluetoothService.SerialPort, new AsyncCallback(Connect), BTDevice);

但我不确定原因。

以下是我的C#课程,如有任何意见或建议,我将不胜感激:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InTheHand.Net.Sockets;
using InTheHand.Net;
using InTheHand.Net.Bluetooth;
using InTheHand.Windows.Forms;
using System.Net.Sockets;
using System.Diagnostics;
using System.Threading;

namespace bluetooth_connectivity_test
{
static class Program
    {
    static string MY_PAIRING_CODE = "1234";
    static string UUID = "00001101-0000-1000-8000-00805F9B34FB";

    //dongle
    private static BluetoothEndPoint EP = new BluetoothEndPoint(BluetoothAddress.Parse("00:1A:7D:DA:71:13"), BluetoothService.SerialPort);

    //BC used to manage connections;
    private static BluetoothClient BC = new BluetoothClient(EP);

    //phone
    private static BluetoothDeviceInfo BTDevice = new BluetoothDeviceInfo(BluetoothAddress.Parse("04:1B:BA:31:0A:CB"));
    private static NetworkStream stream = null;

    static void Main(string[] args)
    {
        Console.WriteLine(BTDevice.DeviceName + " is a " + BTDevice.ClassOfDevice.MajorDevice.ToString());

        bool is_paired = false;
        is_paired = BluetoothSecurity.PairRequest(BTDevice.DeviceAddress, MY_PAIRING_CODE);

        if (is_paired)
        {
            Console.WriteLine("PairRequest: OK");

            if (BTDevice.Authenticated)
            {
                Console.WriteLine("Authenticated: OK");

                BC.SetPin(UUID);
//
                BC.BeginConnect(BTDevice.DeviceAddress, BluetoothService.SerialPort, new AsyncCallback(Connect), BTDevice);
//
                Console.WriteLine("Decive Name: " + BTDevice.DeviceName);
                Console.WriteLine("Device Connected: " + BTDevice.Connected);

            }
            else
            {
                Console.WriteLine("Authenticated: No");
            }
        }
        else
        {
            Console.WriteLine("PairRequest: No");
        }

        Console.ReadLine();
    }

    private static void Connect(IAsyncResult result)
    {
        if (result.IsCompleted)
        {
            try
            {
                Console.WriteLine("Bluetooth Client Connected: " +BC.Connected);
                //crash because stream = null
                stream = BC.GetStream();
            }
            catch(InvalidOperationException e)
            {
                Console.WriteLine(e);
            }

            if (stream.CanRead)
            {
                byte[] myReadBuffer = new byte[1024];
                StringBuilder myCompleteMessage = new StringBuilder();
                int numberOfBytesRead = 0;

                // Incoming message may be larger than the buffer size. 
                do
                {
                    numberOfBytesRead = stream.Read(myReadBuffer, 0, myReadBuffer.Length);

                    //right
                    for (int i = 0; i < numberOfBytesRead; i++)
                    {
                        myCompleteMessage.AppendFormat("0x{0:X2} ", myReadBuffer[i]);
                    }
                }

                while (stream.DataAvailable);

                // Print out the received message to the console.
                Console.WriteLine("You received the following message : " + myCompleteMessage);
            }
            else
            {
                Console.WriteLine("Sorry.  You cannot read from this NetworkStream.");
            }
            Console.ReadLine();
            BC.Close();
        }
    }
}
}

在控制台上我得到:

name of the device and device type (as a test)
Pair request: 'ok'
authenticated: 'ok'
device name: shows the name fine
device connected: true
bluetooth client connected: false

编辑:我应该补充一点,我花了最近3天的时间来查看从Stackoverflow到32feet wiki和主页以及MSDN和其他人的个人项目页面的所有内容,但发现没有任何有用的东西。但正如我所说,我没有任何使用蓝牙的经验,所以我一直在尝试不同的代码,但我不确定我的意思到底是什么意思。正在寻找。

0 个答案:

没有答案