我想修改BluetoothLeGatt項目作爲Android Studio的示例,以便每次讀取特性時將RSSI值發送回主要活動以進行顯示。我創建了一個新的意圖過濾器,但接收方從未收到意圖。我已經嘗試將它添加到清單文件,但也沒有工作。sendBroadcast不能使用自定義意圖過濾器
這裏的服務代碼:
public class BluetoothLeService extends Service {
...
public final static String ACTION_GATT_CONNECTED =
"com.example.bluetooth.le.ACTION_GATT_CONNECTED";
public final static String ACTION_GATT_DISCONNECTED =
"com.example.bluetooth.le.ACTION_GATT_DISCONNECTED";
public final static String ACTION_GATT_SERVICES_DISCOVERED =
"com.example.bluetooth.le.ACTION_GATT_SERVICES_DISCOVERED";
public final static String ACTION_DATA_AVAILABLE =
"com.example.bluetooth.le.ACTION_DATA_AVAILABLE";
public final static String RSSI_DATA_AVAILABLE =
"com.example.bluetooth.le.RSSI_DATA_AVAILABLE";
public final static String EXTRA_DATA =
"com.example.bluetooth.le.EXTRA_DATA";
public final static String EXTRA_RSSI =
"com.example.bluetooth.le.EXTRA_RSSI";
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
mBluetoothGatt.readRemoteRssi();
}
}
@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
final Intent intent = new Intent();
if (status == BluetoothGatt.GATT_SUCCESS) {
intent.putExtra(EXTRA_RSSI, String.valueOf(rssi));
intent.setAction(RSSI_DATA_AVAILABLE);
sendBroadcast(intent);
}
}
,並且活動:
public class DeviceControlActivity extends Activity {
...
private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
mConnected = true;
updateConnectionState(R.string.connected);
invalidateOptionsMenu();
} else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
mConnected = false;
updateConnectionState(R.string.disconnected);
invalidateOptionsMenu();
clearUI();
} else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
// Show all the supported services and characteristics on the user interface.
displayGattServices(mBluetoothLeService.getSupportedGattServices());
} else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
displayData(intent.getStringExtra(BluetoothLeService.EXTRA_DATA));
} else if (BluetoothLeService.RSSI_DATA_AVAILABLE.equals(action)) {
updateRssi(intent.getStringExtra(BluetoothLeService.EXTRA_RSSI));
}
}
};
...
}
答:
我不得不行動加入到這個方法在活動:
private static IntentFilter makeGattUpdateIntentFilter() {
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothLeService.ACTION_GATT_CONNECTED);
intentFilter.addAction(BluetoothLeService.ACTION_GATT_DISCONNECTED);
intentFilter.addAction(BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED);
intentFilter.addAction(BluetoothLeService.ACTION_DATA_AVAILABLE);
intentFilter.addAction(BluetoothLeService.RSSI_DATA_AVAILABLE);
return intentFilter;
}
你在哪裏註冊的廣播接收器? – Blackbelt
不確定。 Android noob。它不會顯示在示例代碼中的任何清單文件中。 – JBaczuk
您是否創建了IntentFilter? – Blackbelt