互相識別後,PC卻出現如下錯誤訊息....不知問題在哪??
2017年9月21日 星期四
Raspberry]Bluetooth pi 無法與PC 正確識別對方
網路上找到的解法
https://www.raspberrypi.org/forums/viewtopic.php?p=947185#p947185
2017年9月8日 星期五
[Raspberry]Bluetooth
照著抄寫,有一堆編譯錯誤,好像找不到,h檔似的,可是我都有include...不知哪邊有誤
照著如下進去,看似有點進展:
但是會有如下錯誤訊息:
好像沒找到lib似的..加入如下檔案
竟成功了....YA!!
不錯的資料
照著如下進去,看似有點進展:
但是會有如下錯誤訊息:
好像沒找到lib似的..加入如下檔案
竟成功了....YA!!
不錯的資料
樹莓派中 USB 藍牙卡的驅動與設置
http://ruten-proteus.blogspot.tw/2014/07/Bluetooth-Kit-tutorial-01.html
http://tekibrain.blogspot.tw/2013/05/raspberry-pi-usb-bluetooth-dongle.html
Bluetooth - Installing and Using Bluetooth on the Raspberry Pi
Bluetooth Commands
Raspberry Pi 安裝 BlueZ 支援藍牙 BLE 功能http://www.arthurtoday.com/2014/12/raspberry-pi-install-bluez-5-with-ble-support.html
Chapter 4. Bluetooth programming in C with BlueZhttps://people.csail.mit.edu/albert/bluez-intro/c404.html
關於藍牙裝置找尋(inquiry, scan)兩三事http://ops9.blogspot.tw/2013/09/inquiry-scan.html
https://thepihut.com/blogs/raspberry-pi-tutorials/17841464-bluetooth-installing-and-using-bluetooth-on-the-raspberry-pi
http://www.raspberry-projects.com/pi/pi-operating-systems/raspbian/bluetooth/bluetooth-commands
Chapter 4. Bluetooth programming in C with BlueZhttps://people.csail.mit.edu/albert/bluez-intro/c404.html
關於藍牙裝置找尋(inquiry, scan)兩三事http://ops9.blogspot.tw/2013/09/inquiry-scan.html
2017年9月1日 星期五
[C#]Bluetooth
照著網路文章打,竟通了....YA!!
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
BluetoothClient bc =new BluetoothClient();
BluetoothDeviceInfo[] arr = new BluetoothDeviceInfo[10];
//第一參數表明搜索的最大設備數
//第二參數表示是否搜尋已配對設備
//第三參數表示是否搜尋濟助的設備
//第四個表示是否搜尋未知設備
//第五個表示搜索範圍內可被發現的設備
arr =bc.DiscoverDevices(255, true, true, true,true);
bc.Close();
}
BluetoothRadio radio = null;
string sendFileName = null;
BluetoothAddress sendAddress = null;
ObexListener listener = null;
string recDir = null;
Thread listenThread, sendThread;
private void button6_Click(object sender, EventArgs e)
{
radio = BluetoothRadio.PrimaryRadio;//獲取當前PC的藍牙適配器
CheckForIllegalCrossThreadCalls = false;//不檢查跨執行緒
if(radio==null)
{
MessageBox.Show("Bluetooth can't use !", "RROR");
}
recDir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
richTextBox1.Text = recDir;
}
private void button7_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;
richTextBox1.Text = "Address:" + sendAddress.ToString() + "\r\n";
richTextBox1.Text = richTextBox1.Text + "Device Name:" + dialog.SelectedDevice.DeviceName+"\r\n";
}
}
private void button8_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
if(dialog.ShowDialog()==DialogResult.OK)
{
sendFileName = dialog.FileName;
richTextBox1.Text = Path.GetFileName(sendFileName);
sendThread = new Thread(sendFile);
sendThread.Start();
}
}
private void sendFile()
{
ObexWebRequest request = new ObexWebRequest(sendAddress, Path.GetFileName(sendFileName)); //創建網路需求
WebResponse response = null;
try
{
button8.Enabled = false;
richTextBox1.Text = "";
request.ReadFile(sendFileName);
richTextBox1.Text = "start transfer"+"\r\n";
response = request.GetResponse();
richTextBox1.Text = richTextBox1.Text + "transfer End";
}
catch (System.Exception ex)
{
MessageBox.Show("transfer Failed");
richTextBox1.Text = richTextBox1.Text + "transfer failed";
}
finally
{
if(response!=null)
{
response.Close();
button8.Enabled = true;
}
}
}
private void button9_Click(object sender, EventArgs e)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.Description = "選擇藍芽接收文件的存放路徑";
if(dialog.ShowDialog()==DialogResult.OK)
{
recDir = dialog.SelectedPath;
richTextBox1.Text = dialog.SelectedPath;
richTextBox1.Text = recDir;
if((listener==null) ||(!listener.IsListening))
{
radio.Mode = RadioMode.Discoverable;//設置本地藍芽可被檢測
listener = new ObexListener(ObexTransport.Bluetooth);
listener.Start();
if(listener.IsListening)
{
richTextBox1.Text = "Start listener";
listenThread = new Thread(receiveFile);
listenThread.Start();
}
}
else
{
listener.Stop();
richTextBox1.Text = "Stop Listener";
}
}
}
private void receiveFile()
{
ObexListenerContext context = null;
ObexListenerRequest request = null;
while(listener.IsListening)
{
context = listener.GetContext(); //獲取解析上下文
if(context==null)
{
break;
}
request = context.Request;//獲取請求
string uriString = Uri.UnescapeDataString(request.RawUrl);//將 uri轉換成字符號
string recFileName=recDir + uriString;
request.WriteFile(recFileName);
richTextBox1.Text = "收到文件" + uriString.TrimStart(new char[] { '/' });
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if(sendThread!=null)
{
sendThread.Abort();
}
if (listener != null && listener.IsListening)
{
listener.Stop();
}
if (listenThread!=null)
{
listenThread.Abort();
}
}
private void button5_Click(object sender, EventArgs e)
{
BluetoothRadio bluetoothRadio = BluetoothRadio.PrimaryRadio;
if(bluetoothRadio==null)
{
MessageBox.Show("沒找到本機藍芽裝置");
}
else
{
richTextBox1.Text = "Class of Device:" + bluetoothRadio.ClassOfDevice + "\r\n";
richTextBox1.Text = richTextBox1.Text + "Hardware Status:" + bluetoothRadio.HardwareStatus + "\r\n";
richTextBox1.Text = richTextBox1.Text + "HCiRevision:" + bluetoothRadio.HciRevision+"\r\n";
richTextBox1.Text = richTextBox1.Text + "HCiVersion:" + bluetoothRadio.HciVersion+"\r\n";
richTextBox1.Text = richTextBox1.Text + "LmpSubversion:" + bluetoothRadio.LmpSubversion + "\r\n";
richTextBox1.Text = richTextBox1.Text + "Local address:" + bluetoothRadio.LocalAddress + "\r\n";
richTextBox1.Text = richTextBox1.Text + "Manufacturer:" + bluetoothRadio.Manufacturer + "\r\n";
richTextBox1.Text = richTextBox1.Text + "Mode:" + bluetoothRadio.Mode+"\r\n";
richTextBox1.Text = richTextBox1.Text + "Name:" + bluetoothRadio.Name + "\r\n";
richTextBox1.Text = richTextBox1.Text + "Remote:" + bluetoothRadio.Name + "\r\n";
richTextBox1.Text = richTextBox1.Text + "SoftwareManfacturer:" + bluetoothRadio.SoftwareManufacturer + "\r\n";
richTextBox1.Text = richTextBox1.Text + "StackFactory" + bluetoothRadio.StackFactory + "\r\n";
}
}
}
兩台PC使用rfcomm通訊
using InTheHand.Net;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Sockets;
using System.IO;
namespace bluetooth_rfcomm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
BluetoothClient client = new BluetoothClient();
BluetoothDeviceInfo[] devices = client.DiscoverDevices();
BluetoothDeviceInfo device = null;
richTextBox1.Text = "";
foreach(BluetoothDeviceInfo d in devices)
{
richTextBox1.Text = richTextBox1.Text + " " + d.DeviceName + "\n";
if (d.DeviceName=="NB-13")
{
device = d;
break;
}
}
if(device!=null)
{
richTextBox1.Text = richTextBox1.Text + "connect start\n";
client.Connect(device.DeviceAddress, BluetoothService.SerialPort);
Stream perStream = client.GetStream();
byte[] buffer = new byte[200];
perStream.Read(buffer, 0, 40);
string data = System.Text.ASCIIEncoding.ASCII.GetString(buffer, 0, 40);
richTextBox1.Text = richTextBox1.Text + "DATA:"+data+"\n";
perStream.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
richTextBox1.Text ="\n";
BluetoothRadio myRadio = BluetoothRadio.PrimaryRadio;
if (myRadio == null)
{
richTextBox1.Text = "No radio HW or unsupported SW stack";
return;
}
richTextBox1.Text = richTextBox1.Text+"Radio,address:" + myRadio.LocalAddress.ToString() + "\n";
richTextBox1.Text = richTextBox1.Text + "Mode:" + myRadio.Mode.ToString() + "\n";
richTextBox1.Text = richTextBox1.Text + "Name: " + myRadio.Name + " LmpSubversion:" + myRadio.LmpSubversion + "\n";
richTextBox1.Text = richTextBox1.Text + "ClassofDevice:" + myRadio.ClassOfDevice.ToString() + " device:" + myRadio.ClassOfDevice.Device.ToString() + " Service:" + myRadio.ClassOfDevice.Service.ToString() + "\n";
myRadio.Mode = RadioMode.Discoverable;
richTextBox1.Text = richTextBox1.Text + "Radio Mode now:" + myRadio.Mode.ToString();
richTextBox1.Text = richTextBox1.Text + "\n";
Application.DoEvents();
BluetoothListener listener = new BluetoothListener(BluetoothService.SerialPort);
listener.Start();
richTextBox1.Text = richTextBox1.Text + "service startd:\n";
BluetoothClient client = listener.AcceptBluetoothClient();
richTextBox1.Text = richTextBox1 + "Got a request\n";
Stream peerStream=client.GetStream();
string dataToSend = "Hello from service";
byte[] dataBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(dataToSend);
peerStream.Write(dataBuffer, 0, dataBuffer.Length);
}
}
}
不錯的文章:
How to find Bluetooth MAC Address in Windows
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
BluetoothClient bc =new BluetoothClient();
BluetoothDeviceInfo[] arr = new BluetoothDeviceInfo[10];
//第一參數表明搜索的最大設備數
//第二參數表示是否搜尋已配對設備
//第三參數表示是否搜尋濟助的設備
//第四個表示是否搜尋未知設備
//第五個表示搜索範圍內可被發現的設備
arr =bc.DiscoverDevices(255, true, true, true,true);
bc.Close();
}
BluetoothRadio radio = null;
string sendFileName = null;
BluetoothAddress sendAddress = null;
ObexListener listener = null;
string recDir = null;
Thread listenThread, sendThread;
private void button6_Click(object sender, EventArgs e)
{
radio = BluetoothRadio.PrimaryRadio;//獲取當前PC的藍牙適配器
CheckForIllegalCrossThreadCalls = false;//不檢查跨執行緒
if(radio==null)
{
MessageBox.Show("Bluetooth can't use !", "RROR");
}
recDir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
richTextBox1.Text = recDir;
}
private void button7_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;
richTextBox1.Text = "Address:" + sendAddress.ToString() + "\r\n";
richTextBox1.Text = richTextBox1.Text + "Device Name:" + dialog.SelectedDevice.DeviceName+"\r\n";
}
}
private void button8_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
if(dialog.ShowDialog()==DialogResult.OK)
{
sendFileName = dialog.FileName;
richTextBox1.Text = Path.GetFileName(sendFileName);
sendThread = new Thread(sendFile);
sendThread.Start();
}
}
private void sendFile()
{
ObexWebRequest request = new ObexWebRequest(sendAddress, Path.GetFileName(sendFileName)); //創建網路需求
WebResponse response = null;
try
{
button8.Enabled = false;
richTextBox1.Text = "";
request.ReadFile(sendFileName);
richTextBox1.Text = "start transfer"+"\r\n";
response = request.GetResponse();
richTextBox1.Text = richTextBox1.Text + "transfer End";
}
catch (System.Exception ex)
{
MessageBox.Show("transfer Failed");
richTextBox1.Text = richTextBox1.Text + "transfer failed";
}
finally
{
if(response!=null)
{
response.Close();
button8.Enabled = true;
}
}
}
private void button9_Click(object sender, EventArgs e)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.Description = "選擇藍芽接收文件的存放路徑";
if(dialog.ShowDialog()==DialogResult.OK)
{
recDir = dialog.SelectedPath;
richTextBox1.Text = dialog.SelectedPath;
richTextBox1.Text = recDir;
if((listener==null) ||(!listener.IsListening))
{
radio.Mode = RadioMode.Discoverable;//設置本地藍芽可被檢測
listener = new ObexListener(ObexTransport.Bluetooth);
listener.Start();
if(listener.IsListening)
{
richTextBox1.Text = "Start listener";
listenThread = new Thread(receiveFile);
listenThread.Start();
}
}
else
{
listener.Stop();
richTextBox1.Text = "Stop Listener";
}
}
}
private void receiveFile()
{
ObexListenerContext context = null;
ObexListenerRequest request = null;
while(listener.IsListening)
{
context = listener.GetContext(); //獲取解析上下文
if(context==null)
{
break;
}
request = context.Request;//獲取請求
string uriString = Uri.UnescapeDataString(request.RawUrl);//將 uri轉換成字符號
string recFileName=recDir + uriString;
request.WriteFile(recFileName);
richTextBox1.Text = "收到文件" + uriString.TrimStart(new char[] { '/' });
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if(sendThread!=null)
{
sendThread.Abort();
}
if (listener != null && listener.IsListening)
{
listener.Stop();
}
if (listenThread!=null)
{
listenThread.Abort();
}
}
private void button5_Click(object sender, EventArgs e)
{
BluetoothRadio bluetoothRadio = BluetoothRadio.PrimaryRadio;
if(bluetoothRadio==null)
{
MessageBox.Show("沒找到本機藍芽裝置");
}
else
{
richTextBox1.Text = "Class of Device:" + bluetoothRadio.ClassOfDevice + "\r\n";
richTextBox1.Text = richTextBox1.Text + "Hardware Status:" + bluetoothRadio.HardwareStatus + "\r\n";
richTextBox1.Text = richTextBox1.Text + "HCiRevision:" + bluetoothRadio.HciRevision+"\r\n";
richTextBox1.Text = richTextBox1.Text + "HCiVersion:" + bluetoothRadio.HciVersion+"\r\n";
richTextBox1.Text = richTextBox1.Text + "LmpSubversion:" + bluetoothRadio.LmpSubversion + "\r\n";
richTextBox1.Text = richTextBox1.Text + "Local address:" + bluetoothRadio.LocalAddress + "\r\n";
richTextBox1.Text = richTextBox1.Text + "Manufacturer:" + bluetoothRadio.Manufacturer + "\r\n";
richTextBox1.Text = richTextBox1.Text + "Mode:" + bluetoothRadio.Mode+"\r\n";
richTextBox1.Text = richTextBox1.Text + "Name:" + bluetoothRadio.Name + "\r\n";
richTextBox1.Text = richTextBox1.Text + "Remote:" + bluetoothRadio.Name + "\r\n";
richTextBox1.Text = richTextBox1.Text + "SoftwareManfacturer:" + bluetoothRadio.SoftwareManufacturer + "\r\n";
richTextBox1.Text = richTextBox1.Text + "StackFactory" + bluetoothRadio.StackFactory + "\r\n";
}
}
}
兩台PC使用rfcomm通訊
using InTheHand.Net;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Sockets;
using System.IO;
namespace bluetooth_rfcomm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
BluetoothClient client = new BluetoothClient();
BluetoothDeviceInfo[] devices = client.DiscoverDevices();
BluetoothDeviceInfo device = null;
richTextBox1.Text = "";
foreach(BluetoothDeviceInfo d in devices)
{
richTextBox1.Text = richTextBox1.Text + " " + d.DeviceName + "\n";
if (d.DeviceName=="NB-13")
{
device = d;
break;
}
}
if(device!=null)
{
richTextBox1.Text = richTextBox1.Text + "connect start\n";
client.Connect(device.DeviceAddress, BluetoothService.SerialPort);
Stream perStream = client.GetStream();
byte[] buffer = new byte[200];
perStream.Read(buffer, 0, 40);
string data = System.Text.ASCIIEncoding.ASCII.GetString(buffer, 0, 40);
richTextBox1.Text = richTextBox1.Text + "DATA:"+data+"\n";
perStream.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
richTextBox1.Text ="\n";
BluetoothRadio myRadio = BluetoothRadio.PrimaryRadio;
if (myRadio == null)
{
richTextBox1.Text = "No radio HW or unsupported SW stack";
return;
}
richTextBox1.Text = richTextBox1.Text+"Radio,address:" + myRadio.LocalAddress.ToString() + "\n";
richTextBox1.Text = richTextBox1.Text + "Mode:" + myRadio.Mode.ToString() + "\n";
richTextBox1.Text = richTextBox1.Text + "Name: " + myRadio.Name + " LmpSubversion:" + myRadio.LmpSubversion + "\n";
richTextBox1.Text = richTextBox1.Text + "ClassofDevice:" + myRadio.ClassOfDevice.ToString() + " device:" + myRadio.ClassOfDevice.Device.ToString() + " Service:" + myRadio.ClassOfDevice.Service.ToString() + "\n";
myRadio.Mode = RadioMode.Discoverable;
richTextBox1.Text = richTextBox1.Text + "Radio Mode now:" + myRadio.Mode.ToString();
richTextBox1.Text = richTextBox1.Text + "\n";
Application.DoEvents();
BluetoothListener listener = new BluetoothListener(BluetoothService.SerialPort);
listener.Start();
richTextBox1.Text = richTextBox1.Text + "service startd:\n";
BluetoothClient client = listener.AcceptBluetoothClient();
richTextBox1.Text = richTextBox1 + "Got a request\n";
Stream peerStream=client.GetStream();
string dataToSend = "Hello from service";
byte[] dataBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(dataToSend);
peerStream.Write(dataBuffer, 0, dataBuffer.Length);
}
}
}
不錯的文章:
How to find Bluetooth MAC Address in Windows
https://macaddresschanger.com/how-to-find-bluetooth-mac-address-windows
連接方式介紹
http://32feet.codeplex.com/documentation
32feet.net 蓝牙虚拟串口编程http://32feet.codeplex.com/documentation
https://tisyang.github.io/2016/05/bluetooth-serialport-with-32feet-net-library/
Windows Moible, Wince 使用.NET Compact Framework进行蓝牙(Bluetooth)开发 之 32feet.NET
http://www.cnblogs.com/procoder/archive/2009/05/14/1456243.html
C#编程连接蓝牙设备,文件收发
http://hzy3774.iteye.com/blog/1735163
C#编程连接蓝牙设备,文件收发
http://hzy3774.iteye.com/blog/1735163
訂閱:
文章 (Atom)