- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在制作一个 Android Studio 应用程序来通过蓝牙控制我的 Arduino 项目。我已成功将 HC-06 模块连接到具有以下类的应用程序:
class ConnectBT extends AsyncTask<Void, Void, Void>{
private boolean ConnectSuccess = true;
@Override
protected void onPreExecute() {
}
@Override
protected Void doInBackground(Void... devices)
{
try
{
if (Drive.btSocket == null || !Drive.isBtConnected)
{
Drive.myBluetooth = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice dispositivo = Drive.myBluetooth.getRemoteDevice(Drive.BTaddress);
Drive.btSocket = dispositivo.createInsecureRfcommSocketToServiceRecord(Drive.myUUID);
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
Drive.btSocket.connect();
}
}
catch (IOException e)
{
ConnectSuccess = false;exception here
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
if (!ConnectSuccess)
{
msg("Connection Failed. Is it a SPP Bluetooth? Try again.");
}
else
{
msg("Connected.");
Drive.loadingScreen.setVisibility(View.GONE);
System.out.println("Connected");
Drive.isBtConnected = true;
}
//Drive.progress.dismiss();
}
private void msg(String s)
{
//Toast.makeText(Drive.class.,s,Toast.LENGTH_LONG).show();
}
}
但是现在,当我尝试从应用程序发送和接收数据时,事情变得很困难,所以我陷入了困境。我在谷歌上搜索了如何发送和接收数据,但我主要找到了有关如何从头开始制作 BT 应用程序的教程。如何在 Android 应用程序中通过蓝牙从 HC-06 发送和接收数据?有代码 fragment 吗?
最佳答案
用以下代码 fragment 解决了这个问题:
void sendData(String data){
if (isBtConnected){
try {
Drive.btSocket.getOutputStream().write((data).getBytes());
} catch (IOException e) {
e.printStackTrace();
msg("Failed to send Bluetooth data");
}
}
}
关于java - Android 通过蓝牙从 HC-06 发送/接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57039243/
我需要有关 HC-06 蓝牙模块闪烁 LED 的帮助。 我只想知道它什么时候开始闪烁,什么时候一直亮着,什么时候完全不亮,我怎么知道它是通过 LED 连接还是配对。 我的第二个问题是,我正在开发一个
当检测到距离小于1m的物体时,我试图通过仅打开LED来控制HC-SR4超声波传感器。 我将TIM2用于触发信号(引脚PB10),并将TIM4用于接收回波信号(引脚PB6)。 LED连接到PB7引脚。
我最近为我的 arduino 购买了一个 HC-05 蓝牙模块,但我无法从中发送或接收数据。我使用代码来打开或关闭 LED,但是当我从 PC 的串行监视器发送一个字符后,我得到 ⸮。并且模块不响应任何
我想开发Java桌面应用程序,用蓝牙“HC-05”连接我的电脑。我在大学里学过Java,这是我的第一个应用程序。所以我希望尽可能提供更多详细信息,特别是在建立 PC 和 HC-05 之间的连接方面(如
我想将数据从 Arduino 传输到 android 手机。数据将是整数,它们是从连接到 Arduino 的传感器连续生成的值。最快的方法是什么?我的意思是,没有任何延迟地传输此数据的最佳代码是什么?
我有 raspberry pi b,我正在尝试用 C 编写代码,这将允许我使用 HC-SR04 Ultrasonic Ranging Module .我使用 bcm2835 库来控制 GPIO 引脚。
我正在尝试使用 C# 应用从我的个人最爱 Media Player Classic 中获取媒体信息。 假设我得到了我感兴趣的 MPC-HC 实例的窗口句柄,但 WM_GETTEXT 只得到了窗口标题。
我有一个带有 AlphaBot2 的树莓派 Zero,它具有 HC-SR04 超声波传感器。使用Python 的实现效果很好。我想用 C 语言实现,因为我需要将它与另一个同样用 C 语言编写的程序绑定
我正在使用 HC-Sticky JavaScript 插件并尝试使用 documented reinit 方法,但我不明白如何运行它。 这是一个CodePen显示了一个非常基本的设置,并尝试在初始化后
我在我的网站上安装了 hc-sticky 插件,我可以使用此代码使其适用于滚动侧边栏: jQuery(document).ready(function($){ $('.panel-r
我正在尝试让 NodeMCU ESP8266 开发板与 HC SR04 超声波传感器配合使用。我的读数一直为“0”,但事实并非如此。 我将传感器的接地和 5v 引脚连接到 5v 源,以及 GPIO 引
目前将 Java 应用程序连接到 HC-05 蓝牙模块 (Arduino) 的最佳实践是什么?Bluecove 还是其他?如果是这样,您是否可以为带有 HC-05 连接的 Bluecove 推荐任何示
您好,我正在关注 the quick introduction to racket .在第 5 章中,他们建议这段代码(运行良好): (define (four p) (define two-p
你好, 所以我买了一个ZS-040 HC-05 Arduino蓝牙模块,我想更改它的名称。我了解到,您必须在 AT 模式下执行此操作。我遵循了 http://www.martyncurrey.com/
您好,我正在关注 the quick introduction to racket .在第 5 章中,他们建议这段代码(运行良好): (define (four p) (define two-p
我知道 SD 卡中的写保护选项卡并不是真正的硬件保护,而是向读卡器发出的一个简单信号,表明该卡已被写保护。例如,佳能相机的 CHDK 项目就专门忽略了它,曾经有一个廉价的闪存阅读器(从香港邮寄过来的
有人可以向我解释以下代码有什么问题吗?我尝试了所有方法:添加 limit \n\r 以等待行尾和其他几种方式。我总是收到垃圾数据。所以我回到我的开始代码。 有人告诉我可能是InputStream没有接
所以我现在面临着一个问题。任何建议都会很好。首先,我使用我的代码从 arduino 接收数据,然后我使用 bluetoothChat 并更改了 uuid,我可以配对,一切都很好,但是如果我将整个字符串
我正在尝试使用根据与超声波传感器的距离计算出的平均值来控制 LED。我有数据被平均,但它从加电开始是连续的。我想在每十次读数后重新计算平均值。谁能告诉我需要更改哪些内容才能重新计算每 10 个值的平均
我正在尝试在 Arduino 和 Android 设备之间建立连接。我正在使用: 阿杜诺·莱昂纳多 蓝牙设备:HC-06 我的 Android 应用程序应该读取从 Arduino 设备发送的数据。这是
我是一名优秀的程序员,十分优秀!