想要编写应用程序以连接蓝牙设备。

huangapple go评论78阅读模式
英文:

Want to program app to connect to bluetooth device

问题

希望有人能够指导我正确的方向。

我正在尝试编写一个简单的应用程序,通过蓝牙连接到一台火警设备。总体目标是,当设备上的火警被触发时,该应用程序通过简单的二进制火警/无火警功能显示火警状态。

我遇到麻烦的是编程蓝牙功能。我对Java有一些经验,但对蓝牙功能不熟悉。我想要编写一个按钮,用于搜索和连接蓝牙设备。

这是我在MainActivity.kt中开始的代码片段

package com.example.myapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val daqButton = findViewById<Button>(R.id.daqButton)

        daqButton.setOnClickListener {

        }
    }
}

这是我在AndroidManifest.xml中开始的代码片段

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

    <!-- If your app targets Android 9 or lower, you can declare
         ACCESS_COARSE_LOCATION instead. -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

非常感谢您提前给予的任何帮助、技巧和建议!

英文:

Hopefully someone can send me in the right direction.

I am attempting to program a simple app that connects via bluetooth to a piece of fire alarm equipment. The overall goal is that when the fire alarm is triggered on the equipment, the app shows the fire alarm status via simple binary Alarm/No alarm function.

What I am having trouble with is programming the bluetooth function. I have experience with Java but not with a bluetooth functionality. I want to program a button that searches and connects to bluetooth devices.

Here is the bit of code I've started in MainActivity.kt

package com.example.myapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val daqButton = findViewById&lt;Button&gt;(R.id.daqButton)

        daqButton.setOnClickListener {

        }
    }
}

And here's the code I've started in AndroidManifest.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    package=&quot;com.example.myapplication&quot;&gt;
    &lt;uses-permission android:name=&quot;android.permission.BLUETOOTH&quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.BLUETOOTH_ADMIN&quot; /&gt;

    &lt;!-- If your app targets Android 9 or lower, you can declare
         ACCESS_COARSE_LOCATION instead. --&gt;
    &lt;uses-permission android:name=&quot;android.permission.ACCESS_FINE_LOCATION&quot; /&gt;

    &lt;application
        android:allowBackup=&quot;true&quot;
        android:icon=&quot;@mipmap/ic_launcher&quot;
        android:label=&quot;@string/app_name&quot;
        android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
        android:supportsRtl=&quot;true&quot;
        android:theme=&quot;@style/AppTheme&quot;&gt;
        &lt;activity android:name=&quot;.MainActivity&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

                &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
    &lt;/application&gt;

&lt;/manifest&gt;

Any tips or tricks to help me accomplish this is greatly appreciated! Thanks in advance 想要编写应用程序以连接蓝牙设备。

答案1

得分: 0

好的,以下是翻译好的内容:

实现这一功能最简单的方法是使用类似于这个蓝牙库的第三方库。

  • 将库依赖添加到您的app module gradle文件中。
  • 按照在库的GitHub存储库中所述,在您的应用程序中设置库。
  • 如果您需要从应用程序连接到设备,您必须自己实现扫描功能,通过提供一个ListView并插入已发现的蓝牙设备的逻辑。 请参阅Github存储库文档,您将找到一些可用于实现搜索、发现和连接功能的回调函数。
  • 如果您愿意采用简单的方法,只需从设备的蓝牙管理器外部连接到蓝牙设备,然后您可以按如下方式与设备通信。
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main)

    bluetooth = new Bluetooth(this);
    bluetooth.send("Your message");

}
  • 如果您想从另一个蓝牙设备接收一些响应,您必须侦听DeviceCallback() 您将在Github存储库中找到其描述

注意

您必须在清单文件中添加一些权限,它们是

  • BLUETOOTH
  • BLUETOOTH_ADMIN
  • ACCESS_COARSE_LOCATION

以便允许您的应用程序使用蓝牙

英文:

Well, the easiest way to implement that is to use a third-party library like this Bluetooth Library.

  • Include the library dependency into your app module gradle file.
  • set up the library in your app as described in the library's repository on GitHub.
  • If you need to connect to the device from the app you have to implement the scanning functionality yourself by providing a ListView and the logic to insert the discovered Bluetooth devices. Refer to the Github repository documentation you will find some callbacks that you can use to implement the search, discovery, and connection functionalities.
  • If you prefer to follow the easy way just connect to the Bluetooth device externally from your device's Bluetooth manager, then you can communicate with the device as follows.
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main)

    bluetooth = new Bluetooth(this);
    bluetooth.send(&quot;Your message&quot;);

}

  • If you want to receive back some response from the other Bluetooth device you have to listen to the DeviceCallback() you will find its description in the Github repository

Note

You have to add some permissions in the Manifest files which are

  • BLUETOOTH
  • BLUETOOTH_ADMIN
  • ACCESS_COARSE_LOCATION

In order to give access to your app to use the Bluetooth

huangapple
  • 本文由 发表于 2020年10月3日 01:24:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/64175832.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定