Android扫描附近的蓝牙设备并连接蓝牙音响的示例

  

写了一个可以扫描附近蓝牙设备的小演示,可以查看蓝牙设备的设备名和Mac地址
  

  

代码量不多,很容易看懂

     /* *   *叶应是叶   *时间:2017/9/8二十13   *描述:   */公开课ScanDeviceActivity延伸AppCompatActivity {      私人LoadingDialog LoadingDialog;      私人DeviceAdapter DeviceAdapter;      私人BluetoothAdapter BluetoothAdapter;      私人处理程序处理程序=new处理程序();      私人BroadcastReceiver discoveryReceiver=new BroadcastReceiver () {   @Override   公共空间>/* *   *叶应是叶   *时间:2017/9/8 20:02   *描述:   */公开课ConnectA2dpActivity延伸AppCompatActivity {      私人DeviceAdapter DeviceAdapter;      私人BluetoothAdapter BluetoothAdapter;      私人处理程序处理程序=new处理程序();      私人BluetoothA2dp BluetoothA2dp;      私人LoadingDialog LoadingDialog;      私人最终字符串标签=癈onnectA2dpActivity”;      私人BroadcastReceiver a2dpReceiver=new BroadcastReceiver () {      @Override   公共空间onReceive(上下文语境,意图意图){   开关(intent.getAction ()) {   案例BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED:   int connectState=intent.getIntExtra (BluetoothA2dp。EXTRA_STATE BluetoothA2dp.STATE_DISCONNECTED);   如果(connectState==BluetoothA2dp.STATE_DISCONNECTED) {   Toast.makeText (ConnectA2dpActivity。这个,”已断开连接”,Toast.LENGTH_SHORT),告诉();   }else if (connectState==BluetoothA2dp.STATE_CONNECTED) {   Toast.makeText (ConnectA2dpActivity。这个,”已连接”,Toast.LENGTH_SHORT),告诉();   }   打破;   案例BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED:   int playState=intent.getIntExtra (BluetoothA2dp。EXTRA_STATE BluetoothA2dp.STATE_NOT_PLAYING);   如果(playState==BluetoothA2dp.STATE_PLAYING) {   Toast.makeText (ConnectA2dpActivity。这个,”处于播放状态”,Toast.LENGTH_SHORT),告诉();   }else if (playState==BluetoothA2dp.STATE_NOT_PLAYING) {   Toast.makeText (ConnectA2dpActivity。这个,”未在播放”,Toast.LENGTH_SHORT),告诉();   }   打破;   }   }   };      私人BroadcastReceiver discoveryReceiver=new BroadcastReceiver () {   @Override   公共空间onReceive(上下文语境,意图意图){   开关(intent.getAction ()) {   案例BluetoothAdapter.ACTION_DISCOVERY_STARTED:   showLoadingDialog(“正在搜索蓝牙设备,搜索时间大约一分钟”);   打破;   案例BluetoothAdapter.ACTION_DISCOVERY_FINISHED:   Toast.makeText (ConnectA2dpActivity。这个,”搜索蓝牙设备结束”,Toast.LENGTH_SHORT),告诉();   hideLoadingDialog ();   打破;   案例BluetoothDevice.ACTION_FOUND:   BluetoothDevice BluetoothDevice=intent.getParcelableExtra (BluetoothDevice.EXTRA_DEVICE);   deviceAdapter.addDevice (bluetoothDevice);   deviceAdapter.notifyDataSetChanged ();   打破;   案例BluetoothDevice.ACTION_BOND_STATE_CHANGED:=intent.getIntExtra (BluetoothDevice int状态。EXTRA_BOND_STATE BluetoothDevice.BOND_NONE);   如果(状态==BluetoothDevice.BOND_BONDED) {   Toast.makeText (ConnectA2dpActivity。这个,”已连接”,Toast.LENGTH_SHORT),告诉();   }else if(状态==BluetoothDevice.BOND_NONE) {   Toast.makeText (ConnectA2dpActivity。这个,”未连接”,Toast.LENGTH_SHORT),告诉();   }   hideLoadingDialog ();   打破;   }   }   };      私人BluetoothProfile。ServiceListener profileServiceListener=new BluetoothProfile.ServiceListener () {      @Override   公共空间onServiceDisconnected (int) {   如果(profile==BluetoothProfile.A2DP) {   Toast.makeText (ConnectA2dpActivity。“onServiceDisconnected”, Toast.LENGTH_SHORT),告诉();   bluetoothA2dp=零;   }   }      @Override   公共空间onServiceConnected (int,最终BluetoothProfile代理){   如果(profile==BluetoothProfile.A2DP) {   Toast.makeText (ConnectA2dpActivity。“onServiceConnected”, Toast.LENGTH_SHORT),告诉();   bluetoothA2dp=(bluetoothA2dp)代理;   }   }   };      私人AdapterView。OnItemClickListener itemClickListener=new AdapterView.OnItemClickListener () {   @Override   公共空间onItemClick (AdapterView<& # 63;比;父,查看视图,int位置,长id) {   BluetoothDevice设备=deviceAdapter.getDevice(位置);   如果(device.getBondState ()==BluetoothDevice.BOND_BONDED) {   Toast.makeText (ConnectA2dpActivity。这个,”已连接该设备”,Toast.LENGTH_SHORT),告诉();   返回;   }   showLoadingDialog(“正在连接”);   connectA2dp(设备);   }   };         @Override   保护无效onCreate(包savedInstanceState) {   super.onCreate (savedInstanceState);   setContentView (R.layout.activity_connect_a2dp);   BluetoothManager BluetoothManager=(BluetoothManager) getSystemService (Context.BLUETOOTH_SERVICE);   bluetoothAdapter=bluetoothManager.getAdapter ();   如果(bluetoothAdapter==null | | ! getPackageManager () .hasSystemFeature (PackageManager.FEATURE_BLUETOOTH_LE)) {   Toast.makeText (ConnectA2dpActivity。这个,”当前设备不支持蓝牙”,Toast.LENGTH_SHORT),告诉();   完成();   }   bluetoothAdapter。getProfileProxy (profileServiceListener, BluetoothProfile.A2DP);   initView ();   registerDiscoveryReceiver ();   registerA2dpReceiver ();   startScan ();   }      @Override   保护无效onDestroy () {   super.onDestroy ();   handler.removeCallbacksAndMessages(空);   unregisterReceiver (a2dpReceiver);   unregisterReceiver (discoveryReceiver);   如果(bluetoothAdapter.isDiscovering ()) {   bluetoothAdapter.cancelDiscovery ();   }   }      私人空间initView () {   ListView lv_deviceList=(视图)findViewById (R.id.lv_deviceList);   deviceAdapter=new deviceAdapter(这个);   lv_deviceList.setAdapter (deviceAdapter);   lv_deviceList.setOnItemClickListener (itemClickListener);   }      私人空间registerDiscoveryReceiver () {   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

Android扫描附近的蓝牙设备并连接蓝牙音响的示例