简体   繁体   English

Android为蓝牙RSSI返回“空”

[英]Android returning “null” for Bluetooth RSSI

I am developing a android application using Eclipse to return the RSSI values of a Bluetooth device. 我正在使用Eclipse开发一个android应用程序,以返回蓝牙设备的RSSI值。 I have modified the Android Bluetooth Chat example to fit my needs, but I am having problems returning the RSSI value. 我已经修改了Android蓝牙聊天示例以适合我的需求,但是在返回RSSI值时遇到了问题。 After hitting the scan button to discover nearby devices it returns the device name, device address, and is also suppose to return the RSSI value but instead it says null for the RSSI. 按下scan按钮以发现附近的设备后,它会返回设备名称,设备地址,并且还假定要返回RSSI值,但是它对RSSI表示为null

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

I haven't tried it myself, but maybe this SO answer will help: 我自己还没有尝试过,但是也许这样的答案会有所帮助:

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

Supposedly it is possible using the NDK. 可以使用NDK。 Good luck! 祝好运!

This is how I am getting RSSI 这就是我得到RSSI的方式

String deviceRSSI = (intent.getExtras()).get(BluetoothDevice.EXTRA_RSSI).toString(); 字符串deviceRSSI =(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)) {

            }
        }
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM