Android 低功耗蓝牙 readRemoteRssi

标签 android bluetooth-lowenergy

我不知道如何让“onReadRemoteRssi”回调工作。

我的代码很简单:

final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();
    BluetoothGatt gatt;

    mBluetoothAdapter.startLeScan(new LeScanCallback() {

        @Override
        public void onLeScan(BluetoothDevice device, int rssi, byte[] record) {
            gatt = device.connectGatt(getApplicationContext(), false, new BluetoothGattCallback() {
                @Override
                public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
                    super.onReadRemoteRssi(gatt, rssi, status);
                    Log.d(TAG, "rssi is : " + rssi);
                }
            });
        }
    });

    gatt.readRemoteRssi(); //returns true

永远不会调用回调。 有人有什么主意吗 ?

谢谢!

最佳答案

将readRemoteRssi()放在BluetoothGattCallback的回调onConnectionStateChange()中

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);
            // Attempts to discover services after successful connection.
            Log.i(TAG, "Attempting to start service discovery:" +
                    mBluetoothGatt.discoverServices());
        }
    }
};

并且还将 onReadRemoteRssi 放在 BluetoothGattCallback 函数中‍‍‍

@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status){
    if (status == BluetoothGatt.GATT_SUCCESS) {
        Log.d(TAG, String.format("BluetoothGatt ReadRssi[%d]", rssi));
    }
}

关于Android 低功耗蓝牙 readRemoteRssi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19365495/

相关文章:

android - Ble-getting error code - 128 while writing to characteristic 写入特征

ios - L2CAP IOS + Linux (Bluez)

android - GCM android 推送通知始终显示旧消息。收到的 Intent 不正确

Android返回键/按钮,如何完美使用?

Android 应用程序构建因 gradle 而失败

ios - 为什么无法发现特定的 BLE 外设服务?

java - 将 Android 自定义 ImageView 转换为 Xamarin.Android

android - 如果 Activity 已经在前面,则通知中的 PendingIntent 不会触发

android - 警报通知配置文件的 UUID - ANP(在 GATT 中),在低功耗蓝牙中

android - 处理程序后执行队列的顺序不正确