1

我正在关注此线程/帖子中的代码: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's 类变量引用的是实际对象?mPanServicesetBluetoothTethering

我可能没有问正确的问题,所以任何关于改写我的问题的建设性批评将不胜感激。

4

1 回答 1

0

基于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

当您收到通知时onServiceConnectedmPanService将设置,否则mPanServer将为空。onServiceConnected您可以在和期间执行您喜欢的操作onServiceDisconnected

但请注意,当前的 Android 源代码存在缺陷。 将在回调您的when之前BluetoothPan设置mPanService为 null ,因此当您完成上述项目时,它可能为 null。serviceListeneronServiceDisconnectedmPanService

于 2015-03-04T07:50:46.047 回答