Android--BlueTooth蓝牙的简单使用

效果图:

代码:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button open;
    private Button search;
    private TextView show_search;
    private BluetoothAdapter defaultAdapter;
    private Button onBind;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(intent, 100);

        defaultAdapter = BluetoothAdapter.getDefaultAdapter();
    }

    private void initView() {
        open = (Button) findViewById(R.id.open);
        search = (Button) findViewById(R.id.search);
        show_search = (TextView) findViewById(R.id.show_search);

        open.setOnClickListener(this);
        search.setOnClickListener(this);
        onBind = (Button) findViewById(R.id.onBind);
        onBind.setOnClickListener(this);

        BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                //获取蓝牙的信号强度
                short aShort = intent.getExtras().getShort(BluetoothDevice.EXTRA_RSSI);
                //获取已经搜索到的蓝牙设备
                if(BluetoothDevice.ACTION_FOUND.equals(action)){
                    BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    //搜索到的设备不是已经绑定的蓝牙设备
                    if(bluetoothDevice.getBondState()!= BluetoothDevice.BOND_BONDED){
                        //没有绑定
                        show_search.append("蓝牙名称:"+bluetoothDevice.getName()+"\n"+"蓝牙地址:"+bluetoothDevice.getAddress()+"蓝牙的信号强度"+aShort);
                    }
                }else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
                    //defaultAdapter.cancelDiscovery();
                    //扫描完成
                    Toast.makeText(context, "蓝牙搜索完成", Toast.LENGTH_SHORT).show();
                }
            }
        };
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothDevice.ACTION_FOUND);//发现新设备

        intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        registerReceiver(broadcastReceiver,intentFilter);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.open:
                Toast.makeText(this, "蓝牙打开了么?" + defaultAdapter.enable(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.search:
                //点击搜索蓝牙,当蓝牙适配器每发现了新的蓝牙设备的时候就会发现一个新的action为BluetoothDevice.ACTION_FOUND的广播,这个时候我们只需要
                //广播接收机中进行拦截获取数据就可以拿到新发现的蓝牙设备的数据,比如蓝牙的名称和地址
                defaultAdapter.startDiscovery();
                break;
            case R.id.onBind:
                //点击获取已经绑定的蓝牙信息
                Set<BluetoothDevice> bondedDevices = defaultAdapter.getBondedDevices();//查看已经连接的蓝牙设备
                if(bondedDevices.size()>0){
                    for (BluetoothDevice bondedDevice : bondedDevices) {
                        //获取已经绑定的蓝牙的名称
                        String blueToothName = bondedDevice.getName();
                        //获取已经绑定的蓝牙的地址
                        String blueToothAddress = bondedDevice.getAddress();
                        show_search.append(blueToothName + "\n"+blueToothAddress);
                    }
                }
                break;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/ykx_1448488568/article/details/81697943