源码简介:集成目前主流蓝牙的demo
源码效果:
源码片段:
- final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
- mBluetoothAdapter = bluetoothManager.getAdapter();
- // 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 = new BluetoothLeClass(this);
- if (!mBLE.initialize()) {
- Log.e(TAG, "Unable to initialize Bluetooth");
- finish();
- }
- // 发现BLE终端的Service时回调
- mBLE.setOnServiceDiscoverListener(mOnServiceDiscover);
- // 收到BLE终端数据交互的事件
- mBLE.setOnDataAvailableListener(mOnDataAvailable);