/*
* 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;
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
*/
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
*/
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() */
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(BluetoothChat.MESSAGE_DEVICE_NAME);
Bundle bundle = new Bundle();
bundle.putString(BluetoothChat.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(BluetoothChat.MESSAGE_TOAST);
Bundle bundle = new Bundle();
bundle.putString(BluetoothChat.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 =
android蓝牙聊天软件


在Android平台上,蓝牙通信是一个重要的功能,特别是在设备间的数据交换或者构建简单的互动应用时。"android蓝牙聊天软件"是一个典型的Android开发案例,它涉及到许多关键的技术点,对于想要深入理解Android蓝牙编程的开发者来说,这是一个不可多得的学习资源。 我们要了解Android蓝牙的基础知识。在Android系统中,蓝牙功能主要通过BluetoothAdapter类来访问和控制。这个类提供了开启、关闭蓝牙,查找其他蓝牙设备,以及建立连接等方法。此外,我们还需要用到BluetoothDevice类来代表一个蓝牙设备,通过其获取设备的信息并建立连接。 在蓝牙聊天应用中,数据传输是核心部分。Android提供了BluetoothSocket类用于创建两个设备之间的连接,并通过输入输出流进行数据交换。通常,我们需要创建一个服务器端Socket监听特定的UUID(通用唯一识别码),而客户端则通过这个UUID寻找并连接到服务器。一旦连接建立,就可以通过InputStream和OutputStream进行双向通信。 在实现聊天功能时,消息的发送和接收通常会涉及到线程管理。因为蓝牙通信通常在后台线程进行,以免阻塞UI主线程。Android提供了Handler和Looper机制,可以将后台线程处理的结果发送回主线程更新界面。这样,用户可以实时看到聊天信息的变化。 "BluetoothChat"很可能就是这个项目的主要源代码文件夹,其中包含了Activity、Service、BroadcastReceiver等相关组件。Activity通常负责用户界面的显示和交互,Service用于处理长时间运行的蓝牙通信任务,BroadcastReceiver则用于监听蓝牙状态的变化,如设备连接或断开等事件。 在AndroidManifest.xml文件中,我们需要为蓝牙相关的权限添加声明,例如ACCESS_FINE_LOCATION(因为蓝牙需要位置权限)和BLUETOOTH_ADMIN、BLUETOOTH权限,以便进行蓝牙操作。 此外,为了提供良好的用户体验,蓝牙聊天应用通常会有设备配对、连接状态显示、消息历史记录等功能。这些都需要利用到ListView或者RecyclerView来展示,以及SharedPreferences来存储用户设置和聊天记录。 "android蓝牙聊天软件"涵盖了Android蓝牙通信、UI设计、线程管理、服务、广播接收器等多个方面的知识,是Android开发学习的一个很好的实战项目。通过研究这个项目,开发者不仅可以掌握蓝牙通信的实现,还能提高在Android平台上的综合开发能力。
























































































- 1

- #完美解决问题
- #运行顺畅
- #内容详尽
- #全网独家
- #注释完整

- 粉丝: 1
- 资源: 5
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 编程语言_Python_实用技巧_Cookbook_1741399547.zip
- Python基础学习_50课教程_1741399454.zip
- 编程语言_Python基础应用_全面教程_实用工具_1741403170.zip
- 机器学习运维_Azure机器学习服务_持续集成持续部署_实践_1741402178.zip
- 网络分析_NetworkX核心类算法性能测试_Benchma_1741401263.zip
- 编程_Python语言_入门教程_自学_1741402607.zip
- 通信_示例代码库_Python_Telegram_Bot_1741398712.zip
- 计算机科学思维_ThinkPython_第三版_教学材料_1741403153.zip
- Python_编程技巧_中文食谱_学习资源_1741398408.zip
- 计算机科学_ThinkPython_教材源码_学习辅助_1741400453.zip
- 编程语言_Python_深度学习_教程系列_1741401224.zip
- Ubuntu_Python基础教程_安装配置_Tutoria_1741399913.zip
- 编程_Python_提示工具包_REPL应用_1741573678.zip
- 编程_Python_速查表_学习工具_1741399893.zip
- 编程语言_Python3学习资源_教学辅助_入门提升_1741398489.zip
- 文档编写_开源项目_README_指南_1741399902.zip


