onReadRemoteRssi不起作用

onReadRemoteRssi不起作用

我想在gatt连接后继续读取rssi.code像这样:

final BluetoothDevice device = mBluetoothAdapter
        .getRemoteDevice(address);
if (device == null) {
    Log.w(TAG, "Device not found.  Unable to connect.");
    return false;
}

mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
Log.d(TAG, "Trying to create a new connection.");
boolean readRssiFlag = mBluetoothGatt.readRemoteRssi();
Log.i(TAG,"readRssiFlag: "+readRssiFlag);


mGattCallback像这样:

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
        ....
        @Override
        public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
            Log.i(TAG,"rssi = " + rssi);
        }
        ...
    };


并且onReadRemoteRssi不起作用。
请告诉我如何修改代码或其他解决方案以读取rssi!

感谢您的意见!

最佳答案

private mRssiTimer;
....
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback()
{
    @Override
   public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState)
   {
       if (newState == BluetoothProfile.STATE_CONNECTED)
       {
          TimerTask task = new TimerTask()
         {
            @Override
            public void run()
            {
               mBluetoothGatt.readRemoteRssi();
            }
         };
         mRssiTimer = new Timer();
         mRssiTimer.schedule(task, 1000, 1000);
      }
      else if (newState == BluetoothProfile.STATE_DISCONNECTED)
      {
         mRssiTimer.cancel();
      }
   }
  ....
}

09-28 06:25