我在以下线程/帖子中跟踪代码:Enable bluetooth tethering android programmatically(随后,此帖子:How to check Bluetooth tethering status programmatically in Android),遇到了一个障碍。我在用着
anirudh reddy的代码。
当我调用线

setTetheringOn.invoke(instance,true);


我得到一个NullPointerException。这是LogCat堆栈跟踪:

12-21 13:11:16.655: W/System.err(12738): java.lang.reflect.InvocationTargetException
12-21 13:11:16.655: W/System.err(12738):    at java.lang.reflect.Method.invoke(Native Method)
12-21 13:11:16.655: W/System.err(12738):    at java.lang.reflect.Method.invoke(Method.java:372)
12-21 13:11:16.655: W/System.err(12738):    at com.myname.android.bluetoothtetheringwidget.BluetoothTetheringActivity.turnBluetoothTetheringOn(BluetoothTetheringActivity.java:50)
12-21 13:11:16.655: W/System.err(12738):    at com.myname.android.bluetoothtetheringwidget.BluetoothTetheringActivity.onCreate(BluetoothTetheringActivity.java:27)
12-21 13:11:16.655: W/System.err(12738):    at android.app.Activity.performCreate(Activity.java:5933)
12-21 13:11:16.655: W/System.err(12738):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
12-21 13:11:16.655: W/System.err(12738):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
12-21 13:11:16.655: W/System.err(12738):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
12-21 13:11:16.656: W/System.err(12738):    at android.app.ActivityThread.access$800(ActivityThread.java:144)
12-21 13:11:16.656: W/System.err(12738):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
12-21 13:11:16.656: W/System.err(12738):    at android.os.Handler.dispatchMessage(Handler.java:102)
12-21 13:11:16.656: W/System.err(12738):    at android.os.Looper.loop(Looper.java:135)
12-21 13:11:16.656: W/System.err(12738):    at android.app.ActivityThread.main(ActivityThread.java:5221)
12-21 13:11:16.656: W/System.err(12738):    at java.lang.reflect.Method.invoke(Native Method)
12-21 13:11:16.656: W/System.err(12738):    at java.lang.reflect.Method.invoke(Method.java:372)
12-21 13:11:16.656: W/System.err(12738):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
12-21 13:11:16.656: W/System.err(12738):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
12-21 13:11:16.656: W/System.err(12738): Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void android.bluetooth.IBluetoothPan.setBluetoothTethering(boolean)' on a null object reference
12-21 13:11:16.656: W/System.err(12738):    at android.bluetooth.BluetoothPan.setBluetoothTethering(BluetoothPan.java:337)
12-21 13:11:16.656: W/System.err(12738):    ... 17 more


在我的BluetoothTetheringActivity中,我的onCreate方法调用turnBluetoothTetheringOn方法。

继续前进,我进入调试模式(我正在使用Eclipse Luna),当我进入android.bluetooth.BluetoothPan.setBluetoothTethering方法时发现了一些有趣的东西:类变量mPanService为null。这很奇怪,因为代码明确使用了对mPanService的引用。这是android.bluetooth.BluetoothPan.setBluetoothTethering中的代码:

public void setBluetoothTethering(boolean value) {
    if (DBG) log("setBluetoothTethering(" + value + ")");
    try {
        mPanService.setBluetoothTethering(value);
    } catch (RemoteException e) {
        Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
    }
}


我不确定这段代码是最新的,但是一个名为grepcode.com的网站有一个BluetoothPan代码页面:android.bluetooth.BluetoothPan。在此代码中,mPanService被称为mService

在尝试调用BluetoothPan之前,如何确保mPanService的类变量setBluetoothTethering引用了实际对象?

我可能没有问正确的问题,因此,如果您对我的问题措辞有建设性的批评,将不胜感激。

最佳答案

基于Android源代码:
http://androidxref.com/5.0.0_r2/xref/frameworks/base/core/java/android/bluetooth/BluetoothPan.java
http://androidxref.com/5.0.0_r2/xref/frameworks/base/core/java/android/bluetooth/BluetoothAdapter.java

建议通过以下方式获得Pan Proxy:

getProfileProxy@BluetoothAdapter
public boolean getProfileProxy(Context context, BluetoothProfile.ServiceListener listener,int profile)


并将您的ServiceListener作为参数传递来接收以下事件:

(1)onServiceConnected

(2)onServiceDisconnected

当通知您onServiceConnected时,将设置mPanService,否则,mPanServer将为null。您可以在onServiceConnectedonServiceDisconnected期间执行首选操作。

但请注意,当前的Android源代码存在缺陷。
当调用BluetoothPan时,mPanService将在回调到serviceListener之前将onServiceDisconnected设置为null,因此完成上述各项后,mPanService可能为null。

07-27 21:36