蓝牙LE Gatt连接Android 4.4读取实时RSSI

时间:2013-12-10 10:42:43

标签: java android bluetooth rssi gatt

我正在尝试编写一个小型Android应用程序(4.4),用于搜索多个蓝牙LE设备。一旦找到需要连接的每个设备,然后尽可能快地连续读取每个设备的RSSI。我一直试图让它与6个设备一起工作。我目前的代码如下:

public class BluetoothGatt extends Activity {
private BluetoothAdapter mBluetoothAdapter;
private static final int REQUEST_ENABLE_BT = 1;
int count = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    // Initializes Bluetooth adapter.
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();
    System.out.println("Adapter: " + mBluetoothAdapter);

    BTScanStart();
}

// Start the Bluetooth Scan
private void BTScanStart() {
    if (mBluetoothAdapter == null) {
        System.out.println("Bluetooth NOT supported. Aborting.");
        return;
    } else {
        if (mBluetoothAdapter.isEnabled()) {
            System.out.println("Bluetooth is enabled...");

            // Starting the device discovery 
            mBluetoothAdapter.startLeScan(mLeScanCallback); 

        }
    }
}

// Device scan callback.
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() { 
    public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {
        count++;
        System.out.println("Found " + count + ":" + device + " " + rssi + "db");
        device.connectGatt(null, false, mGattCallback);
        if (count > 5) mBluetoothAdapter.stopLeScan(mLeScanCallback);
    }
};

// Gatt Callback
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            System.out.println(gatt.getDevice() + ": Connected.. ");
            gatt.readRemoteRssi();
        }
        if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            System.out.println(gatt.getDevice() + ": Disconnected.. "); 
        }
    }

    public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
        System.out.println(gatt.getDevice() + " RSSI:" + rssi + "db "); 
        try {
            Thread.sleep(300); 
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        gatt.readRemoteRssi();  

        }
};

}

我遇到以下问题:

1)它成功连接到设备,但是在大约5秒后它们都会在'btm_sec_disconnected - 清除挂起标志'错误时断开连接。有没有让他们保持联系?

2)代码适用于单个设备,但是当使用多个设备时,只有一个设备定期打印RSSI更新,其他设备随机更新,有些设备根本不更新。

3)我不确定在调用device.connectGatt时应该提供什么上下文。

提前感谢您的想法!

2 个答案:

答案 0 :(得分:0)

如何使用startLeScan并获取rssi?

如果您的Android设备过滤掉设备(如nexus 7),您可以反复停止LeScan / startLeScan。

如果没有(比如三星s4和android 4.3),只要让它扫描一下,onLeScan(...)就会连续给每个设备的rssi。

答案 1 :(得分:0)

对于RSSI问题,我将回答Sam的回答(https://stackoverflow.com/a/20676785/4248895)。看来,对于您的用例,连续扫描应该足够了。如果可以避免,你不想添加连接的开销。

我将回答您的另一个问题,如果您 因某种原因需要连接到这些设备,那么您的稳定性问题可能与您同时连接和扫描的事实有关。一般来说,您不应同时执行任何两个gatt操作(扫描,连接,读取,写入等)。