Android6.0蓝牙搜索设备
2017-06-15 23:22
330 查看
BluetoothAdapter
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); // null:表示不支持蓝牙 boolean enabled = mBluetoothAdapter.isEnabled(); // true:处于打开状态, false:处于关闭状态
调用系统提示框开启蓝牙
startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), 10001);
操作蓝牙别忘了声明权限
<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> //Android6.0以后蓝牙搜索 需要定位权限 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
开始搜索
//搜索蓝牙需要定位权限 if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(context, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1); } mBluetoothAdapter.startDiscovery();//开始搜索
定义广播接收者
Manifest中定义<receiver android:name=".MyReceiver" android:enabled="true" android:exported="true"> <intent-filter android:priority="1000"> <action android:name="android.bluetooth.device.action.FOUND"/> <action android:name="android.bluetooth.adapter.action.DISCOVERY_FINISHED"/> </intent-filter> </receiver>
MyReceiver 继承BroadcastReceiver
public class MyReceiver extends BroadcastReceiver { public MyReceiver() { } @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); if (device.getBondState() != BluetoothDevice.BOND_BONDED) { Log.e("tag",device.getName() + ":"+ device.getAddress()); } } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { Log.e("tag","搜索完成"); } } }
动态定义/注册
// 设置广播信息过滤 IntentFilter filter = new IntentFilter(); filter.addAction(BluetoothDevice.ACTION_FOUND);//每搜索到一个设备就会发送一个该广播 filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);//当全部搜索完后发送该广播 filter.setPriority(Integer.MAX_VALUE);//设置优先级 // 注册蓝牙搜索广播接收者,接收并处理搜索结果 this.registerReceiver(receiver, filter);
内部类的方式定义广播接收者
private final BroadcastReceiver receiver = 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); if (device.getBondState() != BluetoothDevice.BOND_BONDED) { Log.e("tag",device.getName() + ":"+ device.getAddress()); } } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { Log.e("tag","搜索完成"); } } };
相关文章推荐
- android6.0 蓝牙搜索不到设备 BluetoothAdapter.getDefaultAdapter().startDiscovery()不起作用
- Android6.0以上系统搜索不到 蓝牙BLE 设备问题
- Android6.0 蓝牙搜索不到设备原因
- Android6.0 蓝牙模块显示搜索到的设备的流程
- Android6.0 蓝牙搜索不到设备原因,MIUI权限申请机制
- 记Android6.0以上手机无法搜索到蓝牙设备的问题
- android搜索蓝牙时,区分所搜索到的设备是2.0还是BLE
- Android BLE与终端通信(二)——Android Bluetooth基础科普以及搜索蓝牙设备显示列表
- 如何修改蓝牙搜索设备的时间
- Android 蓝牙通信开发(一) 搜索蓝牙设备
- WM蓝牙开发--搜索周围的设备
- 代码流程分析二:Settings-蓝牙分析-搜索设备原理
- Android蓝牙搜索设备,向其发送数据并接收-刘宇
- Android 6.0 蓝牙搜索不到设备原因,MIUI权限申请机制方法
- 编程代码分析-使用winsock搜索蓝牙设备
- 关于安卓6.0以上系统中无法搜索到蓝牙设备的问题
- 怎么在搜索蓝牙4.0 设备中调用蓝牙2.0的搜索界面?(和UI关系更大,和蓝牙4.0关系不大)
- Android蓝牙搜索设备,向其发送数据并接收
- [Bluetooth]搜索一定范围内的远程蓝牙设备
- Android6.0蓝牙开发中获取附近低功耗蓝牙设备结果权限问题分析