获取Android明确配对设备的RSSI值

时间:2017-07-07 20:12:44

标签: android bluetooth

我已经通过应用程序明确地将我的设备与Android手机配对。该设备是MI-Band 2,它使用MI-FIT应用程序配对。我目前正在使用此代码连接到蓝牙设备:

String action = intent.getAction();
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);

然而,问题是如果通过mi-fit app连接MI波段,我无法从MI-Band获得RSSI值。

是否有可能获得Android中明确配对设备的RSSI值?如果是这样,我做错了什么?

由于

编辑:似乎我无法获得任何连接设备的RSSI值。例如,如果Android手机A连接到Android手机B,那么在尝试通过手机A中的上述代码进行阅读时,我无法读取RSSI值。

1 个答案:

答案 0 :(得分:1)

如果您确定要连接的设备的蓝牙名称,也许您可​​以尝试使用BluetoothAdapter.LeScanCallback在扫描时引起注意。或者你真的需要告诉扫描或连接分开吗?

BluetoothManager bleManager = (BluetoothManager) getApplicationContext().getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter bleAdapter = bleManager.getAdapter();
bleAdapter.startLeScan(leScanCallback);// find match device
BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {

        @Override
        public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {
            if (device.getName() == null || !device.getName().equals("your bluetooth device name")) {
                return;
            }
            // here you can get rssi of specified bluetooth device if it's available.
            // and try to connect it programmatically.
            connect(device.getAddress());
        }
    };