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

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

代码量不多,很容易看懂

/**
 * 作者:叶应是叶
 * 时间:2017/9/8 20:13
 * 描述:
 */
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();
  }
 }

}

此外,还可以通过利用反射来调用系统API,从而与支持蓝牙A2DP协议的蓝牙音响连接上,不过因为我只有一部不算严格意义上的蓝牙音响来做测试,所以这个功能并不确定是否适用于大多数蓝牙设备

/**
 * 作者:叶应是叶
 * 时间:2017/9/8 20:02
 * 描述:
 */
public class ConnectA2dpActivity extends AppCompatActivity {

 private DeviceAdapter deviceAdapter;

 private BluetoothAdapter bluetoothAdapter;

 private Handler handler = new Handler();

 private BluetoothA2dp bluetoothA2dp;

 private LoadingDialog loadingDialog;

 private final String TAG = "ConnectA2dpActivity";

 private BroadcastReceiver a2dpReceiver = new BroadcastReceiver() {

  @Override
  public void onReceive(Context context, Intent intent) {
   switch (intent.getAction()) {
    case BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED:
     int connectState = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, BluetoothA2dp.STATE_DISCONNECTED);
     if (connectState == BluetoothA2dp.STATE_DISCONNECTED) {
      Toast.makeText(ConnectA2dpActivity.this, "已断开连接", Toast.LENGTH_SHORT).show();
     } else if (connectState == BluetoothA2dp.STATE_CONNECTED) {
      Toast.makeText(ConnectA2dpActivity.this, "已连接", Toast.LENGTH_SHORT).show();
     }
     break;
    case BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED:
     int playState = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, BluetoothA2dp.STATE_NOT_PLAYING);
     if (playState == BluetoothA2dp.STATE_PLAYING) {
      Toast.makeText(ConnectA2dpActivity.this, "处于播放状态", Toast.LENGTH_SHORT).show();
     } else if (playState == BluetoothA2dp.STATE_NOT_PLAYING) {
      Toast.makeText(ConnectA2dpActivity.this, "未在播放", Toast.LENGTH_SHORT).show();
     }
     break;
   }
  }
 };

 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(ConnectA2dpActivity.this, "搜索蓝牙设备结束", Toast.LENGTH_SHORT).show();
     hideLoadingDialog();
     break;
    case BluetoothDevice.ACTION_FOUND:
     BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
     deviceAdapter.addDevice(bluetoothDevice);
     deviceAdapter.notifyDataSetChanged();
     break;
    case BluetoothDevice.ACTION_BOND_STATE_CHANGED:
     int status = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE);
     if (status == BluetoothDevice.BOND_BONDED) {
      Toast.makeText(ConnectA2dpActivity.this, "已连接", Toast.LENGTH_SHORT).show();
     } else if (status == BluetoothDevice.BOND_NONE) {
      Toast.makeText(ConnectA2dpActivity.this, "未连接", Toast.LENGTH_SHORT).show();
     }
     hideLoadingDialog();
     break;
   }
  }
 };

 private BluetoothProfile.ServiceListener profileServiceListener = new BluetoothProfile.ServiceListener() {

  @Override
  public void onServiceDisconnected(int profile) {
   if (profile == BluetoothProfile.A2DP) {
    Toast.makeText(ConnectA2dpActivity.this, "onServiceDisconnected", Toast.LENGTH_SHORT).show();
    bluetoothA2dp = null;
   }
  }

  @Override
  public void onServiceConnected(int profile, final BluetoothProfile proxy) {
   if (profile == BluetoothProfile.A2DP) {
    Toast.makeText(ConnectA2dpActivity.this, "onServiceConnected", Toast.LENGTH_SHORT).show();
    bluetoothA2dp = (BluetoothA2dp) proxy;
   }
  }
 };

 private AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
   BluetoothDevice device = deviceAdapter.getDevice(position);
   if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
    Toast.makeText(ConnectA2dpActivity.this, "已连接该设备", Toast.LENGTH_SHORT).show();
    return;
   }
   showLoadingDialog("正在连接");
   connectA2dp(device);
  }
 };

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_connect_a2dp);
  BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
  bluetoothAdapter = bluetoothManager.getAdapter();
  if (bluetoothAdapter == null || !getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
   Toast.makeText(ConnectA2dpActivity.this, "当前设备不支持蓝牙", Toast.LENGTH_SHORT).show();
   finish();
  }
  bluetoothAdapter.getProfileProxy(this, profileServiceListener, BluetoothProfile.A2DP);
  initView();
  registerDiscoveryReceiver();
  registerA2dpReceiver();
  startScan();
 }

 @Override
 protected void onDestroy() {
  super.onDestroy();
  handler.removeCallbacksAndMessages(null);
  unregisterReceiver(a2dpReceiver);
  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);
  lv_deviceList.setOnItemClickListener(itemClickListener);
 }

 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 registerA2dpReceiver() {
  IntentFilter intentFilter = new IntentFilter();
  intentFilter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
  intentFilter.addAction(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED);
  registerReceiver(a2dpReceiver, intentFilter);
 }

 private void startScan() {
  if (!bluetoothAdapter.isEnabled()) {
   if (bluetoothAdapter.enable()) {
    handler.postDelayed(new Runnable() {
     @Override
     public void run() {
      scanDevice();
     }
    }, 1500);
   } else {
    Toast.makeText(ConnectA2dpActivity.this, "请求蓝牙权限被拒绝", Toast.LENGTH_SHORT).show();
   }
  } else {
   scanDevice();
  }
 }

 private void scanDevice() {
  if (bluetoothAdapter.isDiscovering()) {
   bluetoothAdapter.cancelDiscovery();
  }
  bluetoothAdapter.startDiscovery();
 }

 public void setPriority(BluetoothDevice device, int priority) {
  try {
   Method connectMethod = BluetoothA2dp.class.getMethod("setPriority", BluetoothDevice.class, int.class);
   connectMethod.invoke(bluetoothA2dp, device, priority);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 private void connectA2dp(BluetoothDevice bluetoothDevice) {
  if (bluetoothA2dp == null || bluetoothDevice == null) {
   return;
  }
  setPriority(bluetoothDevice, 100);
  try {
   Method connectMethod = BluetoothA2dp.class.getMethod("connect", BluetoothDevice.class);
   connectMethod.invoke(bluetoothA2dp, bluetoothDevice);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 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();
  }
 }

}

这里给出源代码供大家参考:BluetoothDemo

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android6.0蓝牙出现无法扫描设备或闪退问题解决办法

    Android6.0蓝牙出现无法扫描设备或闪退问题解决办法 前言: 目前待的这家公司是做智能家居的,最近客户那边有反馈说为什么我的手机蓝牙也打开了,设备的蓝牙也打开了,为啥总是扫描不到呢,但是我们公司的测试人员几经排查,并未发现客户的所描述的扫描不到设备,但客户所说的问题确实又存在,几经周折,找到了原因,原来是现在市场上出来的android6.0手机需要添加两个权限,Android官网也已经说明了, 直接上图 具体权限官网说的很清楚了 解决办法 Android6.0设备通过蓝牙和Wi-Fi扫描访

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

    写了一个可以扫描附近蓝牙设备的小Demo,可以查看蓝牙设备的设备名和Mac地址 代码量不多,很容易看懂 /** * 作者:叶应是叶 * 时间:2017/9/8 20:13 * 描述: */ public class ScanDeviceActivity extends AppCompatActivity { private LoadingDialog loadingDialog; private DeviceAdapter deviceAdapter; private BluetoothAdap

  • Android 连接蓝牙扫码器无输入框的实现

    Android 的APP 需要集成一个蓝牙扫码器, 特别的是,需要扫码的地方是没有输入框的(EditText),不能通过直觉上理解的通过对EditText输入事件进行监听处理,取得扫码结果.并且设备也没有提供SDK. 细想了一下, 蓝牙扫码器本质应该是个HID设备,相当于蓝牙键盘.而后豁然开朗. 每一次扫码应该会触发按键事件,通过监听当前Activity的按键事件,应该可以实现,无输入框的情况下取得扫码结果. 重载Activity中的dispatchKeyEvent实现按键监听. @Overri

  • Android在类微信程序中实现蓝牙聊天功能的示例代码

    项目要求 1.初次打开程序时右上角标题栏显示"无连接",点击旁边的按钮选择"我的好友",进入配对界面: 2.选择好友之后,返回主界面,标题栏会显示已连接的手机型号: 3.两部手机间可通过蓝牙聊天 效果展示 项目结构 主要代码 1.在清单文件中注册权限 <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-permission and

  • android蓝牙简单开发示例教程

    目录 概述 1.权限申请 2.打开蓝牙 3.接收蓝牙状态的改变 4.扫描其他的设备 5.蓝牙配对 6.获取已经配对的设备 7.连接设备 概述 前段时间学习了一些蓝牙开发的知识,记录一下Android中蓝牙的简单开发.下面是最重要的两个类. BluetoothAdapter : 蓝牙适配器,通过getDefaultAdapter ()去获取一个实例,如果设备不支持蓝牙的话,返回的是一个null对象,通过它,可以打开.关闭蓝牙,扫描设备.向指定设备创建socket通道- BluetoothDevic

  • Android扫描二维码时出现用户禁止权限报错问题解决办法

    Android扫描二维码时出现用户禁止权限报错问题解决办法 当我用ZBarDecoder.jar写了一个扫描二维码的程序,确实实现了扫描功能.组长说如果用户禁止调用摄像头,那程序也不能崩溃.结果我一运行就崩溃了.在网上自己找了找,可以这样解决. try { mCameraManager.openDriver(); } catch (Exception e) { //当用户手动禁止摄像头权限时,防止系统崩溃 AlertDialog.Builder builder=new AlertDialog.B

  • Android使用socket创建简单TCP连接的方法

    本文实例讲述了Android使用socket创建简单TCP连接的方法.分享给大家供大家参考,具体如下: 不管是在Java还是Android编程中,通信都是及其重要的一部分.有连接的socket编程,重要性自然毋庸置疑. 这里以一个简单的demo演示一个最基本的socket编程. 先写服务端.服务端是Java代码.笔者懒得装eclipse等编程软件,就是直接notepad编程,dos运行的.服务端一般是新建一个绑定端口的serversocket,监听客户端请求(死循环监听).当接收到客户端消息时,

  • Android 利用广播监听usb连接状态(变化情况)

    废话不多说了,直接给大家贴代码了,具体代码如下所示: package com.lgs.test.testcode.receiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.widget.Toast; /** * Create

  • android检查手机和无线是否连接的方法

    本文实例讲述了android检查手机和无线是否连接的实现代码,分享给大家供大家参考.具体方法如下: 方法一: 主要功能代码如下: 复制代码 代码如下: ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = manager.getActiveNetworkInfo(); if(info!=null && inf

  • Android WebView或手机浏览器打开连接问题解决办法总结

    Android WebView或手机浏览器打开连接问题解决办法总结 1.通常情况下 大家可能都想使用WebView打开网页内部链接而不想再调用手机浏览器, 我们可以通过以下两种方法实现: (1)为WebView设置一个WebViewClient,并重写shouldOverrideUrlLoading(WebView view, String url)方法. class MyWebViewClient extends WebViewClient { @Override public boolean

  • Android获取热点主机ip和连接热点手机ip的代码

    其中定义WIFI AP的几个状态 public static final int WIFI_AP_STATE_DISABLING = 10; public static final int WIFI_AP_STATE_DISABLED = 11; public static final int WIFI_AP_STATE_ENABLING = 12; public static final int WIFI_AP_STATE_ENABLED = 13; public static final i

随机推荐