很简单。我唯一的代码是:
final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)){
mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.d(TAG,"Device "+mDevice.getName()+" Was Found");
}
}
};
但在配对设备时不会调用此功能。为什么?
答案 0 :(得分:4)
您好需要为广播接收器注册以下过滤器
ACTION_BOND_STATE_CHANGED
然后在onReceive
像这样添加它们
if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)){
mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (mDevice .getBondState() == BluetoothDevice.BOND_BONDED) {
//means device paired
}
}
int BOND_BONDED //Indicates the remote device is bonded (paired).
int BOND_BONDING //Indicates bonding (pairing) is in progress with the remote device.
int BOND_NONE //Indicates the remote device is not bonded (paired).
上次评论后修改
你还需要添加
IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_BOND_STATE_CHANGED);
registerReceiver(mReceiver, filter);
答案 1 :(得分:0)
请参考here您将找到如何连接列出蓝牙相关事件
答案 2 :(得分:0)
你是否在清单中添加了权限?
<uses-permission android:name="android.permission.BLUETOOTH" />
答案 3 :(得分:0)
从android 6.0及更高版本开始,您需要检查其他权限ACCESS_COARSE_LOCATION才能获取列表。 没有这个,带有ACTION_FOUND的onReceive方法将不会调用。