Java 如何连续显示可编程设备的RSSI?
我正在尝试实现一个android应用程序,它可以扫描可扩展设备。因为我需要一些信标的距离信息,所以我想连续读取RSSI。目前我可以显示RSSI,但该值不变。应用程序读取该值一次,不更新该值。我使用来自gitHub的示例BLE应用程序作为基础 这是我的蓝牙设备:Java 如何连续显示可编程设备的RSSI?,java,android,bluetooth-lowenergy,rssi,Java,Android,Bluetooth Lowenergy,Rssi,我正在尝试实现一个android应用程序,它可以扫描可扩展设备。因为我需要一些信标的距离信息,所以我想连续读取RSSI。目前我可以显示RSSI,但该值不变。应用程序读取该值一次,不更新该值。我使用来自gitHub的示例BLE应用程序作为基础 这是我的蓝牙设备: class DeviceHolder{ BluetoothDevice device; int rssi; public DeviceHolder(BluetoothDevice device, int rss
class DeviceHolder{
BluetoothDevice device;
int rssi;
public DeviceHolder(BluetoothDevice device, int rssi){
this. device = device;
this.rssi = rssi;
}
}
列表适配器:
private class LeDeviceListAdapter extends BaseAdapter {
private ArrayList<BluetoothDevice> mLeDevices;
private ArrayList<DeviceHolder> mLeHolders;
private LayoutInflater mInflator;
public LeDeviceListAdapter() {
super();
mLeDevices = new ArrayList<BluetoothDevice>();
mLeHolders = new ArrayList<DeviceHolder>();
mInflator = DeviceScanActivity.this.getLayoutInflater();
}
public void addDevice(DeviceHolder deviceHolder) {
if(!mLeDevices.contains(deviceHolder.device)) {
mLeDevices.add(deviceHolder.device);
mLeHolders.add(deviceHolder);
}
}
BluetoothLeService活动中的Bluetooth GattCallback如下所示:
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED) {
intentAction = ACTION_GATT_CONNECTED;
mConnectionState = STATE_CONNECTED;
boolean rssiStatus = mBluetoothGatt.readRemoteRssi();
broadcastUpdate(intentAction);
Log.i(TAG, "Connected to GATT server.");
// Attempts to discover services after successful connection.
Log.i(TAG, "Attempting to start service discovery:" +
mBluetoothGatt.discoverServices());
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
intentAction = ACTION_GATT_DISCONNECTED;
mConnectionState = STATE_DISCONNECTED;
Log.i(TAG, "Disconnected from GATT server.");
broadcastUpdate(intentAction);
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
} else {
Log.w(TAG, "onServicesDiscovered received: " + status);
}
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
public void onReadRemoteRssi(BluetoothGatt gatt,
int rssi, int status){
super.onReadRemoteRssi(gatt, rssi, status);
if (status == BluetoothGatt.GATT_SUCCESS) {
Log.d(TAG, String.format("BluetoothGatt ReadRssi[%d]", rssi));
} else {
Log.w(TAG, "onReadRemoteRssi received: " + status);
}
broadcastUpdate(ACTION_RSSI_VALUE_READ, rssi);
}
};
private void broadcastUpdate(final String action) {
final Intent intent = new Intent(action);
sendBroadcast(intent);
}
private void broadcastUpdate(final String action, int rssi){
Log.d(TAG, "broadcastUpdate - rssi");
final Intent intent = new Intent(action);
intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_RSSI, rssi);
intent.setAction(ACTION_RSSI_VALUE_READ);
sendBroadcast(intent);
}
我查阅了互联网和Stackoverflow以寻求指导,但提供的解决方案对我并不适用。我使用的是三星Galaxy S5和安卓6.01。
是否有人提示如何连续显示未连接设备的RSSI(扫描时)?任何关于错误实现的提示都是受欢迎的
非常感谢你的帮助
萨尤斯
PS:onReadRemoteRssi的实现是因为我想在连接设备时读取RSSI。此功能在此时并不重要,只是很好地拥有它。您的应用程序只读取一次RSSI值,并且不会更新它,因为当您向适配器中添加新的
Bluetooth设备时,它会检查它是否已经包含RSSI值
if(!mLeDevices.contains(deviceHolder.device)) {
mLeDevices.add(deviceHolder.device);
mLeHolders.add(deviceHolder);
}
相反,我会创建一个映射
来更新设备的rssi值:mDeviceRssi.put(设备,rssi)
非常感谢您的回答!现在我明白了问题所在。我也明白我应该只更新列出的设备。由于我是android开发新手,如果您能提供更多关于Map的信息以及如何在我的代码中使用它,我将非常感激。一个有用的链接也就足够了。再次非常感谢!
if(!mLeDevices.contains(deviceHolder.device)) {
mLeDevices.add(deviceHolder.device);
mLeHolders.add(deviceHolder);
}