Android 扫描附近的蓝牙设备并连接蓝牙音响的示例
作者:叶应是叶 发布时间:2022-05-14 05:36:48
标签: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
来源:http://www.jianshu.com/p/e2a24e871575?utm_source=tuicool&utm_medium=referral


猜你喜欢
- 一、熔断器简介微服务架构特点就是多服务,多数据源,支撑系统应用。这样导致微服务之间存在依赖关系。如果其中一个服务故障,可能导致系统宕机,这就
- JAXBContext 设置xml节点属性在使用JAXBContext将javaBean转化为xml时会出现这样的需求:<xml ve
- 在字符集中,有一类字符具有这样的特性:当从键盘上输入这个字符时,显示器上就可以显示这个字符,即输入什么就显示什么。这类字符称为可显示字符,如
- 本文实例为大家分享了android Matrix图片随意放大缩小和拖动的具体代码,供大家参考,具体内容如下step1:新建一个项目DragA
- 本文实例讲述了C#使用GDI+创建缩略图的方法,分享给大家供大家参考。具体方法分析如下:C#的Gdi+还是相当好用的。创建缩略图步骤如下:1
- 嵌套类型在类或结构内部定义的类型称为嵌套类型。例如:class Container{ class Nested {
- springboot通过URL方式访问外部资源遇到这个问题时翻阅百度,无外乎就是两种方式第一种在springboot 2.1.8中该方法已过
- 本文将介绍如何通过Java程序来查找并高亮PDF中的文本。使用工具:Free Spire.PDF for Java(免费版)Jar文件获取及
- 近期由于负责项目的一个模块,该模块下有很多分类,每个分类都有一个编码code,这个值是作为一个参数携带过来的。但是每个code确实对应一个方
- 最近有一个需求是选择多级联动数据,数据级别不固定,可能是五级,可能是两级,具体看用户等级。所以就需要一个多级联动选择控件 ,在网上一番搜索或
- 什么是队列结构一种线性结构,具有特殊的运算法则【只能在一端(队头)删除,在另一端(队尾)插入】。分类:顺序队列结构链式队列结构基本操作:入队
- 报错信息如下:java: 无法访问org.springframework.boot.SpringApplication错误的类文件: /C:
- 1、引入依赖<dependency><groupId>org.springframework.boot</gr
- 当你在使用Mybatis 时进行配置的时候有这样几个坑一定要注意下。mybatisplus中逻辑删除通俗说为了在数据库中保留数据,但是又不想
- 本文实例讲述了Android编程开发中的正则匹配操作。分享给大家供大家参考,具体如下:在Android开发中,可能也会遇到一下输入框的合法性
- 请求参数解析客户端请求在handlerMapping中找到对应handler后,将会继续执行DispatchServlet的doPatch(
- 当你使用synchronized关键字的时候,是通过互斥器来保障线程安全以及对共享资源的同步访问。线程间也经常需要更进一步的协调
- 以下弹出框是框的实现,放入到SWT项目下就可运行。1.提示框MessageBox mb = new MessageBox(shell,SWT
- .net内存回收与Dispose﹐Close﹐Finalize方法一. net的对象使用一般分为三种情况﹕1.创建对象2.使用对象3.释放对
- 在javaweb中写了一个图片的链接,可以打开预览,另外提供一个下载功能。以下是预览代码,没什么好说的;href若连接的是一个压缩包文件之类