/*
* 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.
*/
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
private static final String NAME = "BluetoothChat";
// Unique UUID for this application
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
// Member fields
private final BluetoothAdapter mAdapter;//本地蓝牙适配器
private final Handler mHandler;//Handler
private AcceptThread mAcceptThread;//接受请求连接时的监听线程
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
*/
//构造函数:取得本地蓝牙适配器、设置状态为STATE_NONE,设置传递进来的handler为mHandler。
public BluetoothChatService(Context context, Handler handler) {
mAdapter = BluetoothAdapter.getDefaultAdapter();
mState = STATE_NONE;
//将BluetoothChat中的Handle赋值给BluetoothChatService中的handler;这样就使得BluetoothChat和luetoothChatService中的是同一个Handler
mHandler = handler;
}
/**
* Set the current state of the chat connection
* @param state An integer defining the current connection state
*/
//设置连接状态,并通知UI界面也同时更改状态
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
//得到当前状态并发送给Handler,由于BluetoothChat和BluetoothChatService中的是同一个Handler,所以当前
//状态将会发送到BluetoothChat中Handler的handleMessage函数中,然后在handleMessage函数中更新当前状态即可
mHandler.obtainMessage(BluetoothChat.MESSAGE_STATE_CHANGE, state, -1).sendToTarget();
}
/**
* Return the current connection state.
* 返回当前状态*/
public synchronized int getState() {
return mState;
}
/*************************************************************************************************
* 在BluetoothChatService这个类中的Start()函数中实际上是取消所有链接并启动AcceptThread线程、
* connect(BluetoothDevice device)函数中实际上是启动ConnectThread线程、
* connected(BluetoothSocket socket, BluetoothDevice device)函数中实际上是启动ConnectedThread线程、
* ***********************************************************************************************/
/**
* Start the chat service. Specifically start AcceptThread to begin a
* session in listening (server) mode. Called by the Activity onResume()
* 此Start()函数并不是线程的Start函数,他只是BluetoothChatService这个类中自定义的一个函数名为Start的函数
* start函数用来开始聊天服务。特别是开始AcceptThread来开始一个监听(服务器)模式的会话。操作很简单,就是
* 首先取消另外两个进程,新建一个AcceptThread进程,并启动AcceptThread进程,最后设置状态 变为监听(STATE_LISTEN),
* 这时UI界面的title也将更新为监听状态,即等待设备的连接。*/
public synchronized void start() {
if (D) Log.d(TAG, "start");
// 取消试图进行连接的任何线程(连接线程),来生成一个连接
if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}
// 取消正在运行连接的任何线程(已经连接的线程)
if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}
// 启动AcceptThread线程来监听BluetoothServerSocket
if (mAcceptThread == null) {
mAcceptThread = new AcceptThread();
mAcceptThread.start();
}
setState(STATE_LISTEN);
}
/**
* Start the ConnectThread to initiate a connection to a remote device.
* @param device The BluetoothDevice to connect
* 初始化ConnectThread线程来启动与远程设备连接
*/
public synchronized void connect(BluetoothDevice device) {
if (D) Log.d(TAG, "connect to: " + device);
// 取消试图进行连接的任何线程(连接线程),来生成一个连接
if (mState == STATE_CONNECTING) {
if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}
}
// 取消正在运行连接的任何线程(已经连接的线程)
if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}
// 启动线程与给定的设备连接
mConnectThread = new ConnectThread(device);
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线程来管理蓝牙连接
*/
public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) {
if (D) Log.d(TAG, "connected");
//取消三个线程
// 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 (mAcceptThread != null) {mAcceptThread.cancel(); mAcceptThread = null;}
// 启动线程来管理连接和进行传输
mConnectedThread = new ConnectedThread(socket);
mConnectedThread.start();
// 发送链接的设备名称给UI Activity界面
Message msg = mHandler.obtainMessage(BluetoothChat.MESSAGE_DEVICE_NAME);
Bundle bundle = new Bundle();
bundle.putString(BluetoothChat.DEVICE_NAME, device.get
没有合适的资源?快使用搜索试试~ 我知道了~
蓝牙小车项目APP
共80个文件
class:21个
jpg:19个
xml:11个
5星 · 超过95%的资源 需积分: 27 41 下载量 195 浏览量
2017-11-18
22:14:11
上传
评论 5
收藏 1.08MB ZIP 举报
温馨提示
这是前面蓝牙小车项目使用的的APP源码,蓝牙小车项目资源链接http://download.csdn.net/download/huangweiqing80/10124241
资源推荐
资源详情
资源评论
收起资源包目录
蓝牙APP.zip (80个子文件)
蓝牙APP
go_down.psd 28KB
right.psd 32KB
go_right.jpg 955B
go_up.psd 28KB
news.jpg 76KB
mid.jpg 10KB
backbout.jpg 64KB
black.jpg 955B
go_left.psd 27KB
left.jpg 12KB
right.jpg 12KB
left.psd 35KB
mid.psd 27KB
go_right.psd 28KB
go_left.jpg 955B
BluetoothChat
.project 849B
project.properties 360B
src
com
example
android
BluetoothChat
BluetoothChat.java 15KB
DeviceListActivity.java 11KB
BluetoothChatService.java 20KB
AndroidManifest.xml 2KB
res
drawable-ldpi
drawable-hdpi
news.jpg 76KB
backbout.jpg 64KB
app_icon.png 5KB
menu
option_menu.xml 1KB
drawable
in.png 4KB
go_right.jpg 17KB
mid.jpg 10KB
down.jpg 114KB
up.jpg 18KB
left.jpg 12KB
right.jpg 12KB
main.png 5KB
go_down.jpg 11KB
go_left.jpg 10KB
go_up.jpg 11KB
app_icon.png 4KB
drawable-mdpi
layout
activity.xml 1KB
device_name.xml 876B
device_list.xml 2KB
message.xml 876B
activity_main.xml 3KB
custom_title.xml 1KB
main.xml 2KB
drawable-xhdpi
values
strings.xml 2KB
.settings
org.eclipse.jdt.core.prefs 177B
assets
gen
com
example
android
BluetoothChat
BuildConfig.java 175B
R.java 4KB
.classpath 475B
bin
resources.ap_ 376KB
BluetoothChat.apk 390KB
classes
com
example
android
BluetoothChat
R$layout.class 645B
BluetoothChat$3.class 991B
BuildConfig.class 375B
R$drawable.class 824B
R$attr.class 385B
BluetoothChatService.class 6KB
DeviceListActivity$2.class 2KB
DeviceListActivity$3.class 955B
BluetoothChatService$ConnectedThread.class 2KB
BluetoothChat$4.class 991B
DeviceListActivity$1.class 2KB
R$id.class 932B
BluetoothChat.class 7KB
R.class 697B
R$string.class 1006B
BluetoothChatService$ConnectThread.class 3KB
R$menu.class 440B
BluetoothChat$1.class 2KB
BluetoothChatService$AcceptThread.class 3KB
DeviceListActivity.class 5KB
BluetoothChat$2.class 991B
AndroidManifest.xml 2KB
dexedLibs
annotations-f5b033d3444039cd46968260191fa304.jar 943B
jarlist.cache 120B
res
drawable-hdpi
app_icon.png 3KB
drawable
in.png 1KB
main.png 3KB
app_icon.png 1KB
classes.dex 26KB
共 80 条
- 1
资源评论
- codywon2018-07-15非常感谢楼主的!!!!!!!!
huangweiqing80
- 粉丝: 76
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Python大作业:音乐播放软件(爬虫+可视化+数据分析+数据库)
- 课程设计-python爬虫-爬取日报,爬取日报文章后存储到本地,附带源代码+课程设计报告
- 软件和信息技术服务行业投资与前景预测.pptx
- 课程设计-基于SpringBoot + Mybatis+python爬虫NBA球员数据爬取可视化+源代码+文档+sql+效果图
- 软件品质管理系列二项目策划规范.doc
- 基于TensorFlow+PyQt+GUI的酒店评论情感分析,支持分析本地数据文件和网络爬取数据分析+源代码+文档说明+安装教程
- 软件定义无线电中的模拟电路测试技术.pptx
- 软件开发协议(作为技术开发合同附件).doc
- 软件开发和咨询行业技术趋势分析.pptx
- 软件测试题详解及答案.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功