问题 1) 我正在尝试在 Android 设备和 Windows 7 PC 之间创建 TCP/IP 连接。为此,我使用了使用 Java 反射 API 的 Android 隐藏的 BlutoothPan 类。这是代码:
private void invokeConnectMehotd() {
String sClassName = "android.bluetooth.BluetoothPan";
try {
Class<?> classBluetoothPan = Class.forName(sClassName);
Constructor<?> ctor = classBluetoothPan.getDeclaredConstructor(Context.class, ServiceListener.class);
ctor.setAccessible(true);
Object instance = ctor.newInstance(mContext, mServiceListener);
if(mPairedBluetoothDevice != null) {
// Set Tethering ON
Class[] paramSet = new Class[1];
paramSet[0] = boolean.class;
Method setTetheringOn = classBluetoothPan.getDeclaredMethod("setBluetoothTethering", paramSet);
setTetheringOn.invoke(instance, true);
// IsTetheringOn?
Class<?> noparams[] = {};
Method m = classBluetoothPan.getDeclaredMethod("isTetheringOn", noparams);
boolean isTetheringOn = ((Boolean) m.invoke(instance, (Object []) noparams)).booleanValue();
Log.d("Tether", "Tethered = "+ isTetheringOn);
// Connect to remote device
Class[] paramDevice = new Class[1];
paramDevice[0] = BluetoothDevice.class;
Method connect = classBluetoothPan.getDeclaredMethod("connect", paramDevice);
boolean isConnected = ((Boolean) connect.invoke(instance, mPairedBluetoothDevice)).booleanValue();
Log.d("Connected", "Connected = "+ isConnected);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
这是来自 logcat 的日志:
07-16 23:04:04.514: D/GLWebViewState(8937): Reinit shader
07-16 23:04:04.604: D/GLWebViewState(8937): Reinit transferQueue
07-16 23:04:12.452: E/QLBluetoothServer(8937): L-AV-SUDGUDI01
07-16 23:04:12.452: E/QLBluetoothServer(8937): E0:2A:82:2C:6E:E0
07-16 23:04:14.123: E/QLBluetoothServer(8937): HPTEST-PC
07-16 23:04:14.123: E/QLBluetoothServer(8937): 00:27:13:DC:AB:FD
07-16 23:04:18.608: D/BluetoothPan(8937): BluetoothPan() call bindService
07-16 23:04:18.628: D/BluetoothPan(8937): BluetoothPAN Proxy object connected
07-16 23:04:18.638: D/BluetoothPan(8937): BluetoothPan(), bindService called
07-16 23:04:19.318: D/BluetoothPan(8937): setBluetoothTethering(true)
07-16 23:04:19.328: D/BluetoothPan(8937): isTetheringOn()
07-16 23:04:19.338: D/Tether(8937): Tethered = true
07-16 23:04:20.469: D/BluetoothPan(8937): connect(E0:2A:82:2C:6E:E0)
07-16 23:04:20.529: D/Connected(8937): Connected = true
即使日志显示设备已连接到 Win7 PC,我仍然看不到从设备分配给我的 PC 的 IP,我的 PC 也无法通过我的 Android 设备的 3G/4G 网络访问 Internet。
请建议这是否是通过蓝牙建立 TCP/IP 的正确方法?
问题 2) 我也在尝试从 Win7 PC 连接到 Android 设备。但我没有找到任何 Win32 API 来访问 Win7 PC 上的蓝牙配置文件。我还尝试在 Win7 上自动化 UI 以调用控制面板小程序的单个应用程序(例如,我想以编程方式模拟以右键单击我的设备 -> 使用连接 -> 接入点)。
请建议是否有任何方法可以以编程方式访问控制面板小程序的各个项目并对其调用操作,或者使用 API 从 Win7 PC 通过蓝牙建立 TCP/IP。
非常感谢任何帮助。