Android 不连接蓝牙设备获取信号强度
2019-07-23 本文已影响0人
Cedric_h
原文:https://blog.csdn.net/uyy203/article/details/50771105
要拿到蓝牙信号指示值 rssi 分为两个步骤。
1.在oncreate方法里面增加 注册扫描广播
public void onCreate(Bundle savedInstanceState) {
// 注册开始发现广播
//当设备扫描得到蓝牙设备时触发,如果需要在连接设备后触发可使用ACTION_ACL_CONNECTED
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(mReceiver, filter);
}
2.新建BroadcastReceiver广播对象,并实现里面的onreceive方法,在onreceive得到rssi(信号强度)
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//当设备开始扫描时。
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
//从Intent得到blueDevice对象
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
//信号强度。
short rssi = intent.getExtras().getShort( BluetoothDevice.EXTRA_RSSI);
}
}
}
};