如何将值从Service传输到Activity?

时间:2013-10-23 08:50:30

标签: android android-activity android-service bluetooth-lowenergy android-4.3-jelly-bean

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

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

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

我在 DeviceControl.java 中使用mRssi = intent.getStringExtra(EXTRAS_DEVICE_RSSI);,它将从 BluetoothLeService.java 中的 rssi 中获取值。< / p>

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

为什么mRssi的值为null ??

这是 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 中的代码 修改 onReadRemoteRssi broadcastUpdate 的代码。

DeviceControl.java 中的代码 在 onReceive

中添加来自intent的接收值代码

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

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

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

2 个答案:

答案 0 :(得分:1)

尝试这样,在广播之前添加操作

final Intent intent = new Intent();
intent.putExtra(DeviceControl.EXTRAS_DEVICE_RSSI, rssi);
intent.setAction(YOUR_INTENT_FILETR);  
sendBroadcast(intent);

答案 1 :(得分:0)

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