android连接蓝牙音响
来源:互联网 发布:软件demo制作工具 编辑:程序博客网 时间:2024/06/10 00:27
public class BluetoothConnect { private DeviceReceiver devicecReceiver; List<BluetoothDevice> devices = new ArrayList<BluetoothDevice>(); private BluetoothA2dp mBluetoothA2dp; private BluetoothAdapter bluetoothAdapter; //蓝牙适配器 public void setDevices(List<BluetoothDevice> devices) { this.devices = devices; } public void setDevicecReceiver(DeviceReceiver DeviceReceiver) { this.devicecReceiver = DeviceReceiver; } //打开蓝牙 public void openBt() { bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if(!bluetoothAdapter.isEnabled()) { bluetoothAdapter.enable(); } else {} } //搜索蓝牙设备 public void searchBt() { devices.clear(); bluetoothAdapter.startDiscovery(); getBluetoothA2DP(); devicecReceiver.deviceRecv(); while(bluetoothAdapter.isDiscovering()); bluetoothAdapter.cancelDiscovery(); } //连接蓝牙音响 public void connect(BluetoothDevice btDev) { if(mBluetoothA2dp == null){ return; } if(bluetoothAdapter == null){ return; } if(btDev.getBondState() == BluetoothDevice.BOND_NONE) { //btDevice.createBond(); try { Method createBondMethod = BluetoothDevice.class .getMethod("createBond"); createBondMethod.invoke(btDev); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } while(btDev.getBondState() != BluetoothDevice.BOND_BONDED); try { Method connect = mBluetoothA2dp.getClass().getDeclaredMethod("connect", BluetoothDevice.class); connect.setAccessible(true); connect.invoke(mBluetoothA2dp,btDev); } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { e.printStackTrace(); } } private void getBluetoothA2DP() { if (bluetoothAdapter == null) { return; } if (mBluetoothA2dp != null) { return; } bluetoothAdapter.getProfileProxy(MainFragmentActivity.instance, new BluetoothProfile.ServiceListener() { @Override public void onServiceConnected(int profile, BluetoothProfile proxy) { if (profile == BluetoothProfile.A2DP) { //Service连接成功,获得BluetoothA2DP mBluetoothA2dp = (BluetoothA2dp) proxy; } } @Override public void onServiceDisconnected(int profile) { } }, BluetoothProfile.A2DP); } public interface DeviceReceiver { public void deviceRecv(); }}
阅读全文
0 0
- android连接蓝牙音响
- Android 扫描附近的蓝牙设备并连接蓝牙音响
- Debian 支持蓝牙音响
- [Android蓝牙]------连接蓝牙流程
- android蓝牙串口连接
- Android 蓝牙连接实现
- android 之连接蓝牙
- Android 蓝牙连接问题
- Android蓝牙设置连接
- Android蓝牙串口连接
- android 蓝牙模块连接
- Android蓝牙连接打印
- Android连接蓝牙4.0
- Android连接经典蓝牙
- android手机蓝牙连接蓝牙设备
- Android蓝牙连接远程蓝牙事例
- Android蓝牙串口连接总结
- android 蓝牙BLE多连接
- 其他类API
- contentos 端口开启
- 《大话设计模式》关于装饰者模式和代理模式个人笔记
- Maven 项目打包源文件 *-sources.jar
- linux去掉登录界面,开机自动登录root用户
- android连接蓝牙音响
- python django rest framework Class-based Views
- 3S基础知识:MapX应用讲义—加载地图数据
- Python解析JSON详解
- msys2下交叉编译fftw(android)
- Android Websocket+protobuf使用
- 如何制作一个可以上传到fir.im的ipa文件(包含最新的Xcode打包方式)
- 读取本地文件并且BASE64转码成字符串
- C/C++隐藏控制台