0

複数の Bluetooth 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 - Clearing Pending flag」エラーですべてが切断されます。それらを接続しておくための方法はありますか?

2) コードは 1 つのデバイスに対しては正常に動作しますが、複数のデバイスを使用する場合、1 つのデバイスのみが RSSI 更新を定期的に出力し、他のデバイスはランダムに更新し、一部のデバイスはまったく更新しません。

3) device.connectGatt を呼び出すときにどのコンテキストを指定すればよいかわかりません。

よろしくお願いします!

4

2 に答える 2

0

RSSI の問題については、Sam の回答 ( https://stackoverflow.com/a/20676785/4248895 ) を 2 番目に使用します。あなたのユースケースでは、継続的なスキャンで十分であるようです。回避できるのであれば、接続のオーバーヘッドを追加したくありません。

何らかの理由でこれらのデバイスに接続する必要がある場合、安定性の問題は、接続とスキャンを同時に行っているという事実に関連している可能性があるという点、他の質問に答えます. 一般的に言えば、2 つの gatt 操作 (スキャン、接続、読み取り、書き込みなど) を同時に実行するべきではありません。

于 2014-11-13T21:36:43.820 に答える