多模信号获取

  • L6_116737
    了解作者
  • 59.8KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-28 09:56
    上传日期
以前刚学android的时候写的一个多模信号获取。有蓝牙信号强度获取,wifi信号强度获取,大家参考一下,修改一下就能作为自己研究需要。
ExMultiScanner.rar
内容介绍
package com.qfyh.multi; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.wifi.ScanResult; import android.net.wifi.WifiManager; import android.os.Bundle; import android.os.Handler; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MultiScannerActivity extends Activity { /** Called when the activity is first created. */ private EditText ex;//x编辑 private EditText ey;//y编辑 private EditText ez;//z编辑 private EditText et;//t编辑 private Button btnScan, btnDel,btnExit;//按钮 private TextView tv;//列表显示 private String filename="exmultiscansql"; private WifiManager wifi; private BroadcastReceiver receiver; private List<ScanResult> results; private List<String> lstDevices = new ArrayList<String>(); private List<MultiSignal> lms = new ArrayList<MultiSignal>(); private List<MultiSignal> boothlist = new ArrayList<MultiSignal>(); //private List<MultiSignal> msresult; //private HashMap<String,ArrayList<Integer>> hm=new HashMap<String,ArrayList<Integer>>(); private BluetoothAdapter btAdapt; //private BluetoothSocket btSocket; //private String timeStamp; private String state; private String buffer; private int count; private int countNum; private long cInterval=1000;//扫描时间间隔 private Handler handler = new Handler(); private Runnable fresh = new Runnable() { public void run() { try { if(!state.equals("MultiScannerActivity-1")) setTitle(state); else{ Toast.makeText(MultiScannerActivity.this, "扫描完成", 1000).show(); setTitle("MultiScannerActivity"); } buffer = new String(); tv.setText(""); Log.i("------------", "------------write---------"); for(int j=0;j<lms.size();j++){ lms.get(j).setLEVEL(-100); } for (int i = 0; i < results.size(); i++) { /*if(hm.get(results.get(i).BSSID.toUpperCase())==null){ ArrayList<Integer> rr=new ArrayList<Integer>(); hm.put(results.get(i).BSSID.toUpperCase(), rr); } hm.get(results.get(i).BSSID.toUpperCase()).add(results.get(i).level); */ tv.append("wifi "+ results.get(i).SSID + " " + results.get(i).BSSID.toUpperCase() + " " + results.get(i).level + "\n"); /*buffer = buffer +"wifi "+ ex.getText().toString()+" "+ey.getText().toString()+" " +ez.getText().toString()+" "+ results.get(i).SSID + " " + results.get(i).BSSID.toUpperCase() + " " + results.get(i).level + "\r\n";*/ for(int j=0;j<lms.size();j++){ if(results.get(i).BSSID.toUpperCase().equals(lms.get(j).getMAC())){ lms.get(j).setLEVEL(results.get(i).level); break; } } } for(int i=0;i<boothlist.size();i++){ tv.append("bluetooth "+lstDevices.get(i)+"\n"); for(int j=0;j<lms.size();j++){ if(boothlist.get(i).getMAC().equals(lms.get(j).getMAC())){ lms.get(j).setLEVEL(boothlist.get(i).getLEVEL()); break; } } //buffer += "bluetooth "+lstDevices.get(i)+"\r\n"; } buffer = buffer + "" + countNum + " "+ex.getText().toString()+" "+ey.getText().toString(); countNum++; for(int j=0;j<lms.size();j++){ buffer = buffer + " " + lms.get(j).getLEVEL(); } buffer = buffer + "\r\n"; FileWriter fw = new FileWriter("/sdcard/data/" + filename + ".txt", true); fw.append(buffer); fw.close(); lstDevices.clear(); boothlist.clear(); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnScan=(Button)this.findViewById(R.id.btnScan); btnScan.setOnClickListener(new ClickEvent()); btnDel=(Button)this.findViewById(R.id.btnStop); btnDel.setOnClickListener(new ClickEvent()); btnExit=(Button)this.findViewById(R.id.btnExit); btnExit.setOnClickListener(new ClickEvent()); ex=(EditText)this.findViewById(R.id.ex); ey=(EditText)this.findViewById(R.id.ey); ez=(EditText)this.findViewById(R.id.ez); et=(EditText)this.findViewById(R.id.et); tv=(TextView)this.findViewById(R.id.TextView01); try { File f = new File("/sdcard/data"); if (f.isDirectory()) { } else { f.mkdir(); } FileWriter fw = new FileWriter("/sdcard/data/" + filename + ".txt", true); fw.write(""); fw.close(); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { File f = new File("/sdcard/data"); if (f.isDirectory()) { } else { f.mkdir(); } FileReader fr = new FileReader("/sdcard/data/mconfig.txt"); BufferedReader rd=new BufferedReader(fr); String str=rd.readLine() ; while(str!=null){ MultiSignal ms = new MultiSignal(); ms.setMAC(str); lms.add(ms); str=rd.readLine() ; } rd.close(); fr.close(); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } wifi = (WifiManager) getSystemService(WIFI_SERVICE); btAdapt = BluetoothAdapter.getDefaultAdapter();// 初始化本机蓝牙功能 btAdapt.enable(); receiver = new BroadcastReceiver() { @SuppressWarnings("static-access") @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub //timeStamp = new Timestamp(new Date().getTime()).toString(); String action = intent.getAction(); /*Bundle b = intent.getExtras(); Object[] lstName = b.keySet().toArray(); // 显示所有收到的消息及其细节 for (int i = 0; i < lstName.length; i++) { String keyName = lstName[i].toString(); Log.e(keyName, String.valueOf(b.get(keyName))); }*/ //搜索设备时,取得设备的MAC地址 if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); String str= device.getName() + " " + device.getAddress()+" "+intent.getExtras().getShort(device.EXTRA_RSSI); //tv.setText(str); if (lstDevices.indexOf(str) == -1)// 防止重复添加 { lstDevices.add(str); // 获取设备名称和mac地址 MultiSignal ms= new MultiSignal(); ms.setMAC(device.getAddress()); ms.setLEVEL(intent.getExtras().getShort(device.EXTRA_RSSI)); boothlist.add(ms); } //adtDevices.notifyDataSetChanged(); } results = wifi.getScanResults(); handler.post(fresh); } }; IntentFilter intent = new IntentFilter(); intent.addAction(BluetoothDevice.ACTION_FOUND);// 用BroadcastReceiver来取得搜索结果 //intent.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); intent.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED); //intent.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); intent.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); registerReceiver(receiver, intent); } protected void onStop() { // TODO Auto-generated method stub unregisterReceiver(receiver); super.onStop(); } class ClickEvent implements View.OnClickListener { public void onClick(View v) { if (v == btnScan)// 搜索设备,显示结果 { if(TextUtils.isEmpty(ex.getText().toString())||TextUtils.isEmpty(ey.getText().toString()) ||TextUtils.isEmpty(ez.getText().toString())||TextUtils.isEmpty(et.getText().toString())){ Toast.makeText(MultiScannerActivity.this, "不允许有空", 10
评论
    相关推荐
    • Android学习
      Android学习Android学习Android学习Android学习Android学习Android学习
    • Android678
      Android678Android678Android678Android678Android678Android678Android678Android678Android678Android678
    • android 例子
      android 例子android 例子android 例子android 例子android 例子
    • android图片
      android精美小图片android精美小图片android精美小图片android精美小图片
    • android课件
      清华老师上课的android课件。ch_01android概述ch_02android开发基础ch_03资源的访问ch_04android用户界面ch_05Ativity和Intent。其他自己看。
    • AndroidKiller
      安卓App反编译工具AndroidKiller\AndroidKiller\AndroidKiller
    • AndroidSample
      Android示例 这是一个示例Android应用,用于降低Google Play服务和Android Volley的使用率。 有关更多详细信息,请参阅以下链接: Google Play服务: Android凌空抽射:
    • android source
      android 源代码 source android 源代码 source android 源代码 source android 源代码 source android 源代码 source android 源代码 source android 源代码 source
    • Android 教程
      Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android ...
    • android资料
      Android程序员指南 Android_UI开发专题 Android程序开发初级教程 android的五大类各种常用类的资料整理 Android高手进阶教程