android蓝牙扫描(简单

蓝牙扫描有两种方式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);
        }
    }
}
  • 5
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值