2013-10-14 270 views
3

我無法弄清楚如何獲得'onReadRemoteRssi'回調工作。Android藍牙低功耗readRemoteRssi

我的代碼是非常簡單的:

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 

回調永遠不會被調用。 有沒有人有任何想法?

謝謝!

+0

你解決這個問題? – phcaze

回答

0
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(deviceAddress); 
BluetoothGatt bluetoothGatt = getBluetoothGatt(device); 

if (bluetoothGatt == null) { 
    return false; 
} 
boolean rdRemoteRssi = bluetoothGatt.readRemoteRssi(); 
Log.d(FTAG, "BluetoothGatt readRemoteRssi : " + rdRemoteRssi); 
return true; 

它將調用

參考onReadRemoteRssi撥打電話back.Need此API之前進行連接。

+0

這是什麼getBluetoothGatt()方法,哪個類有這個? – AAnkit

4

將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)); 
    } 
} 
+0

通過以上解決方案仍然沒有獲取onreadRemoteRssi回調來獲取rssi值。請建議解決方案。 – Aman