藍牙4.0BLE開發(fā)
作者:zhouzhenhua
集成目前主流藍牙的demo
源碼簡介:集成目前主流藍牙的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();
- }
- // 發(fā)現(xiàn)BLE終端的Service時回調(diào)
- mBLE.setOnServiceDiscoverListener(mOnServiceDiscover);
- // 收到BLE終端數(shù)據(jù)交互的事件
- mBLE.setOnDataAvailableListener(mOnDataAvailable);
責(zé)任編輯:倪明
來源:
devstore