0

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

我有两个 java 文件,一个是DeviceControl extends Activity另一个是BluetoothLeService extends Service.

我在BluetoothLeService.java中调用该函数,我希望它将rssi值返回给DeviceControl.java

mRssi = intent.getStringExtra(EXTRAS_DEVICE_RSSI);DeviceControl.java中使用,它将从BluetoothLeService.java中的rssi接收值。

但它总是显示DeviceControl.java中的值为 null

为什么 mRssi 的值为空?

这是BluetoothLeService.java中的函数

        public class BluetoothLeService extends Service {

public final static String ACTION_RSSI_VALUE_READ = "com.example.bluetooth.le.ACTION_RSSI_VALUE_READ";

                                .
                                .
                                .
            public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
    broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}

public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
    //super.onReadRemoteRssi(gatt, rssi, status);
    if(status == BluetoothGatt.GATT_SUCCESS) {
        final Intent intent = new Intent();
        intent.setAction(ACTION_RSSI_VALUE_READ);               
        broadcastUpdate(ACTION_RSSI_VALUE_READ, rssi);
            //call and send the intent and rssi to broadcastUpdate function
    }
}

//broadcastUpdate function receive the intent and rssi value, and transmit to the DeviceControl.java by using sendBroadcast.
private void broadcastUpdate(final String action, final int rssi) {
    final Intent intent = new Intent(action);
    intent.putExtra(DeviceControl.EXTRAS_DEVICE_RSSI, rssi);
    Log.v(TAG, "rssi action = " + action);
    Log.v(TAG, "RSSI = " + rssi);
    sendBroadcast(intent);
}

private void broadcastUpdate(final String action) {
    final Intent intent = new Intent(action);
    sendBroadcast(intent);
}

private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) {

    final Intent intent = new Intent(action);

    final byte[] data = characteristic.getValue();
    if (data != null && data.length > 0) {
        final StringBuilder stringBuilder = new StringBuilder(data.length);
        for(byte byteChar : data)
            stringBuilder.append(String.format("%02X", byteChar));
        intent.putExtra(EXTRA_DATA, new String(data) + "\n" + stringBuilder.toString());
    }
    sendBroadcast(intent);
}

以及DeviceControl.java中的代码

    

public class DeviceControl extends Activity {

public static final String EXTRAS_DEVICE_RSSI = "DEVICE_RSSI";
private String mRssi;

private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        final String action = intent.getAction();

         final Intent intent = getIntent();
                 mRssi = intent.getStringExtra(EXTRAS_DEVICE_RSSI); //Use **mRssi** to receive the value from BluetoothLeService.java 
                 Log.d(TAG, "mRssi = " + mRssi);

         if(BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
            mConnect = true;
            updateConnectionState(R.string.connected);
            invalidateOptionsMenu();
        } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
            mConnect = false;
            updateConnectionState(R.string.disconnected);
            invalidateOptionsMenu();
        }  else if (BluetoothLeService.ACTION_RSSI_VALUE_READ.equals(action)) {
            mRssi = intent.getStringExtra(EXTRAS_DEVICE_RSSI);
            Log.v(TAG, "mRssi == :" + mRssi);
        }//if the action is EXTRAS_DEVICE_RSSI, receive the rssi value
    }
};

编辑

BluetoothLeService.java中的代码 修改了onReadRemoteRssibroadcastUpdate的代码。

DeviceControl.java中的代码在onReceive中 添加从intent接收值的代码

我从日志文件中看到了日志,它在BluetoothLeService.java中的 broadcastUpdate 函数中显示了日志消息,如下所示:

Log.v(TAG, "rssi action = " + action);
Log.v(TAG, "RSSI = " + rssi);

但我没有看到任何关于action = EXTRAS_DEVICE_RSSI的日志。我不知道是 sendBroadcast 没有发送还是onReceive没有调用???

4

2 回答 2

1

试试这样,在广播之前添加动作。

final Intent intent = new Intent();
intent.putExtra(DeviceControl.EXTRAS_DEVICE_RSSI, rssi);
intent.setAction(YOUR_INTENT_FILETR);  
sendBroadcast(intent);
于 2013-10-23T09:01:34.020 回答
0

您正在阅读创建活动的意图,而不是广播意图,所以它当然是空的......

于 2013-10-24T02:04:40.357 回答