android - Android Studio:将数据从 android 传递到 php
问题描述
我正在研究信标项目以找出蓝牙的 uuid、地址、名称、主要、次要、rssi 等。我正在获取所有这些值。但问题是我必须将这些数据推送到 php myadmin 服务器,我用过用于服务器的数据库和 php 文件的 volley 库..我也会发布我的活动代码和 php 文件...请找出解决方案..提前谢谢..
蓝牙活动.java
private ListView listview;
private static final String LOG_TAG = "MainActivity";
private String url = "http://localhost/blue/testing.php";
private BluetoothManager btManager;
private BluetoothAdapter btAdapter;
private Handler scanHandler = new Handler();
private int scan_interval_ms = 5000;
private boolean isScanning = false;
private ArrayAdapter adapter;
private BluetoothAdapter bluetoothAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// init BLE
listview = (ListView) findViewById(R.id.listView);
adapter = new ArrayAdapter
(this, android.R.layout.simple_list_item_1);
listview.setAdapter(adapter);
btManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
btAdapter = btManager.getAdapter();
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
scanHandler.post(scanRunnable);
//new scanHandler(getApplicationContext(),getMainLooper());
}
private void scanHandler(Context applicationContext, Looper mainLooper) {
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
// ------------------------------------------------------------------------
// public usage
// ------------------------------------------------------------------------
private Runnable scanRunnable = new Runnable() {
@Override
public void run() {
if (isScanning) {
if (btAdapter != null) {
btAdapter.stopLeScan(leScanCallback);
}
} else {
if (btAdapter != null) {
btAdapter.startLeScan(leScanCallback);
}
}
isScanning = !isScanning;
scanHandler.postDelayed(this, scan_interval_ms);
}
};
// ------------------------------------------------------------------------
// Inner classes
// ------------------------------------------------------------------------
int rssi1,major1,minor1;
private BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) {
/* scanHandler.getLooper();*/
int startByte = 2;
rssi1=rssi;
boolean patternFound = false;
while (startByte <= 5) {
if (((int) scanRecord[startByte + 2] & 0xff) == 0x02 && //Identifies an iBeacon
((int) scanRecord[startByte + 3] & 0xff) == 0x15) { //Identifies correct data length
patternFound = true;
break;
}
startByte++;
}
if (patternFound) {
//Convert to hex String
byte[] uuidBytes = new byte[16];
System.arraycopy(scanRecord, startByte + 4, uuidBytes, 0, 16);
String hexString = bytesToHex(uuidBytes);
//UUID detection
final String uuid = hexString.substring(0, 8) + "-" +
hexString.substring(8, 12) + "-" +
hexString.substring(12, 16) + "-" +
hexString.substring(16, 20) + "-" +
hexString.substring(20, 32);
// major
final int major = (scanRecord[startByte + 20] & 0xff) * 0x100 + (scanRecord[startByte + 21] & 0xff);
major1=major;
// minor
final int minor = (scanRecord[startByte + 22] & 0xff) * 0x100 + (scanRecord[startByte + 23] & 0xff);
minor1=minor;
adapter.add("Name: "+ device.getName() + "\n" + "BluetoothAddress: "+ device.getAddress()+"\n" +"UUID:"+ uuid +"\n" + "MajorKey: "+ major +"\n" +"MinorKey: "+ minor + "\n"+ "RSSI: "+ rssi);
Log.i(LOG_TAG, "UUID: " + uuid +"\n"+ "nmajor: " + major + "\n"+"nminor" + minor+"\n" +"rssi1" + rssi +"\n"+device.getAddress()+"\n"+device.getName());
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(MainActivity.this, "dfdsfsd"+response, Toast.LENGTH_SHORT).show();
Log.i("My success",""+response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "my error :"+error, Toast.LENGTH_LONG).show();
Log.i("My error",""+error);
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> map = new HashMap<String, String>();
map.put("bluetoothname",device.getName());
map.put("bluetoothaddress",device.getAddress());
map.put("bluetoothUUID",uuid);
map.put("bluetoothMajorKey",String.valueOf(major1));
map.put("bluetoothMinorKey",String.valueOf(minor1));
map.put("bluetoothrssi",String.valueOf(rssi1));
return map;
}
};
}
}
};
/**
* bytesToHex method
*/
static final char[] hexArray = "0123456789ABCDEF".toCharArray();
private static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
}
解决方案
我也有同样的情况。如果你在 pc 上运行你的 php 服务器,你可以通过 ifconfig 找到你的 pc 的 IP 地址。我通常发现它是例如 192.168.43.1,所以当我在模拟器中运行 android 时,我使用 URL 作为 http://192.168.43.1/xxx.php 即使模拟器在同一台电脑上运行,“localhost”也不起作用。这也适用于我用手机测试 android 应用程序时,只要它们在同一个网络中。
推荐阅读
- node.js - 如果发送的对象中不存在一个字段,则会失败给出此错误“[parsing_exception] 没有为文本查询指定文本”
- nim-lang - Nim 计算多个线程
- typescript - 带有 nx-Monorepo 问题的 TypeORM 迁移
- mysql - 如何显示来自 MySQL GROUP 和 HAVING 聚合查询的所有行?
- python - 美汤返回空数组
- php - 如何使用具有最小文件日期(filemtime)的PHP glob()?
- android - 如何为不同的应用设置 FLAG_KEEP_SCREEN_ON?
- angular - 错误“无法绑定到‘parentData’,因为它不是‘ng-template’的已知属性
- html - 如何使跨越图像或 div 的文本透明或不同颜色?
- php - PHP - 计算 CSV 文件中的不同值