1

I want to continual read the rssi after gatt is connected.code like this:

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

the mGattCallback like this:

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

and onReadRemoteRssi does not work. ple tell me how to modify the code, or other solutions to read the rssi!

Thanks for your advise!

4

2 回答 2

4
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();
      }
   }
  ....
}
于 2014-01-03T07:22:53.307 回答
0
mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
Log.d(TAG, "Trying to create a new connection.");
boolean readRssiFlag = mBluetoothGatt.readRemoteRssi();

问题是您没有等到连接状态更改后才调用 readRemoteRssi。readRemoteRssi 调用的最早时间是在您收到 onConnectionStateChange 且 newState = 2(已连接)之后。

于 2013-11-17T08:11:42.300 回答