【问题标题】:How to read the rssi value of multiple device at one time where I have connected?如何在我连接的地方一次读取多个设备的 rssi 值?
【发布时间】:2013-11-09 11:40:28
【问题描述】:

我正在开发一个应用程序,我必须连接到 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?

【问题讨论】:

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


    【解决方案1】:

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

    懒惰的坏例子,你应该可以把那些 BluetoothGatt 对象放入一个数组中

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

    【讨论】:

    • 如何让多个BluetoothGatt对象分别连接多个设备?
    • 查看编辑,从2个独立的mac地址获取device1和device2
    • 感谢您的回复!我已经将单独的mac地址存储到数组中。但是mBluetoothGatt1和device1的类型是BluetoothGatt。怎么能转阵法??它喜欢 BluetoothGatt mBluetoothGatt[1] 吗?抱歉...我是新手。
    • 我是否需要使用 mBluetoothAdapter1..getRemoteDevice(address); ??或者我只需要制作多个 BluetoothGatt 对象??
    • 感谢您的指导!!我制作了多个 BluetoothGatt 对象并且它工作!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-02
    • 1970-01-01
    • 2016-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多