android - 如何一次读取已连接的多个设备的 RSSI 值?

标签 android bluetooth bluetooth-lowenergy android-4.3-jelly-bean rssi

我正在开发一个应用程序,我必须在 Android 4.3 上连接到蓝牙设备。

我可以连接到 BLE 设备并使用 BluetoothGatt.readRemoteRssi() 从设备读取 RSSI。

我想一次读取已连接的多个设备的 RSSI 但我只能读取我上次连接的设备的BLE设备的RSSI。

如果有两个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 ?

最佳答案

制作多个BluetoothGatt对象分别连接多个设备,并一一调用readRemoteRssi。

懒惰和糟糕的例子,你应该能够将这些BluetoothGatt对象放入数组中

BluetoothGatt mBluetoothGatt1 = device1.connectGatt(this, false, mGattCallback);
BluetoothGatt mBluetoothGatt2 = device2.connectGatt(this, false, mGattCallback);

关于android - 如何一次读取已连接的多个设备的 RSSI 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19673102/

相关文章:

android - 意外强制关闭

android 如何同时拥有edittext自动格式本身和可计算的值

java - 如何循环浏览 Android RecyclerView 中的项目?

java - 将 Google Play 服务添加到非 gradle Android Studio 项目?

Android - 连接成功后无法在 onScanResult 回调中找到设备

wpf - 在 Windows 10 上禁用绝对音量

ios - 设备 [iPhone] 如何与单个外围设备绑定(bind) [蓝牙低功耗设备]

ios - 写 CBCharacteristic 的问题

android - iOS 和 Android Max BLE 通告和扫描字节

android - 通过 Eclipse 查看内置 Android 应用程序的调用堆栈?