android - Android 的蓝牙示例

标签 android bluetooth

有没有人知道任何可用的示例来说明 Android 上的蓝牙开发。

我已阅读教程here我了解该页面上的所有内容。

然而,当谈到在应用程序中实现蓝牙代码时,有必要查看蓝牙聊天示例以了解其工作原理。

蓝牙聊天示例 here

这个例子很好,但也很难理解,因为每个设备最初都设置为服务器。

谁是服务器,两个设备都发送服务器套接字直到一个设备扫描?

一旦设备使自己可被发现,它就会成为服务器吗?

OnResume Activity 何时启动,因为一旦启动并且 mChatService 已在 SetupChat 中初始化,设备将启动一个接受线程。

下面给出了一些代码示例,上面提供了完整蓝牙聊天的链接。

@Override
public synchronized void onResume() {
    super.onResume();
    if(D) Log.e(TAG, "+ ON RESUME +");

    // Performing this check in onResume() covers the case in which BT was
    // not enabled during onStart(), so we were paused to enable it...
    // onResume() will be called when ACTION_REQUEST_ENABLE activity returns.
    if (mChatService != null) {
        // Only if the state is STATE_NONE, do we know that we haven't started already
        if (mChatService.getState() == BluetoothChatService.STATE_NONE) {
          // Start the Bluetooth chat services
          mChatService.start();
        }
    }
}

private void setupChat() {

    // Initialize the BluetoothChatService to perform bluetooth connections
    mChatService = new BluetoothChatService(this, mHandler);

    // Initialize the buffer for outgoing messages
    mOutStringBuffer = new StringBuffer("");
}


/**
 * Start the chat service. Specifically start AcceptThread to begin a
 * session in listening (server) mode. Called by the Activity onResume() */
public synchronized void start() {
    if (D) Log.d(TAG, "start");

    // Cancel any thread attempting to make a connection
    if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}

    // Cancel any thread currently running a connection
    if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}

    setState(STATE_LISTEN);

    // Start the thread to listen on a BluetoothServerSocket
    if (mSecureAcceptThread == null) {
        mSecureAcceptThread = new AcceptThread(true);
        mSecureAcceptThread.start();
    }
    if (mInsecureAcceptThread == null) {
        mInsecureAcceptThread = new AcceptThread(false);
        mInsecureAcceptThread.start();
    }
}

我要的是蓝牙的任何示例,这些示例更容易理解,并且示例清楚地将蓝牙的服务器端和客户端分开。 我用 Google 搜索了这个,我已经阅读了 developer.android.com 网站上提供的所有详细信息。

最佳答案

根据我收集到的信息,区别在于:服务器和客户端仅在建立蓝牙连接时存在(即在发现和配对过程中)。为了建立连接,一个设备充当服务器(使用 BluetoothServerSocket 类的实例),另一个充当客户端(使用 BluetoothSocket 类的实例)。 (代理)服务器监听传入请求,客户端请求监听服务器进行连接。建立连接后(请参阅 Android 开发指南中使用的方法的详细信息),(最初调用的)服务器和客户端仅使用 BluetoothSocket 对象进行交互。所以不存在这样的服务器/客户端区别。

您可以在开发指南中查看蓝牙聊天示例的代码,尤其是 BluetoothChatService 类的代码。调用方法 createRfcommSocketToServiceRecord() 会将 BluetotohSocket 返回到监听(服务器)设备。请求设备(客户端)使用类似的对象。

是的,进一步的示例代码会更好。

关于android - Android 的蓝牙示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6141668/

相关文章:

bluetooth - BLE 设备无法连续发送多个 GATT 通知

android - Google maps beam 在 android MapFragment 中显示用户方向/航向

java - Firebase - 如何确保已通过身份验证的用户不会再次进行身份验证?

android - 在 Android 浏览器中使用 input type=file capture=camera 上传照片时,防止照片自动保存到图库

Android:从我的闹钟中减去分钟数

node.js - 蓝牙连接问题。树莓派3和nodejs 7.4.0

java - 如何创建具有特定颜色(不是黑色)的透明位图?

android - 使用蓝牙不安全地连接到射频阅读器

ios - iPhone 应用程序 - 与蓝牙设备通信

java - 蓝牙连接;无法正确发送字符串