2011-09-21 94 views
2

我一直与Monodroid工作了几天,仍然无法弄清楚如何通过蓝牙发送命令。MonoDroid蓝牙

这是我的情况:我有一台使用Android 2.1+的平板电脑/手机,需要向蓝牙打印机发送和接收数据(以字节为单位)。

我管理至今:

using Android.Bluetooth; // library necessary 

BluetoothAdapter bth = BluetoothAdapter.DefaultAdapter; 
if (!bth.IsEnabled) 
    bth.Enable(); 

ICollection<BluetoothDevice> bthD = bth.BondedDevices; 

foreach (BluetoothDevice d in bthD) 
{ 
    if (d.Name == "DPP-350") 
    { 
     Java.Util.UUID UUID = Java.Util.UUID.FromString("00001101-0000-1000-8000-00805F9B34FB"); 
     // Get the BLuetoothDevice object 
     BluetoothSocket s = d.CreateRfcommSocketToServiceRecord(UUID); 

     s.Connect(); 

     // Try to send command 
     ... 

     s.Close() 
    } 
} 

的程序要求配对信息,以正确地完成。 我曾尝试过很多方式发送命令:

// the command 
// Self_Test = Chr(27) + Chr(84) = ESC T 
byte[] dBytes = System.Text.Encoding.GetEncoding(1252).GetBytes(Self_Test); 

// wont work 
new Java.IO.ObjectOutputStream(s.OutputStream).Write(dBytes); 
// wont work 
System.IO.Stream st = s.OutputStream; 
if (st.CanWrite) 
{ 
    st.Write(dBytes, 0, dBytes.Length); 
    st.Flush(); 
} 
// wonk work 
s.OutputStream.Write(dBytes, 0, dBytes.Length); 
s.OutputStream.Flush(); 

没有错误发生。我在这里用尽了...

在此先感谢!

+0

有什么办法与“调试”蓝牙设备配对以查看数据是否正在发送?无论知道错误是什么,它发生在哪里,它都很难修复... – jonp

+0

不,没有。但是同样的程序是在WM 6.x中编译并工作的。 主要区别在于,我有一个.dll沟通,并在单声道,不会工作。 我的问题是发送命令,并看到与打印机发生的事情,而不使用.dll – Gh0stman

回答

4

我知道这是一个非常古老的线程,但我想发布回复,以便其他人知道答案。我也努力搜索,没有运气。

s.OutputStream.BeginWrite(buffer, 0, buffer.Length,new AsyncCallback(delegate {}), State.Connected); 

谢谢。