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

所属分类: 软件编程 / Android 阅读数: 25
收藏 0 赞 0 分享

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

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

更多精彩内容其他人还在看

Android中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多