android获取bluetooth的信号强度(RSSI)
16lz
2021-01-23
要拿到蓝牙信号指示值 rssi 分为两个步骤。
1.在oncreate方法里面增加 注册扫描广播
public void onCreate(Bundle savedInstanceState) {
// 注册开始发现广播。
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
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);
}
}
}
};
1.在oncreate方法里面增加 注册扫描广播
public void onCreate(Bundle savedInstanceState) {
// 注册开始发现广播。
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
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);
}
}
}
};
更多相关文章
- Android五大布局对象
- Android 随时随地键值对存储对象解决方案
- android对象池之Message
- Android Telephony —— 手机信号实时变化源码分析过程记录
- Android的绘制文本对象FontMetrics的介绍及绘制文本
- Android JNI和NDK学习(09)--JNI实例二 传递类对象
- Android 通过intent 传递对象
- 服务器向Android写一个对象