abe*_*ker 4 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
Run Code Online (Sandbox Code Playgroud)
永远不会调用回调.有谁有想法吗 ?
谢谢 !
小智 6
将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());
}
}
};
Run Code Online (Sandbox Code Playgroud)
并且还将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));
}
}
Run Code Online (Sandbox Code Playgroud)
http://developer.android.com/reference/android/bluetooth/BluetoothGatt.html#readRemoteRssi()
异步调用开始读取信号强度。
读取完成后回调。
阅读前需要连接
| 归档时间: |
|
| 查看次数: |
13209 次 |
| 最近记录: |