Programmatically Get the Incoming/Outgoing COM Bluetooth Port.How can do it in Windows Mobile 6 App in C#?

眉间皱痕 提交于 2019-12-10 11:36:12

问题


I want to Print from windows mobile App through Bluetooth printer.What i am doing is that hard coding the COM port as below...

SerialPort comPort1 = new SerialPort("COM5", 57600, Parity.None, 8, StopBits.One);
comPort1.Open();

//Some thing to pint
//comPort1.WriteLine("PRINT");

System.Threading.Thread.Sleep(500);
comPort1.Close();

Its Not Proper Way,I want to search the application available bluetooth printer and connect with it.otherwise show a proper error message ["Bluetooth device not found/Selected Device is not a printer"]. please help me to found this, Anyone can direct me to do this?


回答1:


You would have to write some code that can go through each port number on the device, testing all of the various settings to see if a port exists. Even then, I don't know of a good way to guarantee that the port is a Bluetooth printer.

Have you ever noticed that Microsoft displays a form for the User to specify the settings with?

Just saying, you might find it easier to present your people with a custom dialog box.




回答2:


Using a Bluetooth API might be more what you want.

See for instance my library 32feet.NET. Lots of people use it for printing. You can discover devices like this:

var cli = new BluetoothClient();
BluetoothDeviceInfo[] devList = cli.DiscoverDevices();
BluetoothDeviceInfo[] selected = null;
foreach (var cur in devList) {
   ... chose one and set 'selected'
}
return selected;

Discovery

You can connect to a printer like:

BluetoothAddress addr = ... e.g. selected.DeviceAddress;
var cli = new BluetoothClient();
cli.Connect(addr, BluetoothService.SerialPort);
using(var peer = cli.GetStream())
using(var wtr = new StreamWriter(peer)) {
   wtr.WriteLine("Hello world");
}

General Bluetooth Data Connections



来源:https://stackoverflow.com/questions/7911874/programmatically-get-the-incoming-outgoing-com-bluetooth-port-how-can-do-it-in-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!