Android BluetoothPAN to create TCP/IP network between Android device and Windows7 PC

不打扰是莪最后的温柔 提交于 2020-01-06 14:12:41

问题


Question 1) I'm trying to create a TCP/IP connection between Android device and a Windows 7 PC. For this purpose, I'm using Android's hidden BlutoothPan class using Java reflection API. Here is the code:

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();
    }  
}

Here is the log from 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

Even though the log says, the device is connected to Win7 PC, I still don't see IP Assigned to my PC from the device nor my PC can access Internet through 3G/4G network of my Android device.

Please suggest if this is the correct method to establish TCP/IP over Bluetooth?

Question 2) I'm also trying to connect from Win7 PC to Android device. But I didn't find any Win32 APIs to access Bluetooth profiles on Win7 PC. I also tried to automate the UI on Win7 to invoke Control Panel applet's individual Application (Say, I want to mimic programatically to right click on my device -> Connect Using -> Access Point).

Please suggest if there are any methods to either programatically access Control Panel applet's individual items and invoke operations on them or using APIs to establish TCP/IP over Bluetooth from Win7 PC.

Any help is highly appreciated.


回答1:


please check this library http://bluecove.org/. It may be solve your problem.



来源:https://stackoverflow.com/questions/17692368/android-bluetoothpan-to-create-tcp-ip-network-between-android-device-and-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!