0

我正在尝试编写一个搜索多个蓝牙 LE 设备的小型 android 应用程序(4.4)。一旦找到需要连接的每个设备,然后尽可能快地不断读取每个设备的 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 时应该提供什么上下文。

提前感谢您的想法!

4

2 回答 2

0

对于 RSSI 问题,我将支持 Sam 的回答 ( https://stackoverflow.com/a/20676785/4248895 )。对于您的用例,似乎连续扫描就足够了。如果可以避免,您不想增加连接的开销。

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

于 2014-11-13T21:36:43.820 回答
0

使用 startLeScan 并获取 rssi 怎么样?

如果您的 android 设备过滤了 ble 设备(如 nexus 7),您可以重复 stopLeScan/startLeScan。

如果没有(如 samsung s4 与 android 4.3),让它扫描,onLeScan(...) 将连续给每个设备的 rssi。

于 2013-12-19T08:35:21.063 回答