Pregunta

I am developing an application where I have to connect to Bluetooth device on Android 4.3.

I have two java file,one is the DeviceControl extends Activity another one is BluetoothLeService extends Service.

I call the function in BluetoothLeService.java, and I want it return the rssi value to DeviceControl.java.

I use mRssi = intent.getStringExtra(EXTRAS_DEVICE_RSSI); in DeviceControl.java, and it will receive the value from rssi in BluetoothLeService.java.

But it always show the value is null in DeviceControl.java.

Why the value of mRssi is null??

This is the function in 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);
}

And the code in 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
    }
};

EDIT

the code in BluetoothLeService.java modify the code of onReadRemoteRssi and broadcastUpdate.

the code in DeviceControl.java add the code of receive value from intent in onReceive

I see the log from log file, it has show the log message at broadcastUpdate function in BluetoothLeService.java like the following:

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

But I didn't saw any log about action = EXTRAS_DEVICE_RSSI. I don't know is the sendBroadcast doesn't send or the onReceive doesn't called ???

¿Fue útil?

Solución

Try Like this, Add the Action before Broadcast.

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

Otros consejos

You are reading the intent that creates the activity, but not the broadcast intent, so of course it is null......

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top