栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

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

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 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("", "");

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/905773.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号