问题1)我试图在Android设备和Windows 7 PC之间创建TCP/IP连接。为此,我使用Android的隐藏的BlutoothPan类使用Java反射API。下面是代码:Android BluetoothPAN在Android设备和Windows7之间创建TCP/IP网络PC
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的电脑,我还没有看到IP分配给我来自设备的个人电脑和我的电脑都不能通过我的Android设备的3G/4G网络访问互联网。
请建议如果这是通过蓝牙建立TCP/IP的正确方法?
问题2)我也试图从Win7 PC连接到Android设备。但是我没有找到任何Win32 API来访问Win7 PC上的蓝牙配置文件。我也尝试在Win7上自动化UI以调用控制面板小程序的单个应用程序(比如,我想以编程方式模拟右键单击我的设备 - > Connect Using - > Access Point)。
请建议是否有任何方法以编程方式访问控制面板小程序的各个项目并调用它们的操作或使用API从Win7 PC上通过蓝牙建立TCP/IP。
任何帮助,高度赞赏。
setBluetoothTethering()和isTetheringOn()调用会影响Android设备本身是否共享其Internet。尝试调用connect()而不打开BT共享。 – pmont