1、Android studio创建项目
创建完成之后进入如下界面
删除界面的ConstraintLyout和Textview,新建一个布局(这里选择LinearLayout),添加两个按钮(Button)一个扫描的按钮和一个停止按钮,再加入一个显示数据的列表得到一个简易的APP界面
编辑完成之后在另外一个窗口进行编程
2、实现思路
(1)首先需要获得蓝牙权限:
在AndroidManifest.xml中
安卓6.0版本以前,需要以下权限
6.0版本以后,以上两者不够,还需要位置权限:
在MainActivity.java中:
ActivityCompat.requestPermissions(MainActivity.this, new String[]{ Manifest.permission.ACCESS_COARSE_LOCATION},1);
(2)获得蓝牙适配器,打开蓝牙
private BluetoothAdapter bleadapter = BluetoothAdapter.getDefaultAdapter();
private void enableBle(){
if(!bleadapter.isEnabled()){
bleadapter.enable();
}
}
(3)开始扫描
①可使用方法:bleadapter.startLeScan(leScanCallback);
该方法使用后会启动数据回传接口leScanCallback,所以只要编辑leScanCallback即可获取蓝牙相关数据。
private BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(BluetoothDevice bluetoothDevice, int i, byte[] bytes) {
//此处写处理信息
}
};
②也可以使用:bluetoothLeScanner.startScan(ScanCallback);
使用此方法要先获得BLE扫描
private BluetoothAdapter bleadapter = BluetoothAdapter.getDefaultAdapter();
private BluetoothLeScanner bluetoothLeScanner = bleadapter.getBluetoothLeScanner();
与第一个方法类似,此方法也能启动回传接口ScanCallback。编辑相应接口的信息即可获取蓝牙。
(4)停止扫描:①bleadapter.stopLeScan(leScanCallback);
②bluetoothLeScanner.stopScan(ScanCallback);
对应开始扫描的方法进行选择。
3、完整代码
MainActivity.java中:
package com.example.myapplication;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.ParcelUuid;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class MainActivity extends AppCompatActivity {
private ArrayList bledata_compare = null; //用于比较
private ArrayList bledata = null; //用于存储
ArrayAdapter adapter = null;
private Button Search;
private Button Stop;
private ListView lv;
//获取蓝牙适配器
private BluetoothAdapter bleadapter = BluetoothAdapter.getDefaultAdapter();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取权限
ActivityCompat.requestPermissions(MainActivity.this, new String[]{ Manifest.permission.ACCESS_COARSE_LOCATION},1);
//创建容器,用于数据传递
this.bledata_compare = new ArrayList<>();
this.bledata = new ArrayList<>();
//控件初始化
initView();
//数据传递
this.adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1, this.bledata);
//显示
lv.setAdapter(adapter);
}
private void initView(){
//找到控件对象
Search = findViewById(R.id.button);
Stop = findViewById(R.id.button2);
lv = findViewById(R.id.lv1);
//创建单击事件
Search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//开启蓝牙
enableBle();
//开始扫描
bleadapter.startLeScan(leScanCallback);
}
});
Stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//扫描停止
bleadapter.stopLeScan(leScanCallback);
}
});
}
//开启蓝牙
private void enableBle(){
if(!bleadapter.isEnabled()){
bleadapter.enable();
}
}
//数据回传接口
private BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(BluetoothDevice bluetoothDevice, int i, byte[] bytes) {
final ble_device ble_device = new ble_device(); //此处为新建的一个类,为ble_device
ble_device.ble_address = bluetoothDevice.getAddress();
if(bledata_compare.contains(ble_device.ble_address)) {
} //若列表中已经有了相应设备信息,则不添加进去
else {
bledata_compare.add(ble_device.ble_address);
bledata.add("address:"+bluetoothDevice.getAddress()+"\nname:"+bluetoothDevice.getName()+"\nrssi:"+i);
adapter.notifyDataSetChanged();
}
}
};
}
新建的类ble_device中:
package com.example.myapplication;
public class ble_device{
String ble_address;
//由于自带equals函数不能满足我们的需求,所以我们需要重写euals,可以理解为扩充
@Override
public boolean equals(Object obj){
if (obj instanceof ble_device){
if(ble_address.equals(((ble_device) obj).ble_address)){
return true;
}
else {
return false;
}
}
return super.equals(obj);
}
@Override
public int hashCode(){
return ble_address.hashCode();
}
}
(4)运行:
运行结果如下
参考文章:https://blog.csdn.net/qq_35270692/article/details/76359873
https://blog.csdn.net/qq_39586925/article/details/94232669