我正在开发一个应用程序,我必须在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 接收值。
但它始终显示该值在设备控制.java中为 null。
为什么 mRssi 的值为空?
这是蓝牙服务中的功能.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);
}
以及设备控制中的代码.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
}
};
编辑
蓝牙服务中的代码.java修改 onReadRemoteRssi 和 broadcastUpdate 的代码。
设备控制中的代码.java在接收中添加来自意图的接收值代码
我从日志文件中看到日志,它在蓝牙服务中的广播更新功能上显示了日志消息.java如下所示:
Log.v(TAG, "rssi action = " + action);
Log.v(TAG, "RSSI = " + rssi);
但是我没有看到任何关于操作= EXTRAS_DEVICE_RSSI的日志。我不知道是sendBroadcast没有发送还是onReceive没有调用???
尝试 像这样,在广播前添加动作。
final Intent intent = new Intent();
intent.putExtra(DeviceControl.EXTRAS_DEVICE_RSSI, rssi);
intent.setAction(YOUR_INTENT_FILETR);
sendBroadcast(intent);
您正在读取创建活动的意图,而不是广播意图,所以它当然是空的......