2013-07-17 34 views
0

問題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。

任何幫助,高度讚賞。

+0

setBluetoothTethering()和isTetheringOn()調用會影響Android設備本身是否共享其Internet。嘗試調用connect()而不打開BT共享。 – pmont

回答

0

請檢查這個庫http://bluecove.org/。這可能會解決你的問題。

+0

您能否詳細說明bluecove的哪一部分可以幫助我。由於我不是在尋找服務(服務發現,然後連接使用藍牙套接字)基於連接,bluecove似乎並沒有對我有用。但是,我可能會錯過一些東西。 –

相關問題