很简单的。我唯一的代码是这样的:
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");
}
}
};
Run Code Online (Sandbox Code Playgroud)
但这在我配对设备时不会被调用。为什么呢?
嗨,您需要注册您的广播接收器以使用以下过滤器
ACTION_BOND_STATE_CHANGED
Run Code Online (Sandbox Code Playgroud)
然后在onReceive
像这样添加他们
if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)){
mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (mDevice .getBondState() == BluetoothDevice.BOND_BONDED) {
//means device paired
}
}
Run Code Online (Sandbox Code Playgroud)
在此处了解更多信息蓝牙设备页面
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).
Run Code Online (Sandbox Code Playgroud)
编辑:最后评论之后
您还需要添加
IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_BOND_STATE_CHANGED);
registerReceiver(mReceiver, filter);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2658 次 |
最近记录: |