很不错的蓝牙通信demo实现发送和接受功能,就用了两个类就实现了,具体内容如下
说下思路把 主要有两个类主界面类和 蓝牙聊天服务类。 首先创建线程 实际上就是创建BluetoothChatService()(蓝牙聊天服务类) 这个时候把handler 传过去 这样就可以操作UI 界面了,在线程中不断轮询读取蓝牙消息,当主界面点击发送按钮时 调用BluetoothChatService的发送方法write 方法,这里的write 方法 使用了handler 发送消息,在主界面显示,另一个 客户端 不断读取蓝牙消息 类似的有个read 方法 同样显示到界面上去,这样就完成了通信了。
import java.util.ArrayList;
import java.util.Set;
import android.app.Activity;
import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class BluetoothChat extends Activity {
// Message types sent from the BluetoothChatService Handler
public static final int MESSAGE_STATE_CHANGE = 1;
public static final int MESSAGE_READ = 2;
public static final int MESSAGE_WRITE = 3;
public static final int MESSAGE_DEVICE_NAME = 4;
public static final int MESSAGE_TOAST = 5;
// Key names received from the BluetoothChatService Handler
public static final String DEVICE_NAME = "device_name";
public static final String TOAST = "toast";
// Intent request codes
private static final int REQUEST_CONNECT_DEVICE = 1;
private static final int REQUEST_ENABLE_BT = 2;
private TextView mTitle;
private EditText text_chat;
private EditText text_input;
private Button but_On_Off;
private Button but_search; // ------> 在菜单中可以搜索
private Button but_create; // ------> 在菜单中设置"可被发现"
private Button mSendButton;
// 连接到的蓝牙设备的名称
private String mConnectedDeviceName;
// String buffer for outgoing messages
private StringBuffer mOutStringBuffer;
// Local Bluetooth adapter
private BluetoothAdapter mBluetoothAdapter = null;
// Member object for the chat services
private BluetoothChatService mChatService = null;
private ArrayList mPairedDevicesList = new ArrayList();
private ArrayList mNewDevicesList = new ArrayList();
private String[] strName;
private String address;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
mTitle = (TextView) this.findViewById(R.id.text_title);
text_chat = (EditText) this.findViewById(R.id.text_chat);
text_input = (EditText) this.findViewById(R.id.text_input);
but_On_Off = (Button) this.findViewById(R.id.but_off_on);
but_search = (Button) this.findViewById(R.id.but_search_div);
but_create = (Button) this.findViewById(R.id.but_cjlj);
mSendButton = (Button) this.findViewById(R.id.but_fsxx);
// 获得本地的蓝牙适配器
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// 如果为null,说明没有蓝牙设备
if (mBluetoothAdapter == null) {
Toast.makeText(this, "没有蓝牙设备", Toast.LENGTH_LONG).show();
finish();
return;
}
if (mBluetoothAdapter.isEnabled()) {
but_On_Off.setText("关闭蓝牙");
} else {
but_On_Off.setText("开启蓝牙");
}
but_On_Off.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (!mBluetoothAdapter.isEnabled()) {
mBluetoothAdapter.enable();
Toast.makeText(BluetoothChat.this, "蓝牙已开启",
Toast.LENGTH_SHORT).show();
but_On_Off.setText("关闭蓝牙");
} else {
mBluetoothAdapter.disable();
Toast.makeText(BluetoothChat.this, "蓝牙已关闭",
Toast.LENGTH_SHORT).show();
but_On_Off.setText("开启蓝牙");
}
}
});
but_search.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
searchDevice();
}
});
but_create.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final EditText et = new EditText(BluetoothChat.this);
et.setSingleLine();
et.setText(mBluetoothAdapter.getName());
new AlertDialog.Builder(BluetoothChat.this)
.setTitle("请输入房间名:")
.setView(et)
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
String name = et.getText().toString()
.trim();
if (name.equals("")) {
Toast.makeText(BluetoothChat.this,
"请输入房间名",
Toast.LENGTH_SHORT).show();
return;
}
// 设置房间名
mBluetoothAdapter.setName(name);
}
})
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
}
}).create().show();
// 创建连接,也就是设备本地蓝牙设备可被其他用户的蓝牙搜到
ensureDiscoverable();
}
});
// 获得一个已经配对的蓝牙设备的set集合
Set pairedDevices = mBluetoothAdapter
.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
mPairedDevicesList.add("已配对:" + device.getName() + "\n"
+ device.getAddress());
}
} else {
Toast.makeText(this, "没有已配对的设备", Toast.LENGTH_SHORT).show();
}
// 当发现一个新的蓝牙设备时注册广播
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(mReceiver, filter);
// 当搜索完毕后注册广播
filter =