Android 扫描附近的蓝牙设备并连接蓝牙音响的示例
短信预约 -IT技能 免费直播动态提醒
写了一个可以扫描附近蓝牙设备的小Demo,可以查看蓝牙设备的设备名和Mac地址
代码量不多,很容易看懂
public class ScanDeviceActivity extends AppCompatActivity { private LoadingDialog loadingDialog; private DeviceAdapter deviceAdapter; private BluetoothAdapter bluetoothAdapter; private Handler handler = new Handler(); private BroadcastReceiver discoveryReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { switch (intent.getAction()) { case BluetoothAdapter.ACTION_DISCOVERY_STARTED: showLoadingDialog("正在搜索附近的蓝牙设备"); break; case BluetoothAdapter.ACTION_DISCOVERY_FINISHED: Toast.makeText(ScanDeviceActivity.this, "搜索结束", Toast.LENGTH_SHORT).show(); hideLoadingDialog(); break; case BluetoothDevice.ACTION_FOUND: BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); deviceAdapter.addDevice(bluetoothDevice); deviceAdapter.notifyDataSetChanged(); break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_scan_device); BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); bluetoothAdapter = bluetoothManager.getAdapter(); if (bluetoothAdapter == null) { Toast.makeText(this, "当前设备不支持蓝牙", Toast.LENGTH_SHORT).show(); finish(); } initView(); registerDiscoveryReceiver(); startScan(); } @Override protected void onDestroy() { super.onDestroy(); handler.removeCallbacksAndMessages(null); unregisterReceiver(discoveryReceiver); if (bluetoothAdapter.isDiscovering()) { bluetoothAdapter.cancelDiscovery(); } } private void initView() { ListView lv_deviceList = (ListView) findViewById(R.id.lv_deviceList); deviceAdapter = new DeviceAdapter(this); lv_deviceList.setAdapter(deviceAdapter); } private void registerDiscoveryReceiver() { IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); intentFilter.addAction(BluetoothDevice.ACTION_FOUND); intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); registerReceiver(discoveryReceiver, intentFilter); } private void startScan() { if (!bluetoothAdapter.isEnabled()) { if (bluetoothAdapter.enable()) { handler.postDelayed(new Runnable() { @Override public void run() { scanDevice(); } }, 1500); } else { Toast.makeText(this, "请求蓝牙权限被拒绝,请授权", Toast.LENGTH_SHORT).show(); } } else { scanDevice(); } } private void scanDevice() { if (bluetoothAdapter.isDiscovering()) { bluetoothAdapter.cancelDiscovery(); } bluetoothAdapter.startDiscovery(); } private void showLoadingDialog(String message) { if (loadingDialog == null) { loadingDialog = new LoadingDialog(this); } loadingDialog.show(message, true, false); } private void hideLoadingDialog() { if (loadingDialog != null) { loadingDialog.dismiss(); } }}
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
Android 扫描附近的蓝牙设备并连接蓝牙音响的示例
下载Word文档到电脑,方便收藏和打印~
下载Word文档
猜你喜欢
Android 扫描附近的蓝牙设备并连接蓝牙音响的示例
写了一个可以扫描附近蓝牙设备的小Demo,可以查看蓝牙设备的设备名和Mac地址代码量不多,很容易看懂public class ScanDeviceActiv
2023-05-30
C#连接蓝牙设备的实现示例
本文主要介绍了C#连接蓝牙设备的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
2023-01-30