@Override public void onCreate(Bundle savedInstanceState) { appState = (GlobalVar) getApplicationContext(); // 获得全局变量 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // 设置成竖屏 super.onCreate(savedInstanceState); // getActionBar().setTitle(R.string.title_devices); requestWindowFeature(Window.FEATURE_NO_TITLE); // 去掉title // mHandler = new Handler(); // Use this check to determine whether BLE is supported on the device. Then you can // selectively disable BLE-related features. if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show(); finish(); } // Initializes a Bluetooth adapter. For API level 18 and above, get a reference to // BluetoothAdapter through BluetoothManager. final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); mBluetoothAdapter = bluetoothManager.getAdapter(); appState.BluetoothAdapter = mBluetoothAdapter; // Checks if Bluetooth is supported on the device. if (mBluetoothAdapter == null) { Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show(); finish(); return; } // 开启蓝牙 mBluetoothAdapter.enable(); mBLE_reciv = new BluetoothLeClassDianzichen(this); appState.mBLE_reciv_dianzichen = mBLE_reciv; // appState.init_BluetoothLeClassDianzichen(); if (!mBLE_reciv.initialize()) { Log.e(TAG, "Unable to initialize Bluetooth"); finish(); } // 发现BLE终端的Service时回调 mBLE_reciv.setOnServiceDiscoverListener(mOnServiceDiscover); // 收到BLE终端数据交互的事件 mBLE_reciv.setOnDataAvailableListener(mOnDataAvailable); mBLE_send = new BluetoothLeClassDianzichen(this); appState.mBLE_send_dianzichen = mBLE_send; if (!appState.mBLE_send_dianzichen.initialize()) { Log.e(TAG, "Unable to initialize Bluetooth"); finish(); } // 发现BLE终端的Service时回调 mBLE_send.setOnServiceDiscoverListener(mOnServiceDiscover); // 收到BLE终端数据交互的事件 mBLE_send.setOnDataAvailableListener(mOnDataAvailable); }