2

Android 4.3 で Bluetooth デバイスに接続する必要があるアプリケーションを開発しています。

BluetoothGatt.readRemoteRssi() を使用して、BLE デバイスに接続し、デバイスから RSSI を読み取ることができます。

また、接続した複数のデバイスの RSSI を一度に読みたいのですが、前回接続したデバイスの BLE デバイスの RSSI しか読み込めません。

2 つの BLE デバイス A と B がある場合、デバイス Aに接続し、そこから RSSI を読み取りました。デバイス Bに接続した後、デバイス Bから RSSI を読み取ることができます。ただし、デバイス Aの RSSI は読み取らず、デバイス Bからの RSSI のみを読み取ります。

Main.javaには、接続したすべてのデバイスがリストされています。

リスト上のデバイスをクリックすると、デバイス名とアドレスがDeviceControl.javaに送信されます。

final Intent qintent = new Intent(this, DeviceControl.class);
        devicelist.setOnItemClickListener(new OnItemClickListener() {

                    @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            HashMap<String, Object> select = (HashMap<String, Object>) devicelist.getItemAtPosition(arg2);
            String name = (String) select.get("name");
            String address = (String) select.get("address");
            qintent.putExtra(DeviceControl.EXTRAS_DEVICE_NAME, name);
            qintent.putExtra(DeviceControl.EXTRAS_DEVICE_ADDRESS, address);
            startActivity(qintent);
        }
    });

そして、DeviceControl.javaはBluetoothLeService.javaを呼び出してデバイスに接続します。

private final ServiceConnection mServiceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName componentName, IBinder service) {
        // TODO Auto-generated method stub
        mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
        if(!mBluetoothLeService.initialize()) {
            Log.e(TAG, "Unable to initialize Bluetooth");
            finish();
        }
        registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
        mBluetoothLeService.connect(mDeviceAddress);
    }
    @Override
    public void onServiceDisconnected(ComponentName componentName) {
        // TODO Auto-generated method stub
        mBluetoothLeService = null;
    }
};

BluetoothLeService.javaがデバイスに接続します。

public boolean connect(final String address) {
    if (mBluetoothAdapter == null || address == null) {
        Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
        return false;
    }
    if(mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress) 
            && mBluetoothGatt != null) {
        Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection.");
        if(mBluetoothGatt.connect()) {
            mConnectionState = STATE_CONNECTING;                
            return true;
        }else {
            return false;
        }
    }
    final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
    if(device == null) {
        Log.w(TAG, "Device not found.  Unable to connect");
        return false;
    }
    mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
    Log.d(TAG, "Try to create a new connection");
    mBluetoothDeviceAddress = address;
    mConnectionState =STATE_CONNECTING;
    return true;
}

デバイスに接続したら、readRemoteRssi を使用してデバイスから RSSI を読み取ることができます。

public void readRemoteRssi() {
    mBluetoothGatt.readRemoteRssi();
}

しかし、最後に接続したデバイスの RSSI しか読み取れませんでした。

ログを見ると、常に、最後に接続したデバイスにonCharacteristicWritereadRemoteRssi()が送信されます。

RSSI を読み取る前、または CharacteristicWrite 値を最初のデバイスに書き込む前に、GATT を再接続するか、デバイスを最初のアドレスに再接続する必要がありますか??

接続したすべてのデバイスの RSSI を読み取る他の方法はありますか??

4

1 に答える 1

2

複数のデバイスを個別に接続するには、複数の BluetoothGatt オブジェクトを作成し、readRemoteRssi を 1 つずつ呼び出します。

怠惰で悪い例です。これらの BluetoothGatt オブジェクトを配列に入れることができるはずです

BluetoothGatt mBluetoothGatt1 = device1.connectGatt(this, false, mGattCallback);
BluetoothGatt mBluetoothGatt2 = device2.connectGatt(this, false, mGattCallback);
于 2013-10-31T02:15:32.843 に答える