zl程序教程

您现在的位置是:首页 >  其他

当前栏目

蓝牙4.0BLE开发

2023-04-18 14:56:50 时间

源码简介:集成目前主流蓝牙的demo

源码效果:

源码片段:

  1. final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); 
  2. mBluetoothAdapter = bluetoothManager.getAdapter(); 
  3.    
  4. // Checks if Bluetooth is supported on the device. 
  5. if (mBluetoothAdapter == null) { 
  6.     Toast.makeText(this, R.string.error_bluetooth_not_supported, 
  7.             Toast.LENGTH_SHORT).show(); 
  8.      finish(); 
  9.      return
  10. // 开启蓝牙 
  11. mBluetoothAdapter.enable(); 
  12.    
  13. mBLE = new BluetoothLeClass(this); 
  14. if (!mBLE.initialize()) { 
  15.     Log.e(TAG, "Unable to initialize Bluetooth"); 
  16.     finish(); 
  17. // 发现BLE终端的Service时回调 
  18. mBLE.setOnServiceDiscoverListener(mOnServiceDiscover); 
  19. // 收到BLE终端数据交互的事件 
  20. mBLE.setOnDataAvailableListener(mOnDataAvailable); 

下载地址:http://down.51cto.com/data/2096560