搜索不到新设备设备

只有已配对设备

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

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


更多相关文章

  1. Android获取移动设备的IP地址
  2. linux无法识别android设备的解决办法
  3. android 4.0 蓝牙分析之二
  4. Android 获取手机的IMEI等设备信息
  5. adb devices 找不到设备的解决方法
  6. Android——禁止设备休眠
  7. android修改MTP及MTP(PC显示名称)、PTP盘符名、蓝牙名称
  8. Android 存储设备管理 -- Vold

随机推荐

  1. 【摘录】Android(安卓)Gesture Detector
  2. android:installLocation简析
  3. ImageView.setScaleType(ImageView.ScaleT
  4. 改变android程序head样式
  5. android 竖屏拍照旋转90度
  6. Android(安卓)API开发之TTS开发之Android
  7. 在android jni中使用log
  8. Android中判断网络功能是否可用
  9. Android学习日记(UI组件)
  10. android adb shell 命令大全