如何连续显示BLE设备的RSSI?

How to display the RSSI of a BLE device continously?

我正在尝试实现一个扫描 BLE 设备的 android 应用程序。因为我需要信标的某种距离信息,所以我想连续读取 RSSI。目前我可以显示 RSSI,但值不会改变。应用程序只读取一次值并且不会更新该值。我使用 gitHub 中的示例 BLE 应用程序作为基础。

这是我的蓝牙设备:

  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);
        }
    }

和扫描回调:

private BluetoothAdapter.LeScanCallback mLeScanCallback =
        new BluetoothAdapter.LeScanCallback() {

            @Override
            public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
                DeviceHolder deviceHolder = new DeviceHolder(device,rssi);
                runOnUiThread(new DeviceAddTask( deviceHolder ) );
            }
        };

class DeviceAddTask implements Runnable {
    DeviceHolder deviceHolder;

    public DeviceAddTask( DeviceHolder deviceHolder ) {
        this.deviceHolder = deviceHolder;
    }

    public void run() {
        mLeDeviceListAdapter.addDevice(deviceHolder);
        mLeDeviceListAdapter.notifyDataSetChanged();
    }
}

BluetoothLeService Activity 中的蓝牙 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);
}

我在互联网和 Whosebug 上查找指导,但提供的解决方案对我不起作用。我正在使用装有 Android 6.01 的三星 Galaxy S5。 有没有人知道如何为未连接的设备(扫描时)连续显示 RSSI?也欢迎任何有关错误实施的提示。

非常感谢您的帮助!

赛尤斯

PS:实现了onReadRemoteRssi,因为我也想在连接设备时读取RSSI。这个功能目前并不重要,只有拥有才好。

您的应用程序只读取一次 RSSI 值并且不会更新它,因为当您将新的 BluetoothDevice 添加到您的适配器时,它会检查它是否已经包含一个。

    if(!mLeDevices.contains(deviceHolder.device)) {
        mLeDevices.add(deviceHolder.device);
        mLeHolders.add(deviceHolder);
    }

相反,我会创建一个 Map<BluetoothDevice,Integer>,您可以在其中更新设备的 rssi 值:mDeviceRssi.put(device, rssi)