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("服务:"