Android的蓝牙范例 [英] Bluetooth Examples for Android

查看:132
本文介绍了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.

然而,当涉及到实现蓝牙code,到应用程序中,有必要查看蓝牙聊天的例子来理解所有它是如何工作。

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.

蓝牙聊天的例子这里

这个例子是好的,但也很难COM prehend因为每个设备的初始设置为服务器。

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

谁是服务器和两台设备发送服务器插槽,直到一台设备扫描?

一旦设备使自己发现它并成为服务器?

什么时候OnResume活动的开始,因为一旦启动和mChatService已SetupChat被初始化,设备将启动接受线程。

有些code例子如下,并链接到完整的蓝牙聊天上面是可用的。

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

我所要求的是蓝牙的任何例子,更易于理解和例子清楚地隔离在服务器端和蓝牙的客户端。 我Google'd这一点,我已经阅读可在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类列于开发指南蓝牙聊天的例子的code,。调用方法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.

诚然,另一个例子codeS会更好。

True, further example codes would be nicer.

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

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