Android蓝牙类在启用、发现、列出配对设备和连接到蓝牙设备方面非常容易使用。
我的计划是发起到另一个蓝牙设备的连接,该设备通过蓝牙提供系留功能。
经过一些调查,这看起来并不可行-看起来我必须自己实现这个配置文件,并拥有root访问权限来进行联网,并在一个应用程序中做所有的事情。
似乎也没有什么意图可以通过设置触发来启动蓝牙连接,我能做的最多就是打开它。
我是不是遗漏了什么--如果系统没有公开启动系统级蓝牙连接的方法,我是不是不走运?
发布于 2014-12-26 23:48:06
接口中已经存在私有配置文件:BluetoothPan
蓝牙PAN (Personal Area Network,个人区域网)是识别蓝牙上的系留的正确名称。
这个私有类允许您通过public boolean connect(BluetoothDevice device)和public boolean disconnect(BluetoothDevice device)方法连接到暴露PAN蓝牙配置文件的设备和从该设备断开连接。
以下是连接到特定设备的示例代码片段:
String sClassName = "android.bluetooth.BluetoothPan";
class BTPanServiceListener implements BluetoothProfile.ServiceListener {
private final Context context;
public BTPanServiceListener(final Context context) {
this.context = context;
}
@Override
public void onServiceConnected(final int profile,
final BluetoothProfile proxy) {
Log.e("MyApp", "BTPan proxy connected");
BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice("AA:BB:CC:DD:EE:FF"); //e.g. this line gets the hardware address for the bluetooth device with MAC AA:BB:CC:DD:EE:FF. You can use any BluetoothDevice
try {
Method connectMethod = proxy.getClass().getDeclaredMethod("connect", BluetoothDevice.class);
if(!((Boolean) connectMethod.invoke(proxy, device))){
Log.e("MyApp", "Unable to start connection");
}
} catch (Exception e) {
Log.e("MyApp", "Unable to reflect android.bluetooth.BluetoothPan", e);
}
}
@Override
public void onServiceDisconnected(final int profile) {
}
}
try {
Class<?> classBluetoothPan = Class.forName(sClassName);
Constructor<?> ctor = classBluetoothPan.getDeclaredConstructor(Context.class, BluetoothProfile.ServiceListener.class);
ctor.setAccessible(true);
Object instance = ctor.newInstance(getApplicationContext(), new BTPanServiceListener(getApplicationContext()));
} catch (Exception e) {
Log.e("MyApp", "Unable to reflect android.bluetooth.BluetoothPan", e);
}https://stackoverflow.com/questions/9936551
复制相似问题