如何在我连接的同时读取多个设备的rssi值?

时间:2013-10-30 03:04:27

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

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

我可以使用BluetoothGatt.readRemoteRssi()连接到BLE设备并从设备读取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。

当我看到日志时,它总是将 onCharacteristicWrite readRemoteRssi()发送到我已连接的最后一台设备。

在我想要读取RSSI或将CharacteristicsWrite值写入第一个设备之前,我应该重新连接GATT还是将设备重新连接到第一个地址?

是否有其他方法可以读取我连接的所有设备的RSSI?

1 个答案:

答案 0 :(得分:1)

使多个BluetoothGatt对象分别连接多个设备,并逐个调用readRemoteRssi。

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

BluetoothGatt mBluetoothGatt1 = device1.connectGatt(this, false, mGattCallback);
BluetoothGatt mBluetoothGatt2 = device2.connectGatt(this, false, mGattCallback);
相关问题