通过WiFi连接Java DatagramSocket的问题

时间:2015-11-26 19:02:24

标签: java android udp

以下代码的目的是通过WiFi从Android应用程序向TI CC3200 Launchpad发送数据报包。 我在建立与Launchpad的连接时遇到了问题。我尝试用笔记本电脑和应用程序测试程序,但我的Wireshark没有显示任何数据包。 有什么建议为什么我的代码没有按预期工作?

BR

EDIT1:

IP是正确的。 我收到:绑定失败:EINVAL(无效参数)。

清单:

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
 <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>
 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
 <uses-permission android:name="android.permission.INTERNET"></uses-permission>

 <uses-feature android:name="android.hardware.wifi.direct"/>

EDIT2:

堆栈跟踪:

。 。

I / AudioPolicyManager(287):getAudioPolicyConfig:audioParam; outDevice

V / AudioPolicyManager(287):getNewOutputDevice()选择设备0

V / AudioPolicyManager(287):### curdevice:2

I / AudioPolicyManager(287):getAudioPolicyConfig:audioParam; outDevice

V / AudioPolicyManager(287):getNewOutputDevice()选择设备0

V / AudioPolicyManager(287):### curdevice:2

D / CustomFrequencyManagerService(751):releaseDVFSLockLocked:获取锁定类型frm列表:DVFS_MIN_LIMIT频率:2457600 uid:1000 pid:751标签:ROTATION_BOOSTER @ 32

I / AudioPolicyManager(287):getAudioPolicyConfig:audioParam; outDevice

V / AudioPolicyManager(287):getNewOutputDevice()选择设备0

V / AudioPolicyManager(287):### curdevice:2

I / AudioPolicyManager(287):getAudioPolicyConfig:audioParam; outDevice

V / AudioPolicyManager(287):getNewOutputDevice()选择设备0

V / AudioPolicyManager(287):### curdevice:2

I / AudioPolicyManager(287):getAudioPolicyConfig:audioParam; outDevice

V / AudioPolicyManager(287):getNewOutputDevice()选择设备0

V / AudioPolicyManager(287):### curdevice:2

I / AudioPolicyManager(287):getAudioPolicyConfig:audioParam; outDevice

V / AudioPolicyManager(287):getNewOutputDevice()选择设备0

V / AudioPolicyManager(287):### curdevice:2

D / StatusBar.NetworkController(1036):refreshViews connected = {wifi} level = 3 combinedSignalIconId = 0x7f020495 / com.android.systemui:drawable / stat_sys_wifi_signal_4 mobileLabel = bob wifiLabel =&#34; Kugelfahrzeug&#34; xxxxXXXXxxxxXXXX emergencyOnly =假combinedLabel =&#34; Kugelfahrzeug&#34; XXXXXXXXXXXXXXXX mAirplaneMode =假mDataActivity = 0 mPhoneSignalIconId = 0x7f020415 / com.android.systemui:抽拉/ stat_sys_signal_3_auto_rotate mQSPhoneSignalIconId = 0x7f020113 / com.android.systemui:抽拉/ ic_qs_signal_3 mDataDirectionIconId =为0x0 /(空)mDataSignalIconId = 0x7f020415 / com.android.systemui:抽拉/ stat_sys_signal_3_auto_rotate mDataTypeIconId = 0x7f0202ac / com.android.systemui:抽拉/ stat_sys_data_connected_3g mQSDataTypeIconId = 0x7f020114 / com.android.systemui:抽拉/ ic_qs_signal_3g mNoSimIconId =为0x0 /(空)mWifiIconId = 0x7f020495 /com.android.systemui:drawable/stat_sys_wifi_signal_4 mQSWifiIconId = 0x7f02012b / com.android.systemui:drawable / ic_qs_wifi_4 mWifiActivityIconId = 0x7f020468 / com.android。 systemui:drawable / stat_sys_signal_no_inout mBluetoothTetherIconId = 0x1080907 / android:drawable / stat_sys_tether_bluetooth

D / StatusBar.NetworkController(1036):refreshSignalCluster - setNWBoosterIndicators(false)

D / StatusBar.NetworkController(1036):applyOpen

I / AudioPolicyManager(287):getAudioPolicyConfig:audioParam; outDevice

V / AudioPolicyManager(287):getNewOutputDevice()选择设备0

V / AudioPolicyManager(287):### curdevice:2

D / StatusBar.NetworkController(1036):refreshSignalCluster - setNWBoosterIndicators(false)

D / StatusBar.NetworkController(1036):applyOpen

D / StatusBar.NetworkController(1036):refreshSignalCluster - setNWBoosterIndicators(false)

D / StatusBar.NetworkController(1036):applyOpen

D / StatusBar.NetworkController(1036):refreshSignalCluster - setNWBoosterIndicators(false)

D / StatusBar.NetworkController(1036):applyOpen

D / TaskPersister(751):removeObsoleteFile:删除文件= 24_task.xml

D / TaskPersister(751):removeObsoleteFile:删除文件= 24_task_thumbnail.png

I / AudioPolicyManager(287):getAudioPolicyConfig:audioParam; outDevice

V / AudioPolicyManager(287):getNewOutputDevice()选择设备0

V / AudioPolicyManager(287):### curdevice:2

I / art(751):显式并发标记扫描GC释放17486(1092KB)AllocSpace对象,5(1360KB)LOS对象,25%免费,45MB / 61MB,暂停1.720ms总计181.936ms

I / AudioPolicyManager(287):getAudioPolicyConfig:audioParam; outDevice

V / AudioPolicyManager(287):getNewOutputDevice()选择设备0

V / AudioPolicyManager(287):### curdevice:2

I / AudioPolicyManager(287):getAudioPolicyConfig:audioParam; outDevice

V / AudioPolicyManager(287):getNewOutputDevice()选择设备0

V / AudioPolicyManager(287):### curdevice:2

I / AudioPolicyManager(287):getAudioPolicyConfig:audioParam; outDevice

V / AudioPolicyManager(287):getNewOutputDevice()选择设备0

V / AudioPolicyManager(287):### curdevice:2

I / AudioPolicyManager(287):getAudioPolicyConfig:audioParam; outDevice

V / AudioPolicyManager(287):getNewOutputDevice()选择设备0

V / AudioPolicyManager(287):### curdevice:2

I / AudioPolicyManager(287):getAudioPolicyConfig:audioParam; outDevice

V / AudioPolicyManager(287):getNewOutputDevice()选择设备0

V / AudioPolicyManager(287):### curdevice:2

I / AudioPolicyManager(287):getAudioPolicyConfig:audioParam; outDevice

V / AudioPolicyManager(287):getNewOutputDevice()选择设备0

V / AudioPolicyManager(287):### curdevice:2

I / AudioPolicyManager(287):getAudioPolicyConfig:audioParam; outDevice

V / AudioPolicyManager(287):getNewOutputDevice()选择设备0

V / AudioPolicyManager(287):### curdevice:2

I / WifiStateMachine(751):CMD_RSSI_POLL:calculateWifiScore in!

I / AudioPolicyManager(287):getAudioPolicyConfig:audioParam; outDevice

V / AudioPolicyManager(287):getNewOutputDevice()选择设备0

V / AudioPolicyManager(287):### curdevice:2

I / WifiStateMachine(751):CMD_RSSI_POLL:calculateWifiScore out!

I / WifiStateMachine(751):CMD_RSSI_POLL:out!

EDIT3:

问题位于主网络呼叫中(用

修复)
 new Thread(new Runnable() 
 { 
    @Override 
    public void run() 
    {   
      (Code below)
    }
 });

App现在运行正常!

代码:

    public void runJavaSocket() throws IOException 
    {   
    try 
    {
        DSocket = new DatagramSocket(null);
        DSocket.bind(new InetSocketAddress("192.168.173.2", 4242));
        DSocket.connect(new InetSocketAddress("192.168.173.1", 4242));
        String send_x = Float.toString(round_x);
        byte[] buffer = send_x.getBytes(); 
        byte [] IP = {(byte) 192,(byte) 168,(byte) 173, (byte) 1};
        InetAddress address = InetAddress.getByAddress(IP);
        int serverSocket = 4242;
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length, address, serverSocket);
        DSocket.send(packet);
        DSocket.close();     
    }
    catch (Exception e) 
    {
        DSocket.close();
    }

}

1 个答案:

答案 0 :(得分:0)

尝试使用此代码,并确保您的两个设备在同一网络上运行:

.entry-content {
    display: inline-block;
}