Blutooth соединение не начнется я думаю
Я сделал учебник по youtube https://www.youtube.com/watch?v=9LG9Kfythu4[^]
но у меня не было подключения к моему устройству на другой стороне. кто-нибудь может мне помочь? что я делаю не так?
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.Threading; using System.Windows; using InTheHand; using InTheHand.Net.Bluetooth; using InTheHand.Net.Ports; using InTheHand.Net.Sockets; using System.IO; using InTheHand.Net; namespace HL7000_Prüf { public partial class Form1 : Form { List<string> items; public Form1() { items = new List<string>(); InitializeComponent(); } private void bGo_Click(object sender, EventArgs e) { if (serverStarted) { updateUI("server has started"); return; } if (rbClient.Checked) { startScan(); } else { connectAsServer(); } } private void startScan() { listBox1.DataSource = null; listBox1.Items.Clear(); items.Clear(); Thread bluetoothScanThread = new Thread(new ThreadStart(scan)); bluetoothScanThread.Start(); } BluetoothDeviceInfo[] devices; private void scan() { updateUI("Starting Scan.."); BluetoothClient client = new BluetoothClient(); devices = client.DiscoverDevicesInRange(); // devices = client.DiscoverDevicesInRange(); updateUI("Scan complete"); updateUI(devices.Length.ToString() + " devices discovered"); foreach (BluetoothDeviceInfo d in devices) { string desc = d.DeviceName + ": " + d.DeviceAddress; items.Add(desc); } updateDeviceList(); } private void connectAsServer() { Thread bluetoothServerThread = new Thread(new ThreadStart(ServerConnectThread)); bluetoothServerThread.Start(); } private void connectAsClient() { throw new NotImplementedException(); } Guid mUUID = new Guid("00011111-0000-1000-8000-00805F9B34FB"); // Guid mUUID = new Guid("00001101-0000-1000-8000-00805f9b34fb"); // Guid mUUID = new Guid("e0cbf06c-cd8b-4647-bb8a-263b43f0f974"); // Guid mUUID = System.Guid.NewGuid(); bool serverStarted = false; public void ServerConnectThread() { serverStarted = true; updateUI("Server started, waiting for clients"); BluetoothListener blueListener = new BluetoothListener(mUUID); blueListener.Start(); BluetoothClient conn = blueListener.AcceptBluetoothClient(); MessageBox.Show(blueListener.Authenticate.ToString()); updateUI("Client has Connected"); Stream mStream = conn.GetStream(); while (true) { try { //handle sever connection byte[] received = new byte[1024]; mStream.Read(received, 0, received.Length); updateUI("received: " + Encoding.ASCII.GetString(received)); byte[] sent = Encoding.ASCII.GetBytes("hello World"); mStream.Write(sent, 0, sent.Length); } catch (IOException exeption) { updateUI("Client has disconnected"); } } } private void updateUI(string message) { Func<int> del = delegate() { tbOutput.AppendText(message + System.Environment.NewLine); return 0; }; Invoke(del); } private void updateDeviceList() { Func<int> del = delegate() { listBox1.DataSource = items; return 0; }; Invoke(del); } BluetoothDeviceInfo deviceInfo; private void listBox1_DoubleClick(object sender, EventArgs e) { deviceInfo = devices.ElementAt(listBox1.SelectedIndex); updateUI(deviceInfo.DeviceName + " was selected"); if (pairDevice()) { updateUI("device paired.."); updateUI("starting connect thread"); Thread bluetoothClientThread = new Thread(new ThreadStart(ClientConnectThread)); bluetoothClientThread.Start(); } else { updateUI("Pair failed"); } } private void ClientConnectThread() { BluetoothClient client = new BluetoothClient(); updateUI("attempting connect"); client.BeginConnect(deviceInfo.DeviceAddress, mUUID, this.BluetoothClientConnectCallback, client); if (client.Connected) { updateUI("Verbindung geht"); } else { updateUI("nicht"); } } void BluetoothClientConnectCallback(IAsyncResult result) { BluetoothClient client = (BluetoothClient)result.AsyncState; client.EndConnect(result); Stream stream = client.GetStream(); stream.ReadTimeout = 1000; while (true) { while (!ready) ; stream.Write(message, 0, message.Length); } } string myPin = "1234"; private bool pairDevice() { if (!deviceInfo.Authenticated) { if (!BluetoothSecurity.PairRequest(deviceInfo.DeviceAddress, myPin)) { return false; } } return true; } bool ready = false; byte[] message; private void tbText_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 13) { message = Encoding.ASCII.GetBytes(tbText.Text); ready = true; tbText.Clear(); } } } }
Я писал на C#. Guid, который я установил в комментарии, я пробовал, но я думаю, что что-то не так, потому что messagebox после
BluetoothClient conn = blueListener.AcceptBluetoothClient();не будет показано.
спасибо за вашу помощь
Что я уже пробовал:
другой Guid, соединение с сервером будет неудачным.