当前位置:   article > 正文

android蓝牙扫描(简单_startscan

startscan

蓝牙扫描有两种方式startScan和startLeScan,其中startLeScan官方@Deprecated

本文只做了蓝牙的扫描和显示。

1、配置蓝牙权限

  1. <uses-permission android:name="android.permission.BLUETOOTH" />
  2. <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
  3. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  4. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

2、xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical"
  7. tools:context=".MainActivity">
  8. <Button
  9. android:id="@+id/search"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:text="搜索"/>
  13. <ListView
  14. android:id="@+id/lv"
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content">
  17. </ListView>
  18. </LinearLayout>

3、第一种蓝牙扫描startScan

  1. public class MainActivity extends AppCompatActivity {
  2. private ArrayList<String> data = null;
  3. private ListView listView = null;
  4. private BluetoothAdapter bta = null;
  5. private BluetoothManager btm = null;
  6. private Button button = null;
  7. ArrayAdapter<String> adapter = null;
  8. private BluetoothLeScanner scanner = null;
  9. private Handler handler = new Handler();
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. // 获得数据
  15. this.data = new ArrayList<>();
  16. // 初始化控件
  17. this.initView();
  18. }
  19. private void initView(){
  20. // 获得button
  21. this.button = findViewById(R.id.search);
  22. this.button.setOnClickListener(new View.OnClickListener() {
  23. @Override
  24. public void onClick(View v) {
  25. // 扫描蓝牙
  26. scanBle();
  27. }
  28. });
  29. // 初始化ListView
  30. this.adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, this.data);
  31. this.listView = findViewById(R.id.lv);
  32. this.listView.setAdapter(adapter);
  33. }
  34. // 蓝牙扫描回调
  35. private ScanCallback newBtsc = new ScanCallback() {
  36. @Override
  37. public void onScanResult(int callbackType, ScanResult result) {
  38. super.onScanResult(callbackType, result);
  39. BluetoothDevice device = result.getDevice();
  40. // 更新数据
  41. data.add(device.getAddress()+""+device.getName());
  42. adapter.notifyDataSetChanged();
  43. }
  44. @Override
  45. public void onBatchScanResults(List<ScanResult> results) {
  46. super.onBatchScanResults(results);
  47. }
  48. @Override
  49. public void onScanFailed(int errorCode) {
  50. super.onScanFailed(errorCode);
  51. }
  52. };
  53. // 扫描蓝牙
  54. private void scanBle(){
  55. // 初始化蓝牙适配器
  56. btm = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
  57.   if(btm == null){
  58.   return;
  59.   }
  60. bta = btm.getAdapter();
  61. // 打开蓝牙
  62. if(bta == null || !bta.isEnabled()){
  63. Intent enable = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  64. startActivityForResult(enable, 1);
  65. }else{
  66. scanner = bta.getBluetoothLeScanner();
  67. scanner.startScan(newBtsc);
  68. // 设置扫描时间
  69. handler.postDelayed(new Runnable() {
  70. @Override
  71. public void run() {
  72. scanner.stopScan(newBtsc);
  73. }
  74. }, 8000);
  75. }
  76. }

}

3、第二种蓝牙扫描startLeScan

  1. public class MainActivity extends AppCompatActivity {
  2. private ArrayList<String> data = null;
  3. private ListView listView = null;
  4. private BluetoothAdapter bta = null;
  5. private BluetoothManager btm = null;
  6. private Button button = null;
  7. ArrayAdapter<String> adapter = null;
  8. private Handler handler = new Handler();
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_main);
  13. // 获得数据
  14. this.data = new ArrayList<>();
  15. // 初始化控件
  16. this.initView();
  17. }
  18. private void initView() {
  19. // 获得button
  20. this.button = findViewById(R.id.search);
  21. this.button.setOnClickListener(new View.OnClickListener() {
  22. @Override
  23. public void onClick(View v) {
  24. // 扫描蓝牙
  25. scanBle();
  26. }
  27. });
  28. // 初始化ListView
  29. this.adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, this.data);
  30. this.listView = findViewById(R.id.lv);
  31. this.listView.setAdapter(adapter);
  32. }
  33. private BluetoothAdapter.LeScanCallback oldBtsc = new BluetoothAdapter.LeScanCallback() {
  34. @Override
  35. public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
  36. data.add(device.getAddress() + "");
  37. adapter.notifyDataSetChanged();
  38. }
  39. };
  40. // 扫描蓝牙
  41. private void scanBle() {
  42. // 初始化蓝牙适配器
  43. btm = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
  44. if( btm == null){
  45.   return;
  46. }
  47. bta = btm.getAdapter();
  48. // 打开蓝牙
  49. if (bta == null || !bta.isEnabled()) {
  50. Intent enable = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  51. startActivityForResult(enable, 1);
  52. } else {
  53. bta.startLeScan(oldBtsc);
  54. // 设置扫描时间
  55. handler.postDelayed(new Runnable() {
  56. @Override
  57. public void run() {
  58. bta.stopLeScan(oldBtsc);
  59. }
  60. }, 8000);
  61. }
  62. }
  63. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/956129
推荐阅读
相关标签
  

闽ICP备14008679号