C#蓝牙基础通讯示例

MianForm.cs

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using BlueCom;
using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.Advertisement;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
using Windows.Foundation;
using Windows.Security.Cryptography;
using Windows.Storage.Streams;

namespace BleTest
{
   
    public partial class MianForm : Form
    {
   
        private BluetoothLEAdvertisementWatcher Watcher = null;

        private BluetoothLEDevice CurrentDevice = null;

        private int Character_index = 0;
        public List<GattCharacteristic> CurrentCharacteristicList = new List<GattCharacteristic>();

        private const GattClientCharacteristicConfigurationDescriptorValue CHARACTERISTIC_NOTIFICATION_TYPE = GattClientCharacteristicConfigurationDescriptorValue.Notify;

        Dictionary<ulong, BluetoothLEAdvertisement> adv_datas = new Dictionary<ulong, BluetoothLEAdvertisement>(); // 
        Dictionary<ulong, BluetoothLEAdvertisement> res_datas = new Dictionary<ulong, BluetoothLEAdvertisement>(); // Scan Respones

        int listview_index = 0;
        Dictionary<ulong, int> listview_indexs = new Dictionary<ulong, int>();


        public byte[] HexStringToByteArray(string s)
        {
   
            s = s.Replace(" ", "");
            byte[] buffer = new byte[s.Length / 2];
            for (int i = 0; i < s.Length; i += 2)
            {
   
                buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
            }
            return buffer;
        }

        public MianForm()
        {
   
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }


        private void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
        {
   
            Int16 rssi = eventArgs.RawSignalStrengthInDBm;
            ulong address = eventArgs.BluetoothAddress;
            String name = eventArgs.Advertisement.LocalName.ToString();
            string addr_str = BitConverter.ToString(BitConverter.GetBytes(address), 0, 6).Replace('-', ':').ToLower();

            var serviceUuids = eventArgs.Advertisement.ServiceUuids;

           
            bool isScanResponse = string.IsNullOrEmpty(name);
            if (isScanResponse)
            {
   
                if (res_datas.ContainsKey(address)) res_datas.Remove(address);
                res_datas.Add(address, eventArgs.Advertisement);
            }
            else
            {
   
                if (adv_datas.ContainsKey(address)) adv_datas.Remove(address);
                adv_datas.Add(address, eventArgs.Advertisement);
            }

            if (None_Name_checkBox.Checked)
            {
   
                if (name.Length == 0) {
    return; }
                if (name.Contains(Name_Filter_textBox.Text) == false) {
    return; }
            }

            if (listview_indexs.TryGetValue(address, out int i))
            {
   
                if (BLE_Device_listView.Items.Count == 0) return;
                BLE_Device_listView.Items[i].SubItems[1].Text = rssi.ToString();
                if (name.Length > 0) BLE_Device_listView.Items[i].SubItems[3].Text = name;
                return;
            }

            bool isConnectable = serviceUuids != null && serviceUuids.Count > 0;

            ListViewItem item = new ListViewItem();
            item.Text = addr_str;
            item.Tag = address;
            item.SubItems.Add(rssi.ToString());
            item.SubItems.Add(isConnectable ? "是":"否");
            item.SubItems.Add(name);
            BLE_Device_listView.Items.Add(item);

            listview_indexs.Add(address, listview_index);
            listview_index++;


        }

        public delegate void Gatt_Tree_Add_Server_delegate(string s);
        public void Gatt_Tree_Add_Server(string s)
        {
   
            TreeNode newNode2 = new TreeNode("服务:" 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值