【问题标题】:Android returning "null" for Bluetooth RSSIAndroid 为蓝牙 RSSI 返回“null”
【发布时间】:2012-04-13 02:12:14
【问题描述】:

我正在使用 Eclipse 开发一个 android 应用程序来返回蓝牙设备的 RSSI 值。我已经修改了 Android 蓝牙聊天示例以满足我的需要,但是在返回 RSSI 值时遇到了问题。在点击scan 按钮以发现附近的设备后,它会返回设备名称、设备地址,并且还假设返回 RSSI 值,但它显示的是 null 用于 RSSI。

if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress() + getRssi());

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        // When discovery finds a device
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
           // Get the Bluetooth RSSI
            short Rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
            // If it's already paired, skip it, because it's been listed already
            // Added getRssi()
            if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress() + getRssi());
            }
        // When discovery is finished, change the Activity title
        } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            setProgressBarIndeterminateVisibility(false);
            setTitle(R.string.select_device);
            if (mNewDevicesArrayAdapter.getCount() == 0) {
                String noDevices = getResources().getText(R.string.none_found).toString();
                mNewDevicesArrayAdapter.add(noDevices);
            }
        }
    }
};

【问题讨论】:

    标签: java android eclipse


    【解决方案1】:

    我自己没有尝试过,但也许这个 SO 答案会有所帮助:

    https://stackoverflow.com/a/2361630/831918

    据说可以使用 NDK。祝你好运!

    【讨论】:

      【解决方案2】:

      这就是我获取 RSSI 的方式

      字符串设备RSSI = (intent.getExtras()).get(BluetoothDevice.EXTRA_RSSI).toString();

      private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver() {
      
              @Override
              public void onReceive(Context context, Intent intent) {
                  // TODO Auto-generated method stub
                  String action = intent.getAction();
                  if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                      BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                      String deviceRSSI = (intent.getExtras()).get(BluetoothDevice.EXTRA_RSSI).toString();
      
                      btArrayAdapter.add(device.getName() + "\n" + device.getAddress() + "\n" + deviceRSSI);
                      btArrayAdapter.notifyDataSetChanged();
                  }
      
                  if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED)) {
      
                  } else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
      
                  }
              }
          };
      

      【讨论】:

        猜你喜欢
        • 2023-03-19
        • 1970-01-01
        • 1970-01-01
        • 2013-04-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多