我正在尝试构建一个非常简单的应用程序,它允许我与来自 Android 的 HC-06 芯片(简单的 UART 接口(interface))进行通信,该应用程序与 BlueTerm 或目前在 Play 商店中类似的东西非常相似。 Xamarin 给出的标准蓝牙示例可以正常工作,但无法连接到蓝牙模块(简单地说无法连接到设备)。如果有人对它为什么会这样做和/或如何解决它有任何想法,我们将不胜感激。
最佳答案
HC05
在master
和slave
模式下工作。 HC05
默认为从属模式,如 hc06。
我 3 年前在 Xamarin 博客中找到了这个链接:https://blog.xamarin.com/hello-mr-bond_agent_watch/
此页面介绍了一些使用微 Controller 创建的智能 watch ,但您可以下载一个名为 NetduinoBT_AgentProto 的 android 项目,它完全包含连接到蓝牙 SPP(串行端口配置文件)所需的所有内容。
将 App.cs
类添加到您的项目中。
和这个权限:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
获取设备列表和名称:
var list = App.Current.BondedBluetoothDeviceNames;
var name = App.Current.BondedBluetoothDeviceNames[yourIndex];`
对于连接到设备: bool connected = App.Current.ConnectToBluetoothDevice(name , out message);
您可以建立“MessageReceived”事件以获取您的设备响应:
string mYourResult;
...
protected override void OnCreate (Bundle bundle)
{
...
App.Current.MessageReceived += (object sender, App.MessageReceivedEventArgs e) => {
RunOnUiThread( () => {
mYourResult = e.MessageString;
});
};
...
}
用于发送字符串:
App.Current.SendStringToBluetoothDevice("你的字符串");
根据我在某些基于 Linux 核心的系统中的经验,您需要在字符串末尾添加下一行。例如:“\r\n”或“\n”或“\r”或 Environment.NewLine
关于android - 使用 Xamarin 创建 Android 蓝牙终端连接到 HC-06 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25230456/