android - 使用 Xamarin 创建 Android 蓝牙终端连接到 HC-06 模块

标签 android bluetooth terminal xamarin

我正在尝试构建一个非常简单的应用程序,它允许我与来自 Android 的 HC-06 芯片(简单的 UART 接口(interface))进行通信,该应用程序与 BlueTerm 或目前在 Play 商店中类似的东西非常相似。 Xamarin 给出的标准蓝牙示例可以正常工作,但无法连接到蓝牙模块(简单地说无法连接到设备)。如果有人对它为什么会这样做和/或如何解决它有任何想法,我们将不胜感激。

最佳答案

HC05masterslave 模式下工作。 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/

相关文章:

c++ - opencv ubuntu 12.04 Cmake错误: Source directory does not appear to contain CmakeLists. txt

android - 将 SQLite 数据库加载到 Android 应用程序中(超过 1MB)

Android 的 ViewModel 和 MVVM

android - 即使我卸载应用程序,进程 p = Runtime.getRuntime().exec() 也在运行

android - 如何设置蓝牙打印的字体大小?

macos - 从 C/C++ 程序采样 Mac OS X 上的进程

linux - 是否可以在一个命令行中将 SCP 远程服务器到远程服务器到本地?

android - SMS Retriever API android系统无法向广播接收器发送消息

android - Android2.2应用启动时如何获取蓝牙耳机连接状态?

android - 从 Android 中的驱动程序获取 HCI 设备列表的问题