适用于 Android 的蓝牙示例 [英] Bluetooth Examples for Android

查看:22
本文介绍了适用于 Android 的蓝牙示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道说明 Android 上蓝牙开发的任何可用示例.

Does anybody know of any example available that illustrates Bluetooth development on Android.

我已经阅读了这里的教程,并且我了解了所有内容页.

I have read the tutorial here and I understand everything on that page.

然而,在实现蓝牙代码时,需要查看蓝牙聊天示例以了解其工作原理.

However when it comes to implementing the Bluetooth code, into an application it is necessary to view the Bluetooth Chat example to understand how it all works.

蓝牙聊天示例此处

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

This example is good, but it is also hard to comprehend because each device is initially set up to be a server.

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

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

OnResume 活动何时开始,因为一旦开始并且在 SetupChat 中初始化了 mChatService,设备将启动一个接受线程.

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

Some code examples are given below, and link to the full Bluetooth chat is available above.

@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();
    }
}

我要求的是任何更容易理解的蓝牙示例,以及清楚地将蓝牙的服务器端和客户端分开的示例.我已经谷歌了这个,我已经阅读了 developer.android.com 网站上提供的所有详细信息.

What I am asking for is for any examples of Bluetooth, that are easier to understand and examples that clearly segregate the server side and the client side of Bluetooth. I have Google'd this, and I have read all details available on the developer.android.com website.

推荐答案

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

From what I have gathered, the distinction: server and client exists only while the Bluetooth connection is being established (ie during the discovery and pairing process). For the connection to be established, one device acts as a server (using an instance of BluetoothServerSocket class) and the other acts as a client (using an instance of the BluetoothSocket class). The (acting) server listens for incoming requests and the client requests listening servers to connect. After the connection is established (see the details of the methods used on the Android Dev Guide), both the (initially called) server and client interact using the BluetoothSocket object only. So no such server/client distinction exists.

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

You can check out the code of the Bluetooth Chat example on the Dev Guide, particularly of the BluetoothChatService class. Call to the method createRfcommSocketToServiceRecord() returns a BluetotohSocket to the listening(server) device. The requesting device(client) as it is uses a similar object.

没错,进一步的示例代码会更好.

True, further example codes would be nicer.

这篇关于适用于 Android 的蓝牙示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆