C#蓝牙开发问题:由于目标主机坏了,套接字操作失败
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using InTheHand.Windows.Forms;
using InTheHand.Net;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Sockets;
using System.Threading;
using System.Runtime.InteropServices;
using System.Net;
using System.IO;
namespace bluetooth
{
public partial class Form1 : Form
{
BluetoothRadio radio = null;//蓝牙适配器
BluetoothAddress sendAddress = null;//发送目的地址
string recDir = null;//接受文件存放目录
//ObexListener listener = null;//监听器
Boolean connection = false;
BluetoothClient bluetoothClient;
BluetoothListener bluetoothListener;
// Thread listenThread;
bool isConnected;
// NetworkStream peerStream;
public Form1()
{
InitializeComponent();
radio = BluetoothRadio.PrimaryRadio;//获取当前PC的蓝牙适配器
CheckForIllegalCrossThreadCalls = false;//不检查跨线程调用
if (radio == null)//检查该电脑蓝牙是否可用
{
MessageBox.Show("这个电脑蓝牙不可用!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
recDir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
}
private void button1_Click(object sender, EventArgs e)
{
SelectBluetoothDeviceDialog dialog = new SelectBluetoothDeviceDialog();
dialog.ShowRemembered = true;
dialog.ShowAuthenticated = true;
dialog.ShowUnknown = true;
if (dialog.ShowDialog() == DialogResult.OK)
{
sendAddress = dialog.SelectedDevice.DeviceAddress;//获取选择的远程蓝牙地址
label1.Text = "地址:" + sendAddress.ToString() + "设备名:" + dialog.SelectedDevice.DeviceName;
bluetoothClient = new BluetoothClient();
bluetoothClient.Connect(sendAddress, BluetoothService.PhonebookAccess);//问题在这里
connection = true;
// Thread ReceiveThread = new Thread(ReceiveData);
//ReceiveThread.Start();
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (connection) { bluetoothClient.Close(); }//; bluetoothListener.Stop(); }
}
}
}

...全文
相关推荐
C#开发android软件 蓝牙打印
用C#语言 xamarin.android 开发的 蓝牙打印功能,用vs2017开发的源码,还包括引用java jar 包
深度剖析:C# 如何实现蓝牙低功耗(BLE)通信全流程开发
🔍 C# BLE开发指南:从原理到实战 本文深入讲解C#开发BLE应用的核心要点,涵盖: BLE与经典蓝牙区别:重点对比通信模型(GATT协议vs串口)、功耗和应用场景 C#开发方案:推荐Windows.Devices.Bluetooth和InTheHand.BluetoothLE两种技术路径 GATT通信原理:详解Peripheral/Central角色、服务/特征值/描述符三级结构 实战代码:提供设备扫描、服务发现、特征值读写和通知订阅的完整示例 常见问题:总结设备连接失败、服务获取异常等典型问题的解
传统蓝牙开发实战:HC-05/HC-08模块SPP串口通信全解析
先做好AT指令配置(波特率、名称、PIN码),这是通信的基础;保证串口参数和模块一致,避免出现“能连不能通”;重视异常处理(超时、断连),尤其是工业场景,需要加入重连机制;按场景选型模块(HC-05性价比,HC-08双模低功耗)。虽然BLE蓝牙是主流,但HC-05/HC-08等SPP模块在串口设备无线化场景中仍不可替代——掌握这套开发流程,能快速解决低成本无线通信需求。
C#代码连接蓝牙设备
c#代码编写连接 PC 蓝牙连接到苹果手机的蓝牙,测试连接可行,网上很多代码,很混乱,不好连接!
c#_蓝牙开发
c#_蓝牙开发,内涵开发思路和开发说明,支持C#


