我正在尝试创建一个应用程序,允许两个设备使用wifi直接连接,并以JSON文件格式从一个设备向另一个设备发送消息,然后将该文件解析为文本视图。到目前为止,我并不关心wifi部分,因为我知道这很困难,我想从项目的“简单”部分开始。到目前为止我有这个代码。
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONObject;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class MainActivity extends AppCompatActivity {
EditText edtx1;
TextView txvw1;
TextView txvw2;
TextView txvw3;
TextView txvw4;
TextView txvw5;
Button btn;
String Message;
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edtx1 = (EditText) findViewById(R.id.editText1);
txvw1 = (TextView) findViewById(R.id.textView1);
txvw2 = (TextView) findViewById(R.id.textView2);
txvw3 = (TextView) findViewById(R.id.textView3);
txvw4 = (TextView) findViewById(R.id.textView4);
txvw5 = (TextView) findViewById(R.id.textView5);
//txvw1.setText(edtx1.getText().toString());
makeJSON();
}
});
}
public JSONArray makeJSON() {
JSONArray jArr = new JSONArray();
JSONObject jObj = new JSONObject();
try {
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wInfo = wifiManager.getConnectionInfo();
String macAddress = wInfo.getMacAddress();
//Long tsLong = System.currentTimeMillis() / 1000;
//String ts = tsLong.toString();
jObj.put("Created_on:", currentDateTimeString);
jObj.put("Sent_by:", Build.MODEL);
jObj.put("MAC_Address:", macAddress);
jObj.put("Number_of_Hops:", 1);
jObj.put("Message:", edtx1.getText());
txvw1.setText(jObj.getString("Created_on:"));
txvw2.setText(jObj.getString("Sent_by:"));
txvw3.setText(jObj.getString("MAC_Address:"));
txvw4.setText(jObj.getString("Number_of_Hops:"));
txvw5.setText(jObj.getString("Message:"));
jArr.put(jObj);
} catch (Exception e) {
System.out.println("Error:" + e);
}
return jArr;
}
}
我想要一个按钮,当点击时,只需将edittext上的信息发送到名为message的json对象。所以最后我会有类似的东西
{
"Created on": Date,
"Sent By": "name",
"MAC Address": "FF:FF:FF:FF:FF:FF",
"Number of Hops": "This number I will figure it out later",
"Message": "Message"
}
然后将该文件发送到另一台设备。
更新:
我刚刚编辑了上面的代码,现在它有点工作,至少我认为是在做我想要的。这里的问题是设备名称和MAC地址。我知道现在无法在应用程序中获取MAC地址,但我可以填写该部分并使其静态。但是我怎样才能检索设备的名称,例如我的设备被称为“Daniel”我怎么能用代码检索?
答案 0 :(得分:0)
我没有对此进行测试,但这可能有所帮助。
以下是使用wifi-direct的官方文档,以备不时之需
你可以尝试将你的json对象作为一个字符串发送(这是我在发送到我的服务器时必须做的事情),另一方面就像标准的json一样解析它。
Uri.Builder builder = new Uri.Builder();
builder.appendQueryParameter("key_1", "value_1");
builder.appendQueryParameter("key_2", "value_2");
String json = builder.build().getEncodedQuery();
构建完类后,可以从中获取数据,然后使用.setText()进行设置
如果您需要有关如何操作的示例,请与我们联系。
---编辑---
到目前为止获取您的设备名称,我发现这段代码应该对您有用,只要您可以请求蓝牙权限。
将此添加到您的清单
<uses-permission android:name="android.permission.BLUETOOTH" />
并在您的代码中
public String getPhoneName(){
BluetoothAdapter myDevice = BluetoothAdapter.getDefaultAdapter();
String deviceName = myDevice.getName();
return deviceName;
}