蓝牙扫描有两种方式startScan和startLeScan,其中startLeScan官方@Deprecated
本文只做了蓝牙的扫描和显示。
1、配置蓝牙权限
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
2、xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="搜索"/>
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
3、第一种蓝牙扫描startScan
public class MainActivity extends AppCompatActivity {
private ArrayList<String> data = null;
private ListView listView = null;
private BluetoothAdapter bta = null;
private BluetoothManager btm = null;
private Button button = null;
ArrayAdapter<String> adapter = null;
private BluetoothLeScanner scanner = null;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获得数据
this.data = new ArrayList<>();
// 初始化控件
this.initView();
}
private void initView(){
// 获得button
this.button = findViewById(R.id.search);
this.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 扫描蓝牙
scanBle();
}
});
// 初始化ListView
this.adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, this.data);
this.listView = findViewById(R.id.lv);
this.listView.setAdapter(adapter);
}
// 蓝牙扫描回调
private ScanCallback newBtsc = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
BluetoothDevice device = result.getDevice();
// 更新数据
data.add(device.getAddress()+""+device.getName());
adapter.notifyDataSetChanged();
}
@Override
public void onBatchScanResults(List<ScanResult> results) {
super.onBatchScanResults(results);
}
@Override
public void onScanFailed(int errorCode) {
super.onScanFailed(errorCode);
}
};
// 扫描蓝牙
private void scanBle(){
// 初始化蓝牙适配器
btm = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
if(btm == null){
return;
}
bta = btm.getAdapter();
// 打开蓝牙
if(bta == null || !bta.isEnabled()){
Intent enable = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enable, 1);
}else{
scanner = bta.getBluetoothLeScanner();
scanner.startScan(newBtsc);
// 设置扫描时间
handler.postDelayed(new Runnable() {
@Override
public void run() {
scanner.stopScan(newBtsc);
}
}, 8000);
}
}
}
3、第二种蓝牙扫描startLeScan
public class MainActivity extends AppCompatActivity {
private ArrayList<String> data = null;
private ListView listView = null;
private BluetoothAdapter bta = null;
private BluetoothManager btm = null;
private Button button = null;
ArrayAdapter<String> adapter = null;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获得数据
this.data = new ArrayList<>();
// 初始化控件
this.initView();
}
private void initView() {
// 获得button
this.button = findViewById(R.id.search);
this.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 扫描蓝牙
scanBle();
}
});
// 初始化ListView
this.adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, this.data);
this.listView = findViewById(R.id.lv);
this.listView.setAdapter(adapter);
}
private BluetoothAdapter.LeScanCallback oldBtsc = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
data.add(device.getAddress() + "");
adapter.notifyDataSetChanged();
}
};
// 扫描蓝牙
private void scanBle() {
// 初始化蓝牙适配器
btm = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
if( btm == null){
return;
}
bta = btm.getAdapter();
// 打开蓝牙
if (bta == null || !bta.isEnabled()) {
Intent enable = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enable, 1);
} else {
bta.startLeScan(oldBtsc);
// 设置扫描时间
handler.postDelayed(new Runnable() {
@Override
public void run() {
bta.stopLeScan(oldBtsc);
}
}, 8000);
}
}
}