制作安卓蓝牙app的基本原理就是使用android sdk中提供的bluetooth api,通过蓝牙模块在设备之间建立无线通信,并实现数据传输的功能。
下面是一个简单的示例,展示如何使用android studio开发一个与外围蓝牙设备进行连接并发送数据的应用程序。
1. 创建新项目
在android studio中创建一个新的应用程序项目。在项目创建完成后,打开build.gradle文件并添加以下依赖项:
“`
dependencies {
implementation ‘com.android.support:appcompat-v7:28.0.0’
implementation ‘com.android.support.constraint:constraint-layout:1.1.3’
implementation ‘com.android.support:design:28.0.0’
}
“`
2. 布局设计
在activity_main.xml中添加两个按钮,一个用于搜索可用设备,另一个用于连接选定的设备。
“`
android:id=”@ id/button_search”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”搜索”
app:layout_constraintbottom_tobottomof=”parent”
app:layout_constrainthorizontal_bias=”0.5″
app:layout_constraintleft_toleftof=”parent”
app:layout_constraintright_toleftof=”@ id/button_connect”
app:layout_constrainttop_totopof=”parent”
app:layout_constraintvertical_bias=”0.5″ />
android:id=”@ id/button_connect”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”连接”
app:layout_constraintbottom_tobottomof=”parent”
app:layout_constrainthorizontal_bias=”0.5″
app:layout_constraintright_torightof=”parent”
app:layout_constrainttop_totopof=”parent”
app:layout_constraintvertical_bias=”0.5″ />
“`
3. 定义java类
在mainactivity.java文件中定义两个按钮的监听器,并使用bluetoothadapter类来搜索和连接设备。此外,也需要使用bluetoothsocket类与外围设备进行无线通信。
“`
public class mainactivity extends appcompatac
tivity {
private final static int request_enable_bt = 1;
private bluetoothadapter bluetoothadapter;
private set paireddevices;
private arrayadapter devicesarrayadapter;
private listview listview;
private bluetoothdevice selecteddevice;
private bluetoothsocket mmsocket;
private outputstream mmoutputstream;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
listview = findviewbyid(r.id.listview);
checkbluetoothstate();
// 设置搜索按钮监听器
button searchbutton = (button) findviewbyid(r.id.button_search);
searchbutton.setonclicklistener(new view.onclicklistener() {
@override
public void onclick(view view) {
discoverdevices();
}
});
// 设置连接按钮监听器
button connectbutton = (button) findviewbyid(r.id.button_connect);
connectbutton.setonclicklistener(new view.onclicklistener() {
@override
public void onclick(view view) {
connecttodevice(selecteddevice);
}
});
}
private void checkbluetoothstate() {
bluetoothadapter = bluetoothadapter.getdefaultadapter();
if (bluetoothadapter == null) {
toast.maketext(this, “蓝牙不可用”, toast.length_short).show();
finish();
} else {
if (!bluetoothadapter.isenabled()) {
intent enableintent = new intent(bluetoothadapter.action_request_enable);
startactivityforresult(enableintent, request_enable_bt);
}
}
}
private void discoverdevices() {
if (bluetoothadapter.isdiscovering()) {
bluetoothadapter.canceldiscovery();
}
bluetoothadapter.startdiscovery();
broadcastreceiver discoveryresult = new broadcastreceiver() {
@override
public void onreceive(context context, intent intent) {
string action = intent.getaction();
if (bluetoothdevice.action_found.equals(action)) {
bluetoothdevice device = intent.getparcelableextra(bluetoothdevice.extra_device);
devicesarrayadapter.add(device.getname() “\n” device.getaddress());
}
}
};
intentfilter filter = new intentfilter(bluetoothdevice.action_found);
registerreceiver(discoveryresult, filter);
devicesarrayadapter = new arrayadapter(this, android.r.layout.simple_list_item_1);
listview.setadapter(devicesarrayadapter);
}
private void connecttodevice(bluetoothdevice device) {
uuid uuid = uuid.fromstring(“00001101-0000-1000-8000-00805f9b34fb”);
try {
mmsocket = device.createrfcommsockettoservicerecord(uuid);
mmsocket.connect();
mmoutputstream = mmsocket.getoutputstream();
} catch (ioexception e) {
e.printstacktrace();
}
}
}
“`
4. 在manifest文件中添加蓝牙权限
“`
“`
5. 运行和测试
在运行和测试你的应用程序之前,请确保你的android设备已启用蓝牙,以及附近有其他蓝牙设备可供连接。
结论:通过这个简单的示例,你可以了解到制作安卓蓝牙app的基础知识,并且可以通过使用android sdk中提供的各种类和api,自己设计出更加强大的应用程序。




