C#使用InTheHand.Net.Bluetooth连接蓝牙,接收数据

最近有个工作需要在UWP项目里连接蓝牙,网上搜了好多都是几年前用的InTheHand.Net.Personal.dll,踩了好多坑。而且这个东西现在已经没有了,现在VS里有InTheHand.Net.Bluetooth的NuGet包,可以导入直接使用。一个简单的蓝牙搞了好久,在这做个记录。

using System;
using InTheHand.Net.Sockets;
using InTheHand;
using InTheHand.Net.Bluetooth;
using System.Threading;
using System.IO;
using System.Text;
using System.Collections.Generic;

namespace BluetoothTest_2
{
    class Program
    {
        ~Program()
        {
            bluetoothClient.Close();
        }

        static BluetoothClient bluetoothClient;
        //static BluetoothListener bluetoothListener;
        //static Thread listenThread;
        static bool isConnected;
        static void Main(string[] args)
        {
            getDevice();

            //listenThread = new Thread(ReceiveData);
            //listenThread.IsBackground = true;

            //listenThread.Start();
            Console.ReadKey();
        }

        //BluetoothService

        public static bool getDevice()
        {
            Console.WriteLine("Finding Devides...");
            //BluetoothRadio.PrimaryRadio.Mode = InTheHand.Net.Bluetooth.RadioMode.Connectable;
            BluetoothClient cli = new BluetoothClient();
            IReadOnlyCollection<BluetoothDeviceInfo> devices = cli.DiscoverDevices();
            foreach (BluetoothDeviceInfo device in devices) //设备搜寻           
            {
                //device.Update();
                device.Refresh();
                if (device.DeviceName == "mark_1")
                {
                    Console.WriteLine("Connecting...");
                    try
                    {
                        if (!device.Connected)
                        {
                            cli.Connect(device.DeviceAddress, BluetoothService.SerialPort);
                            Console.WriteLine("Connected!");

                            bluetoothClient = cli;
                            ReceiveData();
                        }
                        else
                            Console.WriteLine("Has Been Connected!");
                    }
                    catch(Exception e) 
                    { Console.WriteLine("Failed:"+e); }
                    
                    break;
                }
            }

            //Thread ReceiveThread = new Thread(ReceiveData);
            //ReceiveThread.Start();
            return true;
        }
      
        private static void ReceiveData()
        {
            isConnected = bluetoothClient.Connected;
            while (isConnected)
            {
                try
                {
                    string receive = string.Empty;

                    Stream peerStream = bluetoothClient.GetStream();
                    byte[] buffer = new byte[255];
                    peerStream.Read(buffer, 0, 255);
                    receive = Encoding.UTF8.GetString(buffer).ToString().Replace("\0", "");

                    //Console.ReadKey();
                    Console.Write(receive);
                }
                catch(Exception e)
                {
                    Console.Write("Error:" + e);
                    break;
                }
            }
        }

  • 4
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值