Android 搜索不到蓝牙设备

搜索不到新设备设备

只有已配对设备

(已解决http://mp.blog.csdn.net/postedit/78717311)

package com.joey.qzhu.simplebluetooth;

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.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Set;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private static final int REQUEST_ENABLE_BT = 0x01;

    private Button butOpen;
    private Button butSearch;
    private TextView tvState;
    private TextView tvUnPaired;
    private TextView tvPaired;
    private BluetoothAdapter mBluetoothAdapter;
    private Toast toast;


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

        butOpen = (Button) this.findViewById(R.id.but_open_bt);
        butSearch = (Button) this.findViewById(R.id.but_search_bt);
        tvState = (TextView) this.findViewById(R.id.tv_state);
        tvUnPaired = (TextView) this.findViewById(R.id.tv_unpaired);
        tvPaired = (TextView) this.findViewById(R.id.tv_paired);
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        // 验证设备是否支持蓝牙
        if (mBluetoothAdapter == null) {
            showToast("设备不支持蓝牙");
            return;
        }

        // 注册广播接收器
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        registerReceiver(mReceiver, filter);

        // 打开蓝牙功能
        butOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mBluetoothAdapter.isEnabled()) {
                    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
                }
            }
        });

        // 搜索蓝牙设备
        butSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mBluetoothAdapter.isEnabled()) {
                    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
                }
                boolean startDiscovery = mBluetoothAdapter.startDiscovery();
                tvState.setText(startDiscovery ? "正在搜索设备..." : "搜索失败");
            }
        });

        // 获取已配对设备
        Set pairedDevices = mBluetoothAdapter.getBondedDevices();
        if (pairedDevices.size() > 0) {
            for (BluetoothDevice device : pairedDevices) {
                tvPaired.setText("");
                tvPaired.append("\n" + device.getName() + " " + device.getAddress() + " " + device.getBondState());
            }
        }
    }

    // 新增广播接收器
    private 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);
                tvUnPaired.setText("");
                tvUnPaired.append("\n" + device.getName() + " " + device.getAddress() + " " + device.getBondState());
                showToast("发现设备");
            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                tvState.setText("搜索完成");
            }
        }
    };

    // 显示消息
    private void showToast(String message) {
        toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

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

AndroidManifest.xml




    
    
    
    
    


    
        
            
                

                
            
        
    


Android 搜索不到蓝牙设备_第1张图片Android 搜索不到蓝牙设备_第2张图片


你可能感兴趣的:(Android)