I am working on a tool that connected to my Bluetooth receiver and scan and get MAC address of BLE devices. So far I can scan and have the mac address continuously display in textbox. I am having problem with filtering MAC address?(Our assigned MAC address 88 99 66 55 4X XX the last 12 bit is free to use). How can I filter this while scanning and put it in Listbox or Textbox for later use.
Any suggestion or help will be appreciated.
Any suggestion or help will be appreciated.
C#:
using System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.Text.RegularExpressions;
using System.Threading;
namespace Serial_Events
{
public partial class Form1 : Form
{
//MessageSend comm = new MessageSend();
string transType = string.Empty;
public SerialPort serialport;
private bool buttonWasClicked = false;
public Form1()
{
InitializeComponent();
foreach (string s in SerialPort.GetPortNames())
{
cmPort.Items.Add(s);
}
}
public void serialport_connect(String port, int baudrate)
{
DateTime dt = DateTime.Now;
String dtn = dt.ToShortTimeString();
serialport = new SerialPort(port, baudrate);
try
{
serialport.Open();
textBox1.AppendText(" OPEN " + "[" + dtn + "] " + " Port Opened: Connected\n");
// Event Handler
serialport.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Error");
}
}
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
DateTime dt = DateTime.Now;
String dtn = dt.ToShortTimeString();
//TextBox.CheckForIllegalCrossThreadCalls = false;
if (buttonWasClicked == true)
{
ThreadSafeDelegate(delegate { ScanList.Items.Add(serialport.ReadExisting()); });
//ThreadSafeDelegate(delegate { textBox3.Text += "\r\n"; });
}
else
{
ThreadSafeDelegate(delegate { textBox1.AppendText(serialport.ReadExisting()); });
ThreadSafeDelegate(delegate { textBox1.Text += "\r\n"; });
//textBox1.AppendText(serialport.ReadExisting());
//textBox1.Text += "\r\n";
}
}
private void button1_Click(object sender, EventArgs e)
{
byte[] msg = ASCIIEncoding.ASCII.GetBytes(textBox2.Text + "\0");
serialport.Write(msg, 0, msg.Length);
}
private void button2_Click(object sender, EventArgs e)
{
String port = cmPort.Text;
int baudrate = Convert.ToInt32(cmBaud.Text);
//Parity parity = (Parity)Enum.Parse(typeof(Parity), cmbparity.Text);
//int databits = Convert.ToInt32(cmbdatabits.Text);
//StopBits stopbits = (StopBits)Enum.Parse(typeof(StopBits), cmbstopbits.Text);
button3.Enabled = true;
serialport_connect(port, baudrate);
}
private void button3_Click(object sender, EventArgs e)
{
DateTime dt = DateTime.Now;
String dtn = dt.ToShortTimeString();
if (serialport.IsOpen)
{
serialport.Close();
button3.Enabled = false;
button2.Enabled = true;
textBox1.AppendText("[" + dtn + "] " + "Port Close: Disconnected\n");
}
}
private void btnClear_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
private void ThreadSafeDelegate(MethodInvoker method)
{
if (InvokeRequired)
BeginInvoke(method);
else
method.Invoke();
}
private void Scan_Click(object sender, EventArgs e)
{
buttonWasClicked = true;
byte[] msg = ASCIIEncoding.ASCII.GetBytes( "SNS10" + "\0");
serialport.Write(msg, 0, msg.Length);
}
}
}