java实现蓝牙通信程序_Android蓝牙通信聊天实现发送和接受功能

这个博客介绍了一个简单的Java实现的Android蓝牙通信聊天应用,主要包含两个类:主界面类和蓝牙聊天服务类。通过创建线程并使用BluetoothChatService,实现了发送和接收蓝牙消息的功能。当用户点击发送按钮时,会调用BluetoothChatService的write方法并通过handler在界面上显示消息。同时,另一个客户端则不断读取并显示接收到的蓝牙消息,从而完成双向通信。
摘要由CSDN通过智能技术生成

很不错的蓝牙通信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 =

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值