记录一次代码分析(2)

续接上节,我们这次主要分析的是ManagePlayerActivity

  • 成员变量
  • 
      private static LoginListener listener;
      private Button btnAddPlayer;
      private Button btnBack;
      private Button btnEditPlayer;
      private boolean isManagePlayer = true;
      private LinearLayout llFilters;
      private PlayersAdapter mPlayersAdapter;
      private GridView mPlayersGridView;
      private List playersList;
      private RelativeLayout rlRootView;
      private SharedPrefUtil sharedPrefUtil;
      ...
    
  • 主要成员函数
    • onCreate
    
      protected void onCreate(Bundle paramBundle)
      {
        super.onCreate(paramBundle);
        setContentView(R.layout.activity_manage_player);
        sharedPrefUtil = new SharedPrefUtil(this);
        ...
        try
        {
          ...
          mPlayersGridView.setOnItemClickListener(new AdapterView.OnItemClickListener()
          {
            public void onItemClick(AdapterView paramAnonymousAdapterView, View paramAnonymousView, int paramAnonymousInt, long paramAnonymousLong)
            {
              if (isManagePlayer)
              {
                paramAnonymousAdapterView = mPlayersAdapter.getItem(paramAnonymousInt);
                paramAnonymousView = new Intent(ManagePlayerActivity.this, AddEditPlayerActivity.class);
                paramAnonymousView.addFlags(65536);
                paramAnonymousView.putExtra("EXTRA_PLAYER", paramAnonymousAdapterView);
                startActivityForResult(paramAnonymousView, 0);
              }
              ...
              if (BluetoothUtil.isPlaysetConnected())
              {
                ...
                return;
              }
              paramAnonymousAdapterView = new Intent(ManagePlayerActivity.this, PlaysetScanActivity.class);
              ...
              startActivityForResult(paramAnonymousAdapterView, 0);
            }
          });
          ...
          return;
        }
        catch (Exception paramBundle)
        {
          for (;;)
          {
            paramBundle.printStackTrace();
          }
        }
      }
    

    下面来看下PlaysetScanActivity主要用来Scan!
    PlaysetScanActivity同样继承BluetoothBaseActivity。

  • 成员变量
  • 
      private static final int REQUEST_ENABLE_BT = 1;
      private static final long SCAN_PERIOD = 10000L;
      private static final String TAG = PlaysetScanActivity.class.getName();
      private BluetoothStateReceiver bluetoothStateReceiver;
      ...
      private FlowLayout flowLayout;
      private boolean isAudioPlayed = false;
      private boolean isLettersRead = false;
      private boolean isPaused;
      private boolean isPermissionGranted = true;
      private boolean isPlaysetSelected = false;
      ...
      private BluetoothAdapter mBluetoothAdapter;
      private String mDeviceAddress;
      private String mDeviceName;
      private Handler mHandler;
      // 蓝牙设备列表
      private ArrayList mLeDevices = new ArrayList();
    

    这个类也非常重要,我想我们离成功就更近一步了。有关蓝牙(BLE)的相关内容,博客的文章也有所提及,我这边回慢慢完善。

  • 重要成员函数
    • onCreate
    
      public void onCreate(Bundle paramBundle)
      {
        ...
        // 蓝牙接收器
        bluetoothStateReceiver = new BluetoothStateReceiver();
        // 判断是否支持BLE
        if (!getPackageManager().hasSystemFeature("android.hardware.bluetooth_le"))
        {
          ...
          finish();
        }
        // 获取蓝牙适配器
        mBluetoothAdapter = ((BluetoothManager)getSystemService("bluetooth")).getAdapter();
        if (mBluetoothAdapter == null)
        {
          // 蓝牙不支持
          return;
        }
        if (sharedPrefUtil.getBoolean("IS_IDLE_TIME"))
        {
          //
        }
        paramBundle = new IntentFilter("android.bluetooth.adapter.action.STATE_CHANGED");
        // 注册蓝牙广播
        registerReceiver(bluetoothStateReceiver, paramBundle);
      }
    
      onResume
    
      protected void onResume()
      {
        ...
        isPaused = false;
        // 蓝牙关闭
        if (!mBluetoothAdapter.isEnabled())
        {
          ...
        }
        // 蓝牙没有连接
        if ((!BluetoothUtil.isPlaysetConnected()) && (!BluetoothUtil.getFirmwareUpdateState()) && (!BluetoothUtil.isBootModeEnabled()))
        {
          isPlaysetSelected = false;
          isAudioPlayed = false;
          if (!mScanning) {
            new Handler().postDelayed(new Runnable()
            {
              public void run()
              {
                PlaysetScanActivity.this.scanLeDevice(true);
              }
            }, 1000L);
          }
        }
      }
    

    开始扫描设备进行连接scanLeDevice

    
      private void scanLeDevice(boolean paramBoolean)
      {
        if (mBluetoothAdapter == null) {
          return;
        }
        if (paramBoolean)
        {
          mScanning = true;
          // 清空当前列表
          clearScanList();
          mHandler.postDelayed(new Runnable()
          {
            public void run()
            {
              if ((mScanning) && (!isPlaysetSelected))
              {
                ...
              }
            }
          }, 10000L);
          mHandler.postDelayed(new Runnable()
          {
            public void run()
            {
              // 设备不存在
              if ((mLeDevices.size() == 0) && (!isPlaysetSelected) && (!BluetoothUtil.isPlaysetConnected())) {
                ...
              }
              BluetoothDevice localBluetoothDevice;
              ArrayList localArrayList;
              do
              {
                do
                {
                  return;
                } while ((getIntent().hasExtra("EXTRA_CHOOSE_PLAYSET")) || (isPlaysetSelected) || (sharedPrefUtil.getStringSet("REMEMBER_PLAYSET").size() <= 0) || (flowLayout.getChildCount() != 1));
                localBluetoothDevice = (BluetoothDevice)flowLayout.getChildAt(0).getTag();
                localArrayList = new ArrayList();
                localArrayList.addAll(sharedPrefUtil.getStringSet("REMEMBER_PLAYSET"));
              } while ((!localArrayList.contains(localBluetoothDevice.getAddress())) || (isPaused));
              // 自动调用Click事件
              flowLayout.getChildAt(0).performClick();
            }
          }, 2000L);
          BluetoothAdapter localBluetoothAdapter = mBluetoothAdapter;
          UUID localUUID = Constants.FILTER_UUID_FOR_SCAN;
          // 设置回调
          BluetoothAdapter.LeScanCallback localLeScanCallback = mLeScanCallback;
          localBluetoothAdapter.startLeScan(new UUID[] { localUUID }, localLeScanCallback);
          return;
        }
        mScanning = false;
        mBluetoothAdapter.stopLeScan(mLeScanCallback);
      }
    

    下面看下回调mLeScanCallback

    
      public void onLeScan(final BluetoothDevice paramAnonymousBluetoothDevice, int paramAnonymousInt, byte[] paramAnonymousArrayOfByte)
        {
          runOnUiThread(new Runnable()
          {
            public void run()
            {
              PlaysetScanActivity localPlaysetScanActivity;
              if (!isAudioPlayed)
              {
                localPlaysetScanActivity = PlaysetScanActivity.this;
                ...
              }
              for (boolean bool = true;; bool = false)
              {
                ...
                PlaysetScanActivity.this.addDevice(paramAnonymousBluetoothDevice);
                ...
                return;
              }
            }
          });
        }
      };
    

    我们此时加入了新的蓝牙连接设备对象addDevice

    
      private void addDevice(BluetoothDevice paramBluetoothDevice)
      {
        ...
        // 判断当前是否存在该设备
        if (!mLeDevices.contains(paramBluetoothDevice))
        {
          mLeDevices.add(paramBluetoothDevice);
          localView = getLayoutInflater().inflate(R.layout.device_item, null);
          localTextView = (TextView)localView.findViewById(R.id.tvDeviceName);
          String str = paramBluetoothDevice.getName();
          // 不存在设备则break;
          if ((str == null) || (str.length() <= 0)) {
            ...
          }
          localTextView.setText(str);
        }
        // 设置点击回调事件
        for (;;)
        {
          localView.setTag(paramBluetoothDevice);
          localView.setOnClickListener(new View.OnClickListener()
          {
            public void onClick(View paramAnonymousView)
            {
              // 用户选择了则return
              if (isPlaysetSelected) {
                return;
              }
              ...
              BluetoothDevice localBluetoothDevice = (BluetoothDevice)paramAnonymousView.getTag();
              if ((localBluetoothDevice == null) || (BluetoothUtil.isPlaysetConnected()))
              {
                ...
              }
              ...
              // 终止扫描
              SPPlaysetScanActivity.this.scanLeDevice(false);
              // 连接设备!!!
              boolean bool = BluetoothUtil.connectPlayset(mDeviceAddress, mDeviceName);
              ...
            }
          });
          ...
        }
      }
    

    该Activity中还包括检测硬件升级(checkFirmwareUpdate)、初始化UI(initUI)、bleLettersFromPlayset等函数。

    欢迎留言

    avatar
      Subscribe  
    Notify of