Android小部件支持蓝牙连接
我正在尝试通过应用程序在android nexus 5 4.4.4上启用蓝牙连接 有人对此有工作代码吗?我已经看到一些使用android.bluetooth.BluetoothPan类的例子,但是我无法让它工作 对于expamples,我在logcat中得到以下错误: java.lang.reflect.InvocationTargetException 原因:java.lang.NullPointerException 在android.bluetooth.BluetoothPan.isTetheringOnBluetoothPan.java:346 谢谢你的建议 更新: 这里有更多的细节。我尝试获取当前的栓系状态 onCreate方法:Android小部件支持蓝牙连接,android,bluetooth,tethering,Android,Bluetooth,Tethering,我正在尝试通过应用程序在android nexus 5 4.4.4上启用蓝牙连接 有人对此有工作代码吗?我已经看到一些使用android.bluetooth.BluetoothPan类的例子,但是我无法让它工作 对于expamples,我在logcat中得到以下错误: java.lang.reflect.InvocationTargetException 原因:java.lang.NullPointerException 在android.bluetooth.BluetoothPan.isTet
private boolean btEnabled = false;
BluetoothAdapter mBluetoothAdapter = null;
Class<?> classBluetoothPan = null;
Constructor<?> BTPanCtor = null;
Object BTSrvInstance = null;
Class<?> noparams[] = {};
Method mIsBTTetheringOn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
try {
RetrieveWeather();
} catch (IOException e) {
e.printStackTrace();
}
CheckBluetoothState();
Context MyContext = getApplicationContext();
mBluetoothAdapter = getBTAdapter();
try {
classBluetoothPan = Class.forName("android.bluetooth.BluetoothPan");
mIsBTTetheringOn = classBluetoothPan.getDeclaredMethod("isTetheringOn", noparams);
BTPanCtor = classBluetoothPan.getDeclaredConstructor(Context.class, BluetoothProfile.ServiceListener.class);
BTPanCtor.setAccessible(true);
BTSrvInstance = BTPanCtor.newInstance(MyContext, new BTPanServiceListener(MyContext));
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
boolean test = IsBluetoothTetherEnabled();
if (test){
Log.d("Tethering", "Bluetooth Tethering is On");
} else {
Log.d("Tethering", "Bluetooth Tethering is Off");
}
}
private BluetoothAdapter getBTAdapter() {
if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1)
return BluetoothAdapter.getDefaultAdapter();
else {
BluetoothManager bm = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
return bm.getAdapter();
}
}
// Check whether Bluetooth tethering is enabled.
private boolean IsBluetoothTetherEnabled() {
try {
if(mBluetoothAdapter != null) {
return (Boolean) mIsBTTetheringOn.invoke(BTSrvInstance, (Object []) noparams);
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
错误出现在以下行中:
返回布尔值mIsBTTetheringOn.invokeBTSrvInstance,对象[]noparams
致以最诚挚的问候欢迎来到社区!提供导致此错误的代码片段或链接是一种很好的做法。找到解决方案了吗?如果您的问题仍然没有得到回答,请检查以下内容: