1. 程式人生 > >Android提高第十三篇之探祕藍芽隱藏API

Android提高第十三篇之探祕藍芽隱藏API

  1. package com.testReflect;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.app.Activity;
  5. import android.bluetooth.BluetoothAdapter;
  6. import android.bluetooth.BluetoothDevice;
  7. import android.content.BroadcastReceiver;
  8. import android.content.Context;
  9. import android.content.Intent;
  10. import android.content.IntentFilter;
  11. import android.os.Bundle;
  12. import android.util.Log;
  13. import android.view.View;
  14. import android.widget.AdapterView;
  15. import android.widget.ArrayAdapter;
  16. import android.widget.Button;
  17. import android.widget.ListView;
  18. import android.widget.Toast;
  19. public class testReflect extends Activity {
  20.         Button btnSearch, btnShow;
  21.         ListView lvBTDevices;
  22.         ArrayAdapter adtDevices;
  23.         List lstDevices = new ArrayList();
  24.         BluetoothDevice btDevice;
  25.         BluetoothAdapter btAdapt;
  26.         @Override
  27.         public void onCreate(Bundle savedInstanceState) {
  28.                 super.onCreate(savedInstanceState);
  29.                 setContentView(R.layout.main);
  30.                 btnSearch = (Button) this.findViewById(R.id.btnSearch);
  31.                 btnSearch.setOnClickListener(new ClickEvent());
  32.                 btnShow = (Button) this.findViewById(R.id.btnShow);
  33.                 btnShow.setOnClickListener(new ClickEvent());
  34.                 lvBTDevices = (ListView) this.findViewById(R.id.ListView01);
  35.                 adtDevices = new ArrayAdapter(testReflect.this,
  36.                                 android.R.layout.simple_list_item_1, lstDevices);
  37.                 lvBTDevices.setAdapter(adtDevices);
  38.                 lvBTDevices.setOnItemClickListener(new ItemClickEvent());
  39.                 btAdapt = BluetoothAdapter.getDefaultAdapter();// 初始化本機藍芽功能
  40.                 if (btAdapt.getState() == BluetoothAdapter.STATE_OFF)// 開藍芽
  41.                         btAdapt.enable();
  42.                 // 註冊Receiver來獲取藍芽裝置相關的結果
  43.                 IntentFilter intent = new IntentFilter();
  44.                 intent.addAction(BluetoothDevice.ACTION_FOUND);
  45.                 intent.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
  46.                 registerReceiver(searchDevices, intent);
  47.         }
  48.         private BroadcastReceiver searchDevices = new BroadcastReceiver() {
  49.                 public void onReceive(Context context, Intent intent) {
  50.                         String action = intent.getAction();
  51.                         Bundle b = intent.getExtras();
  52.                         Object[] lstName = b.keySet().toArray();
  53.                         // 顯示所有收到的訊息及其細節
  54.                         for (int i = 0; i < lstName.length; i++) {
  55.                                 String keyName = lstName[i].toString();
  56.                                 Log.e(keyName, String.valueOf(b.get(keyName)));
  57.                         }
  58.                         // 搜尋裝置時,取得裝置的MAC地址
  59.                         if (BluetoothDevice.ACTION_FOUND.equals(action)) {
  60.                                 BluetoothDevice device = intent
  61.                                                 .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
  62.                                 if (device.getBondState() == BluetoothDevice.BOND_NONE) {
  63.                                         String str = "未配對|" + device.getName() + "|" + device.getAddress();
  64.                                         lstDevices.add(str); // 獲取裝置名稱和mac地址
  65.                                         adtDevices.notifyDataSetChanged();
  66.                                 }
  67.                         }
  68.                 }
  69.         };
  70.         class ItemClickEvent implements AdapterView.OnItemClickListener {
  71.                 @Override
  72.                 public void onItemClick(AdapterView arg0, View arg1, int arg2,
  73.                                 long arg3) {
  74.                         btAdapt.cancelDiscovery();
  75.                         String str = lstDevices.get(arg2);
  76.                         String[] values = str.split("//|");
  77.                         String address=values[2];
  78.                         btDevice = btAdapt.getRemoteDevice(address);
  79.                         try {
  80.                                 if(values[0].equals("未配對"))
  81.                                 {        
  82.                                         Toast.makeText(testReflect.this, "由未配對轉為已配對", 500).show();
  83.                                         ClsUtils.createBond(btDevice.getClass(), btDevice);
  84.                                 }
  85.                                 else if(values[0].equals("已配對"))
  86.                                 {
  87.                                         Toast.makeText(testReflect.this, "由已配對轉為未配對", 500).show();
  88.                                         ClsUtils.removeBond(btDevice.getClass(), btDevice);
  89.                                 }
  90.                         } catch (Exception e) {
  91.                                 // TODO Auto-generated catch block
  92.                                 e.printStackTrace();
  93.                         }
  94.                 }
  95.         }
  96.         /**
  97.          * 按鍵處理
  98.          * @author GV
  99.          *
  100.          */
  101.         class ClickEvent implements View.OnClickListener {
  102.                 @Override
  103.                 public void onClick(View v) {
  104.                         if (v == btnSearch) {//搜尋附近的藍芽裝置
  105.                                 lstDevices.clear();
  106.                                 Object[] lstDevice = btAdapt.getBondedDevices().toArray();
  107.                                 for (int i = 0; i < lstDevice.length; i++) {
  108.                                         BluetoothDevice device=(BluetoothDevice)lstDevice[i];
  109.                                         String str = "已配對|" + device.getName() + "|" + device.getAddress();
  110.                                         lstDevices.add(str); // 獲取裝置名稱和mac地址
  111.                                         adtDevices.notifyDataSetChanged();
  112.                                 }
  113.                                 // 開始搜尋
  114.                                 setTitle("本機藍芽地址:" + btAdapt.getAddress());
  115.                                 btAdapt.startDiscovery();
  116.                         }
  117.                         else if(v==btnShow){//顯示BluetoothDevice的所有方法和常量,包括隱藏API
  118.                                 ClsUtils.printAllInform(btDevice.getClass());
  119.                         }
  120.                 }
  121.         }
  122. }