其他分享
首页 > 其他分享> > 简单的android 蓝牙发现附近的设备和以连接的设备

简单的android 蓝牙发现附近的设备和以连接的设备

作者:互联网

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="获取以配对的设备"
       />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bt_scan"
        android:text="扫描设备"

        />

</LinearLayout>

android java

package com.yifei.myapplication;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.Tag;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.Set;

public class MainActivity extends AppCompatActivity {

    private static final int REQUEST_CODE = 10;
    private BluetoothAdapter mBluetoothAdapter;
    private static final String TAG = "MainActivity11";
    private Button pardevicebtn ;
    private Button scanBtn;


    private  final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if(BluetoothDevice.ACTION_FOUND.equals(action)){
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                Log.d(TAG, device.getName()+"\n"+device.getAddress());
            }else if(action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)){
                Log.d(TAG, "discovery done");
            }
        }
    };




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

        mBluetoothAdapter =BluetoothAdapter.getDefaultAdapter();
        if(mBluetoothAdapter==null){
            Log.e(TAG, "Device not support BlueTooth");
        }else {
            Toast.makeText(this,"设备支持蓝牙",Toast.LENGTH_LONG).show();
        }

        pardevicebtn=findViewById(R.id.btn);
        scanBtn  = findViewById(R.id.bt_scan);

        pardevicebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Set<BluetoothDevice> pairedDevices=mBluetoothAdapter.getBondedDevices();
                for(BluetoothDevice device:pairedDevices){
                    Log.d(TAG, "Device name="+device.getName()+" address="+device.getAddress());
                }
            }
        });
        scanBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mBluetoothAdapter.isDiscovering()){
                    mBluetoothAdapter.cancelDiscovery();
                }
                mBluetoothAdapter.startDiscovery();
            }
        });

        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothDevice.ACTION_FOUND);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        registerReceiver(mReceiver,filter);

    }

    @Override
    protected void onStart() {
        super.onStart();
        if(!mBluetoothAdapter.isEnabled()){
            Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(intent,REQUEST_CODE);
        }else {
            Toast.makeText(this,"蓝牙已经开启",Toast.LENGTH_LONG).show();
        }

        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(mReceiver,filter);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,
                                    @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode==REQUEST_CODE){
            if(resultCode==RESULT_CANCELED){
                Toast.makeText(this,"蓝牙已取消",Toast.LENGTH_LONG).show();
            }else {
                Toast.makeText(this,"蓝牙已经开启",Toast.LENGTH_LONG).show();
            }
        }



    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mReceiver);
    }
}

标签:Toast,BluetoothDevice,void,mBluetoothAdapter,蓝牙,import,android,设备
来源: https://blog.csdn.net/weixin_41069726/article/details/94169506