package com.example.helloworld;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import android.R.bool;
import android.R.integer;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;
@SuppressLint("NewApi")
public class BluetoothChat {
private static final String s_Tag = "BluetoothChat";
private BluetoothAdapter mBluetoothAdapter = null;//蓝牙适配器
private Context mContext;
private Message message = new Message();//消息
private OnPortListener mOnPortListener;
private PortListenThread mPortListenThread;
private OutputStream mOutputStream;
private InputStream mInputStream;
private int m_State;// 状态
private int m_StateConnected = 0;
private int m_StateDisconnect = 1;
private boolean m_IsNormalClose = false;
// handler处理数据
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
Toast.makeText(mContext, "没有适配器", Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
};
private static final String S_NAME = "BluetoothChat";
private static final UUID S_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");// UUID随机产生
// 接口
public interface OnPortListener {
public abstract void OnReceiveData(String p_Message);
}
// 构造方法
public BluetoothChat(Context pContext) {
mContext = pContext;
message.what = 1;
mOnPortListener = (OnPortListener) pContext;
}
public void CreatePortListen() {
try {
// 获取一个本机蓝牙设备
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// 判断是否不存在蓝牙设备或没有打开
if (mBluetoothAdapter == null || mBluetoothAdapter.isEnabled()) {
// 通过蓝牙设备创建一个无线射频通信蓝牙端口
BluetoothServerSocket _BluetoothServerSocket =
mBluetoothAdapter.listenUsingRfcommWithServiceRecord(S_NAME, S_UUID);
if (mPortListenThread == null) {
// 启动端口监听线程
mPortListenThread = new PortListenThread(
_BluetoothServerSocket);
mPortListenThread.start();
}
} else {
mHandler.sendMessage(message);
}
} catch (IOException e) {
// TODO Auto-generated catch block
Log.i(s_Tag, "CreatePortListen:" + e.getMessage());
CreatePortListen();
}
}
public class PortListenThread extends Thread {
private BluetoothServerSocket mBluetoothServerSocket;
BluetoothSocket mBluetoothSocket;
public PortListenThread(BluetoothServerSocket pBluetoothServerSocket) {
// 初始化Socket
mBluetoothServerSocket = pBluetoothServerSocket;
}
@Override
public void run() {
try {
// 调用accept接受对方数据请求
mBluetoothSocket = mBluetoothServerSocket.accept();
// 获取输出流
mOutputStream = mBluetoothSocket.getOutputStream();
// 修改链接状态
m_State = m_StateConnected;
// 建立一个长连接持续接受对方数据
//while (m_State == m_StateConnected) {
// 获取输入流
mInputStream = mBluetoothSocket.getInputStream();
ReceiveData();// --ReceiveData()
//}
} catch (IOException e) {
Log.i(s_Tag, e.getMessage());
if (!mBluetoothAdapter.isEnabled()) {
mHandler.sendMessage(message);
}
}
}
public void close() {
try {
mBluetoothServerSocket.close();
if (mBluetoothSocket != null) {
mBluetoothSocket.close();
}
if (mOutputStream != null) {
mOutputStream.close();
}
if (mInputStream != null) {
mInputStream.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
Log.i(s_Tag, e.getMessage());
}
}
}
public void ReceiveData() {
try {
// 初始化字节数组
byte[] _Byte = new byte[8];
// 读取前8个自己获取数据长度
mInputStream.read(_Byte);
// 将字符转换为String字符
String _Msg = new String(_Byte);
// 将String字符转换为Int数字
int _length = Integer.parseInt(_Msg);
// 按得到的长度再初始化一个字节数组
_Byte = new byte[_length];
// 继续读取剩余的数据
mInputStream.read(_Byte);
// 将两次数据合并为一个完整的数据
_Msg = _Msg + new String(_Byte);
// 调用回调函数,返回界面处理
mOnPortListener.OnReceiveData(_Msg);
} catch (IOException e) {
Log.i(s_Tag, e.getMessage());
if (!m_IsNormalClose) {
// Close(false);
CreatePortListen();
}
}
}
public void SendData(String p_Data) {
try {
// 调用输出流向对方发送数据
mOutputStream.write(p_Data.getBytes());
// 强制输出所有缓冲的数据
mOutputStream.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void Close(boolean p_IsNomalClose){
m_IsNormalClose = p_IsNomalClose;
m_State = m_StateDisconnect;
if(mPortListenThread != null){
mPortListenThread.close();
mPortListenThread = null;
}
}
}
蓝牙开发
最新推荐文章于 2023-08-29 09:54:17 发布