package jdl.android.test;
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.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
public class BluetoothService {
//debug
private final String TAG = "BluetoothService";
// Name for the SDP record when creating server socket
private static final String NAME = "BluetoothTest";
//唯一的UUID,用于蓝牙连接
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
//连接状态常量
public static final int STATE_NONE = 0; // 任何线程未启动
public static final int STATE_LISTEN = 1; // AcceptThread启动,监听并准备连接
public static final int STATE_CONNECTING = 2; // ConnectThread启动,主动连接
public static final int STATE_CONNECTED = 3; // ConnectedThread启动,已经连接完成
//成员变量
private final BluetoothAdapter mAdapter;
private final Handler mHandler;
private int mState;
//三个线程
private AcceptThread mAcceptThread;
private ConnectThread mConnectThread;
private ConnectedThread mConnectedThread;
public BluetoothService(Handler handler) {
mAdapter = BluetoothAdapter.getDefaultAdapter();
mState = STATE_NONE;
mHandler = handler;
}
public synchronized int getState() {
return mState;
}
public synchronized void setState(int state) {
mState = state;
//返回给Activity状态改变信息
mHandler.obtainMessage(MainActivity.MESSAGE_STATE_CHANGE, state, -1).sendToTarget();
}
//以下3个方法对应3个线程启动操作,stop停止所有线程
/**
* Start the chat service. Specifically start AcceptThread to begin a
* session in listening (server) mode. Called by the Activity onResume() */
public synchronized void 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;}
// Start the thread to listen on a 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
*/
public synchronized void connect(BluetoothDevice 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);
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
*/
public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) {
// 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;}
// Start the thread to manage the connection and perform transmissions
mConnectedThread = new ConnectedThread(socket);
mConnectedThread.start();
Log.i("thethief","ConnectdeThread is started");
// Send the name of the connected device back to the UI Activity
Message msg = mHandler.obtainMessage(MainActivity.MESSAGE_DEVICE_NAME);
Bundle bundle = new Bundle();
bundle.putString(MainActivity.DEVICE_NAME, device.getName());
msg.setData(bundle);
mHandler.sendMessage(msg);
Log.i("thethief","DEVICE_NAME msg is send");
setState(STATE_CONNECTED);
}
/**
* Stop all threads
*/
public synchronized void stop() {
if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}
if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}
if (mAcceptThread != null) {mAcceptThread.cancel(); mAcceptThread = null;}
setState(STATE_NONE);
}
//写入信息到ConnectedThread,准备输出
public void write(byte[] out) {
// Create temporary object
ConnectedThread r;
// Synchronize a copy of the ConnectedThread
synchronized (this) {
if (mState != STATE_CONNECTED) return;
r = mConnectedThread;
}
// Perform the write unsynchronized
r.write(out);
}
//连接失败
private void connectionFailed() {
setState(STATE_LISTEN);
// Send a failure message back to the Activity
Message msg = mHandler.obtainMessage(MainActivity.MESSAGE_TOAST);
Bundle bundle = new Bundle();
bundle.putString(MainActivity.TOAST, "Unable to connect device");
msg.setData(bundle);
mHandler.sendMessage(msg);
}
//连接丢失
private void connectionLost() {
setState(STATE_LISTEN);
// Send a failure message back to the Activity
Message msg = mHandler.obtainMessage(MainActivity.MESSAGE_TOAST);
Bundle bundle = new Bundle();
bundle.putString(MainActivity.TOAST, "Device connection was lost");
msg.setData(bundle);
mHandler.sendMessage(msg);
}
//三个线程类
/**
* 监听线程
* 服务启动即开启该线程,监听其他设备发出的连接请求
* 直到连接成功或被取消时停止
*/
private class AcceptThread extends Thread {
// The local server socket
private final BluetoothServerSocket mmServerSocket;
public AcceptThread() {
BluetoothServerSocket tmp = null;
// Create a new listening server socket
try {
tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
} catch (IOException e) {
Log.e(TAG, "listen() failed", e);
}
mmServerSocket = tmp;
}
public void run() {
setName("AcceptThread");
BluetoothSocket socket = null;
//如果不是STATE_CONNECTED就一直监听
while (mState != STATE_CONNECTED) {
try {
// This is a blocking call and will only return on a
// successful connection or an exception
socket = mmServerSocket.accept();
} catch (IOException e) {
Log.e(TAG, "accept() failed", e);
break;
}
//当接受到请求时
if (socket != null) {
synchronized (BluetoothService.this) {
switch (mState) {
case STATE_L
没有合适的资源?快使用搜索试试~ 我知道了~
wdj.zip_安卓 温度_安卓温度_手机 温度_温度计
共42个文件
class:18个
xml:8个
png:5个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 31 浏览量
2022-09-24
19:00:25
上传
评论
收藏 433KB ZIP 举报
温馨提示
安卓手机端温度计,通过单片机上的温度感应器传到手机端
资源推荐
资源详情
资源评论
收起资源包目录
wdj.zip (42个子文件)
wdj
bin
classes.dex 21KB
jdl
android
test
MainActivity.class 7KB
MainActivity$2.class 2KB
MainActivity$1.class 2KB
DeviceListActivity$3.class 870B
R$layout.class 521B
R$id.class 765B
R$drawable.class 457B
DeviceListActivity.class 5KB
BluetoothService.class 5KB
BluetoothService$AcceptThread.class 2KB
BluetoothService$ConnectThread.class 2KB
R$attr.class 334B
R$menu.class 389B
R.class 561B
BluetoothService$ConnectedThread.class 2KB
DeviceListActivity$2.class 2KB
R$string.class 1016B
DeviceListActivity$1.class 2KB
resources.ap_ 105KB
wdj.apk 118KB
res
drawable-ldpi
icon.png 2KB
menu
option_menu.xml 1KB
drawable-hdpi
icon.png 4KB
values
strings.xml 1KB
drawable-mdpi
ic_launcher.png 6KB
wdj1.png 149KB
icon.png 3KB
layout
wen.xml 1KB
device_list.xml 2KB
custom_title.xml 1KB
main.xml 2KB
device_name.xml 876B
assets
default.properties 362B
gen
jdl
android
test
R.java 3KB
proguard.cfg 1KB
src
jdl
android
test
BluetoothService.java 13KB
DeviceListActivity.java 8KB
MainActivity.java 12KB
.project 849B
.classpath 280B
AndroidManifest.xml 2KB
共 42 条
- 1
资源评论
JonSco
- 粉丝: 75
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功