搜索不到新设备设备

只有已配对设备

(已解决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

<?xml version="1.0" encoding="utf-8"?>                                                                                                    



更多相关文章

  1. Android获取移动设备的IP地址
  2. android 4.0 蓝牙分析之二
  3. Android(安卓)获取手机的IMEI等设备信息
  4. Android获取物理地址(支持5.0~10.0)
  5. android 安装卸载程序
  6. adb devices 找不到设备的解决方法
  7. 系出名门Android(4) - 活动(Activity), 服务(Service), 广播(Bro
  8. Android应用程序中创建一个http服务器
  9. Android——禁止设备休眠

随机推荐

  1. Android常用开源项目
  2. Android中字符串片段高亮
  3. 通读Android文档系列 TWO
  4. js判断当前设备是 Android(安卓)还是 iOS
  5. Android之使用RatingBar设置游戏难度并保
  6. Android(安卓)Canvas drawArc方法介绍
  7. 混合开发-H5 调用Android(安卓)的相册和
  8. Android内存泄漏终极解决篇
  9. Android(安卓)tips2
  10. android源码解析--switch