Android 蓝牙配对

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener , ScanBlueCallBack{
    Button button, button1;
    BluetoothAdapter bluetoothAdapter;
    BlueToothBroadCast blueToothBroadCast;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initview();
        initBlueToothAdapter();
        blueToothBroadCast=new BlueToothBroadCast(this);
        IntentFilter filter1 = new IntentFilter(android.bluetooth.BluetoothAdapter.ACTION_DISCOVERY_STARTED);
        IntentFilter filter2 = new IntentFilter(android.bluetooth.BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        IntentFilter filter3 = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(blueToothBroadCast,filter1);
        registerReceiver(blueToothBroadCast,filter2);
        registerReceiver(blueToothBroadCast,filter3);


    }

    private void initview() {
        button=findViewById(R.id.button1);
        button1=findViewById(R.id.button2);
        button.setOnClickListener(this);
        button1.setOnClickListener(this);
    }

    /**
     * 打开蓝牙设备
     */
    private void initBlueToothAdapter() {
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (!bluetoothAdapter.isEnabled()) {
            //弹出对话框提示用户是后打开
            Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enabler, 1);
            //不做提示,强行打开
            // mAdapter.enable();
        }
    }

    @Override
    public void onClick(View view) {
        int id=view.getId();
        switch (id){
            case R.id.button1:
                bluetoothAdapter.startDiscovery();
                break;
            case R.id.button2:
                break;
        }
    }




import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;


/**
 * Created by Administrator on 2019/5/6 0006.
 */

public class BlueToothBroadCast extends BroadcastReceiver {
    private ScanBlueCallBack callBack;

    public BlueToothBroadCast(ScanBlueCallBack callBack){
        this.callBack = callBack;
    }

    //广播接收器,当远程蓝牙设备被发现时,回调函数onReceiver()会被执行
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        System.out.println("action:" + action);
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        switch (action){
            case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
                callBack.onScanStarted();
                break;

            case BluetoothDevice.ACTION_FOUND:
                System.out.println("name=="+device.getName()+",地址=="+device.getAddress());
                if(device.getBondState()==BluetoothDevice.BOND_BONDED)
                {
                    System.out.println("已配对设备=="+device.getName()+",地址=="+device.getAddress());
                }else if(device.getBondState()!=BluetoothDevice.BOND_BONDED)
                {
                    System.out.println("未配对设备=="+device.getName()+",地址=="+device.getAddress());
                }
                callBack.onScanning(device);
                break;
            case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
                System.out.println("搜索完成");
                callBack.onScanFinished();
                break;
        }
    }


}




    @Override
    public void onScanStarted() {

    }

    @Override
    public void onScanning(BluetoothDevice device) {

    }

    @Override
    public void onScanFinished() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(blueToothBroadCast);
    }
}
发布了180 篇原创文章 · 获赞 27 · 访问量 15万+

猜你喜欢

转载自blog.csdn.net/sinat_28238111/article/details/89915496