Używanie klasy do śledzenia Runtime show, że metoda onReceive()
nie wywołuje, dlaczego?Rejestracja odbiornika dynamicznie nie działa - BluetoothDevice.ACTION_FOUND
Rejestracja odbiornik transmisji dynamicznie
private void discoverDevices() {
Log.e("MOHAB","BEFORE ON RECEIVE");
mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Log.e("MOHAB","ON RECEIVE");
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Add the name and address to an array adapter to show in a ListView
Bluetooth b = new Bluetooth(device.getName(),device.getAddress());
list.add(b);
}
}
};
Log.e("MOHAB","create intentFilter");
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
}
Dlaczego twój odbiornik transmisji został zainicjowany w funkcji? –
ten kod, aby odkryć pobliskie urządzenia bluetooth .. umieszczam ten kod w zdarzeniu onClick(), co jest właściwym sposobem? – Error
BroadcastReceiver są wywoływane, gdy są rozróżniane przez nadawane wiadomości.zainicjuj go na zewnątrz na metodęCreate, a nie w swojej funkcji. –