Я пытаюсь создать мобильное приложение p2p, используя Android. Я следую официальным документам, связанным здесь . Моя проблема заключается в создании раздела BroadcastReceiver
(который начинается с предложения: Now create a new BroadcastReceiver class that you'll use to listen for changes to the System's Wi-Fi P2P state
.
. Метод onReceive
непрерывно запускается с действием STATE_CHANGED
. Мое приложение написано так, что если в методе onReceive()
получено действие STATE_CHANGED
, Wi-Fi должен быть включен или выключен. У меня есть кнопка, которая должна вызывать это событие, но по какой-то причине он запускается непрерывно, когда приложение запускается - заставляет приложение колебаться между включением Wi-Fi и отключением Wi-Fi.
Ниже моя основная активность:
public class MainActivity extends AppCompatActivity {
Button btnOnOff, btnDiscover, btnSend;
ListView listView;
TextView read_msg_box, connectionStatus;
EditText writeMsg;
//For programmatically turning wifi on and off:
WifiManager wifiManager;
WifiP2pManager mManager;
WifiP2pManager.Channel mChannel;
BroadcastReceiver mReceiver;
IntentFilter mIntentFilter = new IntentFilter();
List<WifiP2pDevice> peers = new ArrayList<WifiP2pDevice>();
String[] deviceNameArray;
WifiP2pDevice[] deviceArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialWork();
exqListener();
}
private void exqListener() {
btnOnOff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(false);
btnOnOff.setText("ON");
} else {
wifiManager.setWifiEnabled(true);
btnOnOff.setText("OFF");
}
}
});
btnDiscover.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
connectionStatus.setText("Discovery Started");
}
@Override
public void onFailure(int reason) {
connectionStatus.setText("Discovery Failed");
}
});
}
});
}
private void initialWork() {
btnOnOff = (Button)findViewById(R.id.onOff);
btnDiscover = (Button)findViewById(R.id.discover);
btnSend = (Button)findViewById(R.id.sendButton);
listView = (ListView)findViewById(R.id.peerListView);
read_msg_box = (TextView) findViewById(R.id.readMsg);
connectionStatus = (TextView) findViewById(R.id.connectionStatus);
writeMsg = (EditText)findViewById(R.id.writeMsg);
wifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
mManager = (WifiP2pManager)getSystemService(Context.WIFI_P2P_SERVICE);
mChannel = mManager.initialize(this, getMainLooper(), null);
}
WifiP2pManager.PeerListListener peerListListener = new WifiP2pManager.PeerListListener() {
@Override
public void onPeersAvailable(WifiP2pDeviceList peerList) {
if(!peerList.getDeviceList().equals(peers)) {
peers.clear();
peers.addAll(peerList.getDeviceList());
deviceNameArray = new String[peerList.getDeviceList().size()];
deviceArray = new WifiP2pDevice[peerList.getDeviceList().size()];
int index = 0;
for(WifiP2pDevice device : peerList.getDeviceList()) {
deviceNameArray[index] = device.deviceName;
deviceArray[index] = device;
index++;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, deviceNameArray);
listView.setAdapter(adapter);
}
if(peers.size() == 0) {
Toast.makeText(getApplicationContext(), "No Device Found", Toast.LENGTH_LONG).show();
return;
}
}
};
@Override
protected void onResume() {
super.onResume();
mReceiver = new WiFiDirectBroadcastReceiver(mManager, mChannel, this);
registerReceiver(mReceiver, mIntentFilter);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(mReceiver);
}
}
Ниже мой класс BroadcastReceiver
:
public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {
private WifiP2pManager mManager;
private WifiP2pManager.Channel mChannel;
private MainActivity mActivity;
public WiFiDirectBroadcastReceiver(WifiP2pManager mManager, WifiP2pManager.Channel mChannel, MainActivity mActivity) {
this.mManager = mManager;
this.mChannel = mChannel;
this.mActivity = mActivity;
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
System.out.println("The following action was received: " + action);
if(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)){
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
//if wifi is enabled
if(state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
Toast.makeText(context, "Wifi is ON", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "Wifi is OFF", Toast.LENGTH_LONG).show();
}
} else if(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
if(mManager != null) {
mManager.requestPeers(mChannel, mActivity.peerListListener);
}
} else if(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
//do something
} else if(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
//do something
}
}
}
Вот операторы print, которые показывают, что метод onReceive()
в классе BroadcastReceiver
вызывается непрерывно:
02/28 09:36:59: Launching 'app' on <default>.
$ adb shell am start -n "com.example.wifi_p2p/com.example.wifi_p2p.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
$ adb shell am start -n "com.example.wifi_p2p/com.example.wifi_p2p.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 16127 on device 'emulator-5556'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
D/libEGL: Emulator has host GPU support, qemu.gles is set to 1.
W/RenderThread: type=1400 audit(0.0:33): avc: denied { write } for name="property_service" dev="tmpfs" ino=9369 scontext=u:r:untrusted_app:s0:c133,c256,c512,c768 tcontext=u:object_r:property_socket:s0 tclass=sock_file permissive=0 app=com.example.wifi_p2p
W/libc: Unable to set property "qemu.gles" to "1": connection failed; errno=13 (Permission denied)
D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so
D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
W/xample.wifi_p2: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
I/System.out: The following action was received: android.net.wifi.p2p.STATE_CHANGED
D/HostConnection: HostConnection::get() New Host Connection established 0xd37d1f00, tid 16180
D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_3_0
D/EGL_emulation: eglCreateContext: 0xea07f040: maj 3 min 0 rcv 3
D/EGL_emulation: eglMakeCurrent: 0xea07f040: ver 3 0 (tinfo 0xdee4ecf0)
W/Gralloc3: mapper 3.x is not supported
D/HostConnection: createUnique: call
HostConnection::get() New Host Connection established 0xd37d37b0, tid 16180
D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_3_0
D/eglCodecCommon: allocate: Ask for block of size 0x1000
allocate: ioctl allocate returned offset 0x3ffff4000 size 0x2000
D/EGL_emulation: eglMakeCurrent: 0xea07f040: ver 3 0 (tinfo 0xdee4ecf0)
Connected to process 13138 on device 'emulator-5554'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
[emulator-5554]: W/RenderThread: type=1400 audit(0.0:86): avc: denied { write } for name="property_service" dev="tmpfs" ino=7396 scontext=u:r:untrusted_app:s0:c137,c256,c512,c768 tcontext=u:object_r:property_socket:s0 tclass=sock_file permissive=0 app=com.example.wifi_p2p
[emulator-5554]: D/libEGL: Emulator has host GPU support, qemu.gles is set to 1.
[emulator-5554]: W/libc: Unable to set property "qemu.gles" to "1": connection failed; errno=13 (Permission denied)
[emulator-5554]: D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so
[emulator-5554]: D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
[emulator-5554]: D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
[emulator-5554]: W/xample.wifi_p2: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
[emulator-5554]: W/xample.wifi_p2: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
[emulator-5554]: I/System.out: The following action was received: android.net.wifi.p2p.STATE_CHANGED
[emulator-5554]: D/HostConnection: HostConnection::get() New Host Connection established 0xd782df00, tid 13184
[emulator-5554]: D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_3_0
[emulator-5554]: D/EGL_emulation: eglCreateContext: 0xe2e03f80: maj 3 min 0 rcv 3
[emulator-5554]: D/EGL_emulation: eglMakeCurrent: 0xe2e03f80: ver 3 0 (tinfo 0xee1c0c10)
[emulator-5554]: W/Gralloc3: mapper 3.x is not supported
[emulator-5554]: D/HostConnection: createUnique: call
[emulator-5554]: D/HostConnection: HostConnection::get() New Host Connection established 0xd782f7b0, tid 13184
[emulator-5554]: D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_3_0
[emulator-5554]: D/eglCodecCommon: allocate: Ask for block of size 0x1000
allocate: ioctl allocate returned offset 0x3ff96f000 size 0x2000
[emulator-5554]: D/EGL_emulation: eglMakeCurrent: 0xe2e03f80: ver 3 0 (tinfo 0xee1c0c10)
[emulator-5556]: I/OpenGLRenderer: Davey! duration=2314ms; Flags=1, IntendedVsync=1156460386919, Vsync=1156560386915, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=1156560672300, AnimationStart=1156560763000, PerformTraversalsStart=1156560832900, DrawStart=1156910561000, SyncQueued=1156920522900, SyncStart=1156926542500, IssueDrawCommandsStart=1156926733800, SwapBuffers=1158606730000, FrameCompleted=1158780416000, DequeueBufferDuration=115000, QueueBufferDuration=365000,
[emulator-5556]: I/Choreographer: Skipped 138 frames! The application may be doing too much work on its main thread.
[emulator-5556]: I/OpenGLRenderer: Davey! duration=2325ms; Flags=0, IntendedVsync=1156569082715, Vsync=1158869082623, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=1158875171400, AnimationStart=1158875274100, PerformTraversalsStart=1158875980300, DrawStart=1158878784600, SyncQueued=1158880673900, SyncStart=1158881629700, IssueDrawCommandsStart=1158881748500, SwapBuffers=1158892314200, FrameCompleted=1158895324000, DequeueBufferDuration=384000, QueueBufferDuration=339000,
[emulator-5556]: D/EGL_emulation: eglMakeCurrent: 0xea07f040: ver 3 0 (tinfo 0xdee4ecf0)
[emulator-5556]: I/OpenGLRenderer: Davey! duration=2371ms; Flags=1, IntendedVsync=1156569082715, Vsync=1158869082623, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=1158875171400, AnimationStart=1158875274100, PerformTraversalsStart=1158875980300, DrawStart=1158929883300, SyncQueued=1158930916600, SyncStart=1158932402600, IssueDrawCommandsStart=1158932520500, SwapBuffers=1158933620300, FrameCompleted=1158942419300, DequeueBufferDuration=1796000, QueueBufferDuration=5476000,
[emulator-5556]: I/System.out: The following action was received: android.net.wifi.p2p.STATE_CHANGED
[emulator-5556]: I/System.out: The following action was received: android.net.wifi.p2p.STATE_CHANGED
[emulator-5556]: I/System.out: The following action was received: android.net.wifi.p2p.STATE_CHANGED
[emulator-5556]: I/System.out: The following action was received: android.net.wifi.p2p.STATE_CHANGED
[emulator-5556]: I/System.out: The following action was received: android.net.wifi.p2p.STATE_CHANGED
[emulator-5556]: I/chatty: uid=10133(com.example.wifi_p2p) identical 2 lines
[emulator-5556]: I/System.out: The following action was received: android.net.wifi.p2p.STATE_CHANGED
[emulator-5556]: I/System.out: The following action was received: android.net.wifi.p2p.STATE_CHANGED
[emulator-5556]: I/System.out: The following action was received: android.net.wifi.p2p.STATE_CHANGED
[emulator-5556]: I/System.out: The following action was received: android.net.wifi.p2p.STATE_CHANGED
[emulator-5556]: I/System.out: The following action was received: android.net.wifi.p2p.STATE_CHANGED
[emulator-5556]: I/System.out: The following action was received: android.net.wifi.p2p.STATE_CHANGED
[emulator-5556]: I/System.out: The following action was received: android.net.wifi.p2p.STATE_CHANGED
[emulator-5556]: I/System.out: The following action was received: android.net.wifi.p2p.STATE_CHANGED
[emulator-5556]: I/System.out: The following action was received: android.net.wifi.p2p.STATE_CHANGED
[emulator-5556]: I/chatty: uid=10133(com.example.wifi_p2p) identical 2 lines
[emulator-5556]: I/System.out: The following action was received: android.net.wifi.p2p.STATE_CHANGED
[emulator-5556]: I/System.out: The following action was received: android.net.wifi.p2p.STATE_CHANGED
[emulator-5556]: I/System.out: The following action was received: android.net.wifi.p2p.STATE_CHANGED
[emulator-5556]: I/chatty: uid=10133(com.example.wifi_p2p) identical 1 line
[emulator-5556]: I/System.out: The following action was received: android.net.wifi.p2p.STATE_CHANGED
[emulator-5556]: I/System.out: The following action was received: android.net.wifi.p2p.STATE_CHANGED
[emulator-5556]: I/chatty: uid=10133(com.example.wifi_p2p) identical 2 lines
[emulator-5556]: I/System.out: The following action was received: android.net.wifi.p2p.STATE_CHANGED
[emulator-5556]: I/System.out: The following action was received: android.net.wifi.p2p.STATE_CHANGED
[emulator-5556]: I/System.out: The following action was received: android.net.wifi.p2p.STATE_CHANGED
[emulator-5556]: I/System.out: The following action was received: android.net.wifi.p2p.STATE_CHANGED
[emulator-5556]: I/System.out: The following action was received: android.net.wifi.p2p.STATE_CHANGED
[emulator-5554]: I/OpenGLRenderer: Davey! duration=1680ms; Flags=1, IntendedVsync=8115584185014, Vsync=8115634185012, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=8115635788300, AnimationStart=8115635879400, PerformTraversalsStart=8115635951400, DrawStart=8115757362400, SyncQueued=8115765324900, SyncStart=8115772996200, IssueDrawCommandsStart=8115775766400, SwapBuffers=8117035456600, FrameCompleted=8117272014500, DequeueBufferDuration=63000, QueueBufferDuration=577000,
[emulator-5556]: I/chatty: uid=10133(com.example.wifi_p2p) identical 2 lines
[emulator-5556]: I/System.out: The following action was received: android.net.wifi.p2p.STATE_CHANGED
[emulator-5556]: I/System.out: The following action was received: android.net.wifi.p2p.STATE_CHANGED
[emulator-5556]: I/chatty: uid=10133(com.example.wifi_p2p) identical 5 lines
[emulator-5556]: I/System.out: The following action was received: android.net.wifi.p2p.STATE_CHANGED
Где я go ошибся здесь?