/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
*版权(C)2009的Android开源项目
*
*在Apache许可证授权下,版本2(“许可证”);
*你可以不使用此文件的许可遵守。
*你可以获得一份许可证的拷贝在
* HTTP:/ / www.apache。org /许可证/ license-2.0
*
*除非适用的法律要求或书面同意,软件
*分发的许可证发放的一个“是”的基础上,
*没有任何形式的保证或条件,无论是明示或暗示。
*查看许可证管理权限的具体语言
*在许可的限制。
*/
package com.example.android.BluetoothChat;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
/**
* This class does all the work for setting up and managing Bluetooth
* connections with other devices. It has a thread that listens for
* incoming connections, a thread for connecting with a device, and a
* thread for performing data transmissions when connected.
*/
/*
*这类设置和管理蓝牙做所有的工作
*与其他设备的连接。它有一个线程,监听
*传入的连接,一个连接装置的螺纹,和
*进行数据传输时,连接线。
*/
public class BluetoothChatService {
// Debugging
//调试
private static final String TAG = "BluetoothChatService";
private static final boolean D = true;
// Name for the SDP record when creating server socket
//的SDP记录名称创建服务器套接字时
private static final String NAME_SECURE = "BluetoothChatSecure";
private static final String NAME_INSECURE = "BluetoothChatInsecure";
// Unique UUID for this application
//此应用程序的唯一UUID
private static final UUID MY_UUID_SECURE =
UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");
private static final UUID MY_UUID_INSECURE =
UUID.fromString("8ce255c0-200a-11e0-ac64-0800200c9a66");
// Member fields
//域成员
private final BluetoothAdapter mAdapter;
private final Handler mHandler;
private AcceptThread mSecureAcceptThread;
private AcceptThread mInsecureAcceptThread;
private ConnectThread mConnectThread;
private ConnectedThread mConnectedThread;
private int mState;
// Constants that indicate the current connection state
//常数,指示当前连接状态
public static final int STATE_NONE = 0; // we're doing nothing--我们什么都不做
public static final int STATE_LISTEN = 1; // now listening for incoming connections--现在侦听传入的连接
public static final int STATE_CONNECTING = 2; // now initiating an outgoing connection--现在开始向外的连接
public static final int STATE_CONNECTED = 3; // now connected to a remote device--现在连接到远程设备
/**
* Constructor. Prepares a new BluetoothChat session.
* @param context The UI Activity Context
* @param handler A Handler to send messages back to the UI Activity
*/
/*
*构造函数。准备一个新的bluetoothchat会话。
* @param上下文语境的界面活性
* @param处理程序处理程序将消息发送回用户界面活性
*/
public BluetoothChatService(Context context, Handler handler) {
mAdapter = BluetoothAdapter.getDefaultAdapter();
mState = STATE_NONE;
mHandler = handler;
}
/**
* Set the current state of the chat connection
* @param state An integer defining the current connection state
*/
/*
*组聊天连接的当前状态
* @param状态定义当前连接状态
*/
private synchronized void setState(int state) {
if (D) Log.d(TAG, "setState() " + mState + " -> " + state);
mState = state;
// Give the new state to the Handler so the UI Activity can update
//给新状态的处理程序,界面活性可以更新
mHandler.obtainMessage(BluetoothChat.MESSAGE_STATE_CHANGE, state, -1).sendToTarget();
}
/**
* Return the current connection state.
返回当前的连接状态。
*/
public synchronized int getState() {
return mState;
}
/**
* Start the chat service. Specifically start AcceptThread to begin a
* session in listening (server) mode. Called by the Activity onResume()
*/
/*
*开始聊天服务。特别是开始acceptthread开始
*会话听力(服务器)模式。所谓的活动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
//动线程监听一个bluetoothserversocket
if (mSecureAcceptThread == null) {
mSecureAcceptThread = new AcceptThread(true);
mSecureAcceptThread.start();
}
if (mInsecureAcceptThread == null) {
mInsecureAcceptThread = new AcceptThread(false);
mInsecureAcceptThread.start();
}
}
/**
* Start the ConnectThread to initiate a connection to a remote device.
* @param device The BluetoothDevice to connect
* @param secure Socket Security type - Secure (true) , Insecure (false)
*/
/*
*开始connectthread发起一个连接到一个远程设备。
* @param装置的蓝牙设备连接
* @param安全插座安全类型安全的(真正的),不安全的(假)
*/
public synchronized void connect(BluetoothDevice device, boolean secure) {
if (D) Log.d(TAG, "connect to: " + device);
// Cancel any thread attempting to make a connection
//取消任何线程试图连接
if (mState == STATE_CONNECTING) {
if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}
}
// Cancel any thread currently running a connection
//取消任何线程正在运行的连接
if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}
// Start the thread to connect with the given device
//启动线程与给定的设备连接
mConnectThread = new ConnectThread(device, secure);
mConnectThread.start();
setState(STATE_CONNECTING);
}
/**
* Start the ConnectedThread to begin managing a Bluetooth connection
* @param socket The BluetoothSocket on which the connection was made
* @param device The BluetoothDevice that has been connected
*/
/*
*开始connectedthread开始经营蓝牙连接
* @param插座bluetoothsocket上连接了
* @param装置的蓝牙设备已连接
*/
public synchronized void connected(BluetoothSocket socket, BluetoothDevice
device, final String socketType) {
if (D) Log.d(TAG, "connected, Socket Type:" + socketType);
// Cancel the thread that completed the connection
//取消线程完成连接
if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}
// Cancel any thread currently running a connection
//取消任何线程正在运行的连接
if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}
// Cancel the accept thread because we only want to connect to one device
//取消接受线程因为我们只想连接到一个设备
if (mSecureAcceptThread != null) {
mSecureA