/*
* 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.beyond.testbt;
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;
import android.widget.Toast;
/**
* 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 BluetoothService {
// 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("fa87c0d0-afac-11de-8a39-0800200c9a66");
private static final UUID MY_UUID = UUID
.fromString("00001101-0000-1000-8000-00805F9B34FB");
// private static final UUID MY_UUID =
// UUID.fromString("896db752-44cc-4dda-954b-57dca98db571");
// Member fields
private final BluetoothAdapter mAdapter;
private final Handler mHandler;
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
private Object lock = new Object();
/**
* Constructor. Prepares a new BluetoothChat session.
*
* @param context
* The UI Activity Context
* @param handler
* A Handler to send messages back to the UI Activity
*/
public BluetoothService(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
*/
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(ScaleActivity.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()
*/
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;
}
// 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) {
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);
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) {
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;
}
// Start the thread to manage the connection and perform transmissions
mConnectedThread = new ConnectedThread(socket);
mConnectedThread.start();
// Send the name of the connected device back to the UI Activity
Message msg = mHandler.obtainMessage(ScaleActivity.MESSAGE_DEVICE_NAME);
Bundle bundle = new Bundle();
bundle.putString(ScaleActivity.DEVICE_NAME, device.getName());
msg.setData(bundle);
mHandler.sendMessage(msg);
setState(STATE_CONNECTED);
}
/**
* Stop all threads
*/
public synchronized void stop() {
if (D)
Log.d(TAG, "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);
}
/**
* Write to the ConnectedThread in an unsynchronized manner
*
* @param out
* The bytes to write
* @see ConnectedThread#write(byte[])
*/
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);
}
/**
* Indicate that the connection attempt failed and notify the UI Activity.
*/
private void connectionFailed() {
setState(STATE_LISTEN);
// Send a failure message back to the Activity
Message msg = mHandler.obtainMessage(ScaleActivity.MESSAGE_TOAST);
Bundle bundle = new Bundle();
bundle.putString(ScaleActivity.TOAST, "Unable to connect device");
msg.setData(bundle);
mHandler.sendMessage(msg);
}
/**
* Indicate that the connection was lost and notify the UI Activity.
*/
private void connectionLost() {
setState(STATE_LISTEN);
// Send a failure message back to the Activity
Message msg = mHandler.obtainMessage(ScaleActivity.MESSAGE_TOAST);
Bundle bundle = new Bundle();
bundle.putString(ScaleActivity.TOAST, "Device conn
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
BluetoothSeriaPortDemo.zip (54个子文件)
BluetoothSeriaPortDemo
.project 849B
bin
BluetoothDemo.apk 39KB
AndroidManifest.xml 1KB
jarlist.cache 119B
dexedLibs
annotations-8f82e589d00dcd88ab021ddd108d32b8.jar 943B
res
drawable-ldpi
icon.png 4KB
drawable-hdpi
icon.png 4KB
drawable
icon.png 4KB
drawable-mdpi
icon.png 4KB
resources.ap_ 24KB
classes
com
beyond
testbt
R$string.class 1019B
BluetoothService.class 6KB
R$layout.class 557B
BluetoothService$ConnectThread.class 2KB
R$drawable.class 397B
R$menu.class 392B
R.class 569B
BluetoothService$AcceptThread.class 3KB
BuildConfig.class 343B
DeviceListActivity.class 5KB
DeviceListActivity$2.class 2KB
R$attr.class 337B
ScaleActivity.class 8KB
ScaleActivity$1$1.class 1KB
DeviceListActivity$1.class 2KB
ScaleActivity$1.class 4KB
R$id.class 774B
BluetoothService$ConnectedThread.class 3KB
StringHexUtils.class 3KB
DeviceListActivity$3.class 875B
classes.dex 30KB
.settings
org.eclipse.core.resources.prefs 88B
org.eclipse.jdt.core.prefs 208B
AndroidManifest.xml 1KB
src
com
beyond
testbt
StringHexUtils.java 3KB
BluetoothService.java 14KB
DeviceListActivity.java 8KB
ScaleActivity.java 11KB
project.properties 562B
res
values
strings.xml 2KB
layout
other.xml 457B
message.xml 875B
custom_title.xml 1KB
device_list.xml 2KB
main.xml 392B
device_name.xml 875B
drawable-ldpi
icon.png 4KB
drawable-hdpi
icon.png 4KB
drawable
icon.png 4KB
drawable-mdpi
icon.png 4KB
menu
option_menu.xml 1KB
.classpath 380B
gen
com
beyond
testbt
R.java 3KB
BuildConfig.java 159B
assets
共 54 条
- 1
joonyli
- 粉丝: 1
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- java +elasticsearch+倒排索引技术+PUT+GET+全文搜索、短语匹配、近似搜索和聚合等操作高效执行
- ES数据迁移~~~~~~~~~~~~~~
- E3P303-VB一款SOP8封装2个P-Channel场效应MOS管
- SpyGlass-LowPowerRules-Reference
- 基于Java的SSM框架矿建环境搭建设计源码
- SpyGlass-PowerVerifyAdvancedRules-Reference
- java作业管理系统设计(源代码+论文).rar
- 奥迪C6仪表开机报文··
- SpyGlass-PowerEstimate-And-SpyGlass-PowerReduceRules-Reference
- E3P102-VB一款SOP8封装2个P-Channel场效应MOS管
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页