在Android程序中可以实现自动扫描蓝牙、配对蓝牙、建立数据通道。
蓝牙分不同类型,可以参考(http://gqdy365.iteye.com/admin/blogs/2229304)
可以入下面方法获取蓝牙设备支持的类型:
BluetoothDevice device;
Arrays.toString(device.getUuids());
我的蓝牙音箱支持的类型有:
0000111e-0000-1000-8000-00805f9b34fb:Handsfree
0000110b-0000-1000-8000-00805f9b34fb:AudioSink
0000110e-0000-1000-8000-00805f9b34fb:AVRemoteControl
00001203-0000-1000-8000-00805f9b34fb:GenericFileTransfer
这篇文字只讨论如何与蓝牙耳机(蓝牙音箱)连接。
蓝牙耳机一般都支持A2DP(蓝牙立体声,用于音乐播放)、HFP协议(通话),参考:http://gqdy365.iteye.com/admin/blogs/2231553
所以下面操作要同时操作A2DP和HFP,两个都连接成功,才算连接成功;
一、A2DP的操作可以分三步:
1、扫描蓝牙设备:
注册并监听广播:
BluetoothAdapter.ACTION_DISCOVERY_STARTED
BluetoothDevice.ACTION_FOUND
BluetoothAdapter.ACTION_DISCOVERY_FINISHED
启动扫描:
BluetoothAdapter.getDefaultAdapter().startDiscovery();
对扫描的结果按类型进行筛选,只保留我们需要的蓝牙耳机:
if(device.getBluetoothClass().getDeviceClass() == BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET
|| device.getBluetoothClass().getDeviceClass() == BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE){
//蓝牙耳机
}
2、配对指定的蓝牙设备:
这个跟配对普通蓝牙一样,方法如下:
public static boolean createBond(BluetoothDevice btDevice){
boolean result = false;
try{
Method m = btDevice.getClass().getDeclaredMethod("createBond",new Class[]{});
m.setAccessible(true);
Boolean originalResult = (Boolean) m.invoke(btDevice);
result = originalResult.booleanValue();
}catch(Exception ex){
}
return result;
}
等配对完成之后就是要建立数据连接;
3、建立数据连接:
if you SDK between 11 and 16.call a2dp.connectSink(btDevice) or a2dp.connect(btDevice)
private static IBluetoothA2dp getIBluetoothA2dp() {
IBluetoothA2dp ibta = null;
try {
final Class serviceManager = Class.forName("android.os.ServiceManager");
final Method getService = serviceManager.getDeclaredMethod("getService", String.class);
final IBinder iBinder = (IBinder) getService.invoke(null, "bluetooth_a2dp");
final Class iBluetoothA2dp = Class.forName("android.bluetooth.IBluetoothA2dp");
final Class[] declaredClasses = iBluetoothA2dp.getDeclaredClasses();
final Class c = declaredClasses[0];
final Method asInterface = c.getDeclaredMethod("asInterface", IBinder.class);
asInterface.setAccessible(true);
ibta = (IBluetoothA2dp) asInterface.invoke(null, iBinder);
} catch (final Exception e) {
Log.e("Error " + e.getMessage());
}
return ibta;
}
参考:http://stackoverflow.com/questions/8467178/working-around-a2dp-and-hfp-limitations-of-android-pre-honeycomb
如果API大于16需要用如下的方法:
private void initA2dpService(){
// Intent i = getExplicitIntent(mContext,new Intent(IBluetoothA2dp.class.getName()));//5.0以上系统需要显示intent
//详细参考http://blog.csdn.net/l2show/article/details/47421961
Intent i = new Intent(IBluetoothA2dp.class.getName());
boolean success = mContext.bindService(i, mConnection, Context.BIND_AUTO_CREATE);
if (success) {
} else {
}
}
public ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
try {
mA2dpService = IBluetoothA2dp.Stub.asInterface(service);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
};
public Intent getExplicitIntent(Context context, Intent implicitIntent) {
// Retrieve all services that can match the given intent
PackageManager pm = context.getPackageManager();
List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0);
// Make sure only one match was found
if (resolveInfo == null || resolveInfo.size() != 1) {
return null;
}
// Get component info and create ComponentName
ResolveInfo serviceInfo = resolveInfo.get(0);
String packageName = serviceInfo.serviceInfo.packageName;
String className = serviceInfo.serviceInfo.name;
ComponentName component = new ComponentName(packageName, className);
// Create a new intent. Use the old one for extras and such reuse
Intent explicitIntent = new Intent(implicitIntent);
// Set the component to be explicit
explicitIntent.setComponent(component);
return explicitIntent;
}
建立连接:mA2dpService.connect(device);
断开连接:mA2dpService.disconnect(device);
参考:http://stackoverflow.com/questions/14705167/how-connect-paired-bluetooth-a2dp-device-on-android-4-2-using-reflection
http://blog.csdn.net/qs_csu/article/details/45114251
二、HFP操作:
下面只针对4.0及以上版本;
1、初始化:
private void initOrCloseBtCheck(boolean init){
if(init){
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.getProfileProxy(mContext, new ServiceListener() {
public void onServiceConnected(int profile, BluetoothProfile proxy) {
if (profile == BluetoothProfile.HEADSET) {
mBluetoothHeadset = (BluetoothHeadset) proxy;
}
}
public void onServiceDisconnected(int profile) {
if (profile == BluetoothProfile.HEADSET) {
mBluetoothHeadset = null;
}
}
},BluetoothProfile.HEADSET);
}else{
mBluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET,mBluetoothHeadset);
}
}
建立连接:
Method m = mBluetoothHeadset.getClass().getDeclaredMethod("connect",BluetoothDevice.class);
m.setAccessible(true);
//连接Headset
boolean successHeadset = (Boolean)m.invoke(mBluetoothHeadset, device);
断开连接:
Method m = mBluetoothHeadset.getClass().getDeclaredMethod("disconnect",BluetoothDevice.class);
m.setAccessible(true);
m.invoke(mBluetoothHeadset, device);
三、状态判断:
蓝牙耳机连接成功:
mA2dpService.getConnectionState(device) == BluetoothA2dp.STATE_DISCONNECTED && mBluetoothHeadset.getConnectionState(device) == BluetoothProfile.STATE_DISCONNECTED
断开成功:
(mA2dpService.getConnectionState(device) == BluetoothA2dp.STATE_CONNECTED || mA2dpService.getConnectionState(device) == BluetoothA2dp.STATE_PLAYING)
&& mBluetoothHeadset.getConnectionState(device) == BluetoothProfile.STATE_CONNECTED
分享到:
相关推荐
当Android设备主动连接蓝牙耳机时,通常是在“蓝牙设置”中完成配对。此时,`BluetoothHeadsetService` 会接收到`BONDING_CREATED_ACTION`动作,随后尝试通过RFCOMM协议建立连接。 ```java if (action.equals...
Avrcp 对应 TG,配置在手机等接收控制指令的设备上,而 avrcpcontroller 则对应 CT,配置在蓝牙耳机、车载蓝牙等可以主动发起控制的设备上。 三、媒体浏览器服务的角色 媒体浏览器服务是 Android 系统中提供的一套...
蓝牙主要用于短距离无线通信,常见于智能硬件设备,如打印机、耳机、健康监测器等。在uni-app中,我们可以通过调用原生API来实现蓝牙功能,从而与这些设备进行数据交换。 uni-app 提供了一个名为 `uni....
或者使用BluetoothDevice的createRfcommSocketToServiceRecord()方法创建一个客户端蓝牙套接字,主动连接到服务器。 连接建立后,就可以通过BluetoothSocket的inputStream和outputStream进行数据传输。在实际应用中...
这在那些需要通过蓝牙通信的应用中很常见,如蓝牙耳机控制应用或智能家居控制系统。 #### android.permission.BLUETOOTH_ADMIN 与`BLUETOOTH`不同的是,`BLUETOOTH_ADMIN`权限允许应用发现并配对蓝牙设备。这意味...
1. 手机与耳机:蓝牙耳机是常见的蓝牙应用,用户可以在无线状态下听音乐或通话。 2. 移动设备与外设:手机、平板电脑可以通过蓝牙连接键盘、鼠标、打印机等外设。 3. 健康与健身:智能手环、心率监测器等健康设备...
杰理OTA SDK_Android版本V1.6.0是一个针对珠海市杰理科技...这些改进对于依赖于蓝牙连接进行固件更新的智能硬件产品,如TWS耳机和HID设备,尤其具有重要意义,它们能确保设备在不同环境下的顺利升级和用户友好体验。
漫步者声迈FitPods是一款具备主动降噪功能的真无线蓝牙耳机,旨在提供高品质的音频体验。连接手机的过程相对简单,适用于各种操作系统,包括苹果的iOS和安卓系统。以下是详细的连接步骤: 1. **首次连接**: - ...
Jabra捷波朗Elite 4 Active是一款专为运动爱好者设计的无线蓝牙耳机,以其稳固的佩戴感和出色的音质受到用户的喜爱。本用户手册详细介绍了这款产品的使用方法、功能特点以及保养维护,旨在帮助用户更好地理解和操作...
- 支持蓝牙双模(BR/EDR),BR/EDR和LE可同时连接。 - 内置蓝牙收发器、丰富的基带处理器和蓝牙音频文件。 ### 杰理科技——面向中低端市场 杰理科技则专注于中低端市场,其双模蓝牙芯片凭借成本优势和良好的...
NFC还可以用于设备之间的快速配对,比如蓝牙耳机或扬声器的连接。在汽车或房门解锁方面,NFC标签可以贴在门上或内置于汽车中,通过手机的NFC功能,用户只需靠近或轻触标签,就能完成解锁操作。 射频识别(RFID)...
10. **`HEADSET_STATE_CHANGED_ACTION`**: 当蓝牙耳机的状态发生改变时触发。 11. **`DISCOVERY_COMPLETED_ACTION`**: 在蓝牙扫描结束后触发。 12. **`DISCOVERY_STARTED_ACTION`**: 在蓝牙扫描开始时触发。 13. **`...
在Android开发中,监听事件是实现应用程序与用户交互、设备状态感知以及系统行为响应的关键技术。根据提供的文件信息,我们可以深入探讨一系列Android监听事件,这些事件覆盖了密码管理、设备管理、蓝牙操作等多个...
- **操作系统兼容性**:确保您的操作系统支持蓝牙,如Windows、macOS、iOS、Android等。 - **硬件支持**:确认您的设备具备蓝牙硬件支持,或配备蓝牙适配器。 ### 蓝牙网络 蓝牙不仅可以用于点对点通信,还可以...
3. **主动降噪(ANC)**:QCC300x系列支持混合式主动降噪技术,通过内置的数字信号处理器(DSP)实现内外麦克风的协同工作,有效消除环境噪音,提升音频体验。 4. **低功耗设计**:通过优化的硬件架构和软件算法,该...
此外,一些电视还支持蓝牙音频设备连接,方便用户无线播放音乐或使用蓝牙耳机。 至于压缩包内的文件,可能包含以下内容: 1. 用户手册:详尽介绍电视的使用方法、设置步骤和故障排除指南。 2. 驱动程序:确保电视与...
蓝牙则广泛用于音频设备间,如耳机、扬声器等的配对,提供无线音频播放功能。 其次,音讯功能的集成是此类设备的另一大亮点。现代设备不仅需要具备清晰的语音通话能力,还应支持高质量的音频播放和录制。这涉及到...
- **无线连接**:支持蓝牙4.0功能,可连接蓝牙外部设备。同时支持WIFI无线连接和网口有线连接。 - **USB 3.0接口**:具备超高速全双工数据传输能力,支持U盘播放3840x2160超高清视频,以及连接无线鼠标、键盘或大...
这款芯片集成了先进的蓝牙低功耗(Bluetooth Low Energy, BLE)技术,能够提供高效、稳定的无线连接,确保音乐传输的流畅性和低延迟。 **TrueWireless Mirroring技术** TrueWireless Mirroring是Qualcomm推出的一...