iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >Android 扫描附近的蓝牙设备并连接蓝牙音响的示例
  • 688
分享到

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

android扫描蓝牙 2023-05-30 20:05:56 688人浏览 独家记忆
摘要

写了一个可以扫描附近蓝牙设备的小Demo,可以查看蓝牙设备的设备名和Mac地址代码量不多,很容易看懂public class ScanDeviceActivity extends AppCompatActivity { private Lo

写了一个可以扫描附近蓝牙设备的小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();  } }}

--结束END--

本文标题: Android 扫描附近的蓝牙设备并连接蓝牙音响的示例

本文链接: https://www.lsjlt.com/news/221183.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作