博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android蓝牙耳机来电铃声,Android实现积极连接蓝牙耳机
阅读量:6912 次
发布时间:2019-06-27

本文共 3625 字,大约阅读时间需要 12 分钟。

Android实现主动连接蓝牙耳机

在Android程序中可以实现自动扫描蓝牙、配对蓝牙、建立数据通道。蓝牙分不同类型,这篇文字只讨论如何与蓝牙耳机连接。

大致可以分三步:

一、扫描蓝牙设备:

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){

//蓝牙耳机

}

二、配对指定的蓝牙设备:

这个跟配对普通蓝牙一样,方法如下:

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;

}

等配对完成之后就是要建立数据连接;

三、建立数据连接:

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

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 = 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

你可能感兴趣的文章
当页面是本地页面时,通过ajax访问tomcat里的action,传递的参数在action里并不能识别...
查看>>
RocketMQ Java 客户端实现
查看>>
hdu 1133 Buy the Ticket (大数+递推)
查看>>
java:Java里数字转字符串前面自动补0的实现
查看>>
获取图片颜色的rgb,以供css设计背景颜色
查看>>
org.tinygroup.validate-验证框架
查看>>
人脸识别中的harr特征提取(转)
查看>>
Windows 8 Metro App开发[6]访问Assets文件夹
查看>>
Cpython的全局解释器锁(GIL)
查看>>
session共享方法
查看>>
ASP.NET AJAX web chat application
查看>>
14--Rails的ActiveView2
查看>>
UVa 496 - Simply Subsets
查看>>
java基础思维导图大全
查看>>
C# 面向对象7 命名空间
查看>>
MySQL单机上多实例安装
查看>>
java8 增强的Iterator遍历集合元素
查看>>
Codeforces Round #566 (Div. 2) B. Plus from Picture
查看>>
Linux命令(23)grep命令的使用
查看>>
蓝桥杯第五届B组 李白打酒
查看>>